From c8cfe185b061a0c5def02584183aa7736fcfb09e Mon Sep 17 00:00:00 2001 From: Kris Zyp Date: Thu, 12 May 2022 09:58:46 -0600 Subject: [PATCH] More testing of opening envs in multiple threads, #164 --- test/index.test.js | 12 ++++- test/readonly-threads.cjs | 102 ++++++++++++++++---------------------- 2 files changed, 55 insertions(+), 59 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index f2c8c1e47..72e0d9c20 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -95,6 +95,13 @@ describe('lmdb-js', function() { }) return } + it('will not open non-existent db with create disabled', function() { + let noDb = db.open({ + name: 'not-there', + create: false, + }); + should.equal(noDb, undefined); + }); it('zero length values', async function() { await db.committed // should be able to await db even if nothing has happened db.put(5, asBinary(Buffer.from([]))); @@ -983,6 +990,9 @@ describe('lmdb-js', function() { compression: true, overlappingSync: true, }); + let db2 = db.openDB({ + name: 'child' + }) if (i > 0) { let v = db.get('key') v = db.get('key1') @@ -1134,7 +1144,7 @@ describe('lmdb-js', function() { }); }); }); - describe('Read-only Threads', function() { + describe.only('Read-only Threads', function() { this.timeout(1000000); it('will run a group of threads with read-only transactions', function(done) { var child = spawn('node', [fileURLToPath(new URL('./readonly-threads.cjs', import.meta.url))]); diff --git a/test/readonly-threads.cjs b/test/readonly-threads.cjs index ad9abb3d7..31f971539 100644 --- a/test/readonly-threads.cjs +++ b/test/readonly-threads.cjs @@ -4,69 +4,54 @@ var path = require('path'); var numCPUs = require('os').cpus().length; const { open } = require('../dist/index.cjs'); -const MAX_DB_SIZE = 256 * 1024 * 1024; if (isMainThread) { - var inspector = require('inspector') -// inspector.open(9331, null, true);debugger + var inspector = require('inspector') +// inspector.open(9331, null, true);debugger - // The main thread + // The main thread - let db = open({ - path: path.resolve(__dirname, './testdata'), - maxDbs: 10, - mapSize: MAX_DB_SIZE, - maxReaders: 126, - overlappingSync: true, - }); + var workerCount = Math.min(numCPUs * 2, 20); + console.log({workerCount}) + var value = {test: '48656c6c6f2c20776f726c6421'}; - var workerCount = Math.min(numCPUs * 2, 20); - var value = {test: '48656c6c6f2c20776f726c6421'}; + // This will start as many workers as there are CPUs available. + let messages = []; + let iterations = 1000; + function startWorker() { + if (iterations-- <= 0) + return; + var worker = new Worker(__filename); + worker.on('message', function(msg) { + messages.push(msg); + // Once every worker has replied with a response for the value + // we can exit the test. - // This will start as many workers as there are CPUs available. - var workers = []; - for (var i = 0; i < workerCount; i++) { - var worker = new Worker(__filename); - workers.push(worker); - } - - var messages = []; - workers.forEach(function(worker) { - worker.on('message', function(msg) { - messages.push(msg); - // Once every worker has replied with a response for the value - // we can exit the test. - - setTimeout(() => { - worker.terminate() - }, 20); - if (messages.length === workerCount) { - console.log("done", threadId) - //setTimeout(() => - //process.exit(0), 200); - } - }); - }); - - for (var i = 0; i < workers.length; i++) { - var worker = workers[i]; + setTimeout(() => { + worker.terminate(); + startWorker(); + }, 20); + if (messages.length === iterations) { + console.log("done", threadId) + } + }); worker.postMessage({key: 'key' + i}); - }; - + } + for (var i = 0; i < workerCount; i++) { + startWorker(); + } } else { - // The worker process - let db = open({ - path: path.resolve(__dirname, './testdata'), - maxDbs: 10, - mapSize: MAX_DB_SIZE, - maxReaders: 126, - overlappingSync: true, - }); - + // The worker process + let db = open({ + path: path.resolve(__dirname, './testdata/' + Math.round(Math.random() * 10) + '.mdb'), + maxDbs: 10, + maxReaders: 126, + overlappingSync: true, + }); - parentPort.on('message', async function(msg) { - if (msg.key) { - var value = db.get(msg.key); + parentPort.on('message', async function(msg) { + if (msg.key) { + var value = db.get(msg.key); let lastIterate = db.getRange().iterate() let interval = setInterval(() => { db.get(msg.key); @@ -76,9 +61,10 @@ if (isMainThread) { }, 1); setTimeout(() => { clearInterval(interval) + db.close(); parentPort.postMessage(""); - }, 100); - - } - }); + }, 10); + + } + }); }