|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const {testdir} = require('./util/common'); |
| 4 | +const nurkel = require('..'); |
| 5 | +const {runForked} = require('./util/worker'); |
| 6 | +const fs = require('bfile'); |
| 7 | + |
| 8 | +const cleanupTests = { |
| 9 | + openTree: { |
| 10 | + name: 'should clean up opened tree', |
| 11 | + fn: async (prefix) => { |
| 12 | + const tree = nurkel.create({ prefix }); |
| 13 | + |
| 14 | + await tree.open(); |
| 15 | + } |
| 16 | + }, |
| 17 | + openMultipleTrees: { |
| 18 | + name: 'should clean up multiple opened trees', |
| 19 | + fn: async (prefix) => { |
| 20 | + for (let i = 0; i < 10; i++) { |
| 21 | + const prefixN = `${prefix}/${i}`; |
| 22 | + const tree = nurkel.create({ prefix: prefixN }); |
| 23 | + |
| 24 | + await tree.open(); |
| 25 | + } |
| 26 | + } |
| 27 | + }, |
| 28 | + openClosingTree: { |
| 29 | + name: 'should clean up opened and closed tree', |
| 30 | + fn: async (prefix) => { |
| 31 | + const tree = nurkel.create({ prefix }); |
| 32 | + |
| 33 | + await tree.open(); |
| 34 | + tree.close(); |
| 35 | + } |
| 36 | + }, |
| 37 | + openClosingTrees: { |
| 38 | + name: 'should clean up multiple opened and closed trees', |
| 39 | + fn: async (prefix) => { |
| 40 | + for (let i = 0; i < 10; i++) { |
| 41 | + const prefixN = `${prefix}/${i}`; |
| 42 | + const tree = nurkel.create({ prefix: prefixN }); |
| 43 | + |
| 44 | + await tree.open(); |
| 45 | + tree.close(); |
| 46 | + } |
| 47 | + } |
| 48 | + }, |
| 49 | + openTreeTx: { |
| 50 | + name: 'should clean up opened tree with transaction', |
| 51 | + fn: async (prefix) => { |
| 52 | + const tree = nurkel.create({ prefix }); |
| 53 | + |
| 54 | + await tree.open(); |
| 55 | + |
| 56 | + const tx = tree.txn(); |
| 57 | + await tx.open(); |
| 58 | + } |
| 59 | + }, |
| 60 | + openTreesTxs: { |
| 61 | + name: 'should clean up multiple opened trees with transactions', |
| 62 | + fn: async (prefix) => { |
| 63 | + for (let i = 0; i < 10; i++) { |
| 64 | + const prefixN = `${prefix}/${i}`; |
| 65 | + const tree = nurkel.create({ prefix: prefixN }); |
| 66 | + |
| 67 | + await tree.open(); |
| 68 | + |
| 69 | + const txs = []; |
| 70 | + for (let t = 0; t < 20; t++) { |
| 71 | + const tx = tree.txn(); |
| 72 | + await tx.open(); |
| 73 | + txs.push(tx); |
| 74 | + |
| 75 | + // close half |
| 76 | + if (t < 10) |
| 77 | + tx.close(); |
| 78 | + } |
| 79 | + |
| 80 | + // close 10th tree |
| 81 | + if (i === 10) |
| 82 | + tree.close(); |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + openTreeTxIter: { |
| 87 | + name: 'should clean up opened tree with transaction and iterator', |
| 88 | + fn: async (prefix) => { |
| 89 | + const tree = nurkel.create({ prefix }); |
| 90 | + |
| 91 | + await tree.open(); |
| 92 | + |
| 93 | + const tx = tree.txn(); |
| 94 | + await tx.open(); |
| 95 | + |
| 96 | + tx.iterator(); |
| 97 | + |
| 98 | + // TODO: separate iterator init from open. |
| 99 | + // const iter = tx.iterator(); |
| 100 | + // await iter.open(); |
| 101 | + } |
| 102 | + }, |
| 103 | + openTreesTxsIters: { |
| 104 | + name: 'should clean up multiple opened trees with transactions and iterators', |
| 105 | + fn: async (prefix) => { |
| 106 | + for (let i = 0; i < 10; i++) { |
| 107 | + const tree = nurkel.create({ prefix: `${prefix}/${i}` }); |
| 108 | + |
| 109 | + await tree.open(); |
| 110 | + |
| 111 | + for (let t = 0; t < 20; t++) { |
| 112 | + const tx = tree.txn(); |
| 113 | + await tx.open(); |
| 114 | + |
| 115 | + for (let j = 0; j < 20; j++) { |
| 116 | + const iter = tx.iterator(); |
| 117 | + |
| 118 | + if (j === 10) |
| 119 | + iter.end(); |
| 120 | + } |
| 121 | + |
| 122 | + if (t < 10) |
| 123 | + tx.close(); |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | + timeout: 3000 |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +const isForked = Boolean(process.send); |
| 132 | +const isMainThread = !isForked; |
| 133 | + |
| 134 | +if (!isMainThread) { |
| 135 | + const data = JSON.parse(process.env.WORKER_DATA); |
| 136 | + |
| 137 | + // select the cleanup test |
| 138 | + const { |
| 139 | + testName, |
| 140 | + prefix |
| 141 | + } = data; |
| 142 | + |
| 143 | + const testCase = cleanupTests[testName]; |
| 144 | + |
| 145 | + if (!testCase) |
| 146 | + throw new Error(`Test ${testName} not found`); |
| 147 | + |
| 148 | + testCase.fn(prefix).catch((e) => { |
| 149 | + console.error(e); |
| 150 | + process.exit(1); |
| 151 | + }); |
| 152 | + |
| 153 | + return; |
| 154 | +} |
| 155 | + |
| 156 | +// Main Thread runs the tests. |
| 157 | +describe.skip('Tree env cleanup', function() { |
| 158 | + let prefix; |
| 159 | + |
| 160 | + beforeEach(() => { |
| 161 | + prefix = testdir('cleanup-tree'); |
| 162 | + }); |
| 163 | + |
| 164 | + afterEach(async () => { |
| 165 | + await fs.rimraf(prefix); |
| 166 | + }); |
| 167 | + |
| 168 | + for (const [id, testDetails] of Object.entries(cleanupTests)) { |
| 169 | + it(testDetails.name, async () => { |
| 170 | + if (testDetails.timeout != null) |
| 171 | + this.timeout(testDetails.timeout); |
| 172 | + |
| 173 | + await runForked(__filename, { |
| 174 | + testName: id, |
| 175 | + prefix: prefix |
| 176 | + }, testDetails.timeout); |
| 177 | + }); |
| 178 | + } |
| 179 | +}); |
0 commit comments