|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tape') |
| 4 | +const testCommon = require('./common') |
| 5 | +const sourceData = [] |
| 6 | + |
| 7 | +// For this test the number of records in the db must be a multiple of |
| 8 | +// the hardcoded fast-future limit (1000) or a cache size limit in C++. |
| 9 | +for (let i = 0; i < 1e4; i++) { |
| 10 | + sourceData.push({ |
| 11 | + type: 'put', |
| 12 | + key: i.toString(), |
| 13 | + value: '' |
| 14 | + }) |
| 15 | +} |
| 16 | + |
| 17 | +test('setUp', testCommon.setUp) |
| 18 | + |
| 19 | +test('iterator does not starve event loop', function (t) { |
| 20 | + t.plan(6) |
| 21 | + |
| 22 | + const db = testCommon.factory() |
| 23 | + |
| 24 | + db.open(function (err) { |
| 25 | + t.ifError(err, 'no open error') |
| 26 | + |
| 27 | + // Insert test data |
| 28 | + db.batch(sourceData.slice(), function (err) { |
| 29 | + t.ifError(err, 'no batch error') |
| 30 | + |
| 31 | + // Set a high highWaterMark to fill up the cache entirely |
| 32 | + const it = db.iterator({ highWaterMark: Math.pow(1024, 3) }) |
| 33 | + |
| 34 | + let breaths = 0 |
| 35 | + let entries = 0 |
| 36 | + let scheduled = false |
| 37 | + |
| 38 | + // Iterate continuously while also scheduling work with setImmediate(), |
| 39 | + // which should be given a chance to run because we limit the tick depth. |
| 40 | + const next = function () { |
| 41 | + it.next(function (err, key, value) { |
| 42 | + if (err || (key === undefined && value === undefined)) { |
| 43 | + t.ifError(err, 'no next error') |
| 44 | + t.is(entries, sourceData.length, 'got all data') |
| 45 | + t.is(breaths, sourceData.length / 1000, 'breathed while iterating') |
| 46 | + |
| 47 | + return db.close(function (err) { |
| 48 | + t.ifError(err, 'no close error') |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + entries++ |
| 53 | + |
| 54 | + if (!scheduled) { |
| 55 | + scheduled = true |
| 56 | + setImmediate(function () { |
| 57 | + breaths++ |
| 58 | + scheduled = false |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + next() |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + next() |
| 67 | + }) |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +test('iterator with seeks does not starve event loop', function (t) { |
| 72 | + t.plan(6) |
| 73 | + |
| 74 | + const db = testCommon.factory() |
| 75 | + |
| 76 | + db.open(function (err) { |
| 77 | + t.ifError(err, 'no open error') |
| 78 | + |
| 79 | + db.batch(sourceData.slice(), function (err) { |
| 80 | + t.ifError(err, 'no batch error') |
| 81 | + |
| 82 | + const it = db.iterator({ highWaterMark: Math.pow(1024, 3), limit: sourceData.length }) |
| 83 | + |
| 84 | + let breaths = 0 |
| 85 | + let entries = 0 |
| 86 | + let scheduled = false |
| 87 | + |
| 88 | + const next = function () { |
| 89 | + it.next(function (err, key, value) { |
| 90 | + if (err || (key === undefined && value === undefined)) { |
| 91 | + t.ifError(err, 'no next error') |
| 92 | + t.is(entries, sourceData.length, 'got all data') |
| 93 | + t.is(breaths, sourceData.length, 'breathed while iterating') |
| 94 | + |
| 95 | + return db.close(function (err) { |
| 96 | + t.ifError(err, 'no close error') |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + entries++ |
| 101 | + |
| 102 | + if (!scheduled) { |
| 103 | + // Seeking clears the cache, which should only have a positive |
| 104 | + // effect because it means the cache must be refilled, which |
| 105 | + // again gives us time to breathe. This is a smoke test, really. |
| 106 | + it.seek(sourceData[0].key) |
| 107 | + |
| 108 | + scheduled = true |
| 109 | + setImmediate(function () { |
| 110 | + breaths++ |
| 111 | + scheduled = false |
| 112 | + }) |
| 113 | + } |
| 114 | + |
| 115 | + next() |
| 116 | + }) |
| 117 | + } |
| 118 | + |
| 119 | + next() |
| 120 | + }) |
| 121 | + }) |
| 122 | +}) |
| 123 | + |
| 124 | +test('tearDown', testCommon.tearDown) |
0 commit comments