Skip to content

Commit

Permalink
More testing of opening envs in multiple threads, #164
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed May 12, 2022
1 parent c58b269 commit c8cfe18
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 59 deletions.
12 changes: 11 additions & 1 deletion test/index.test.js
Expand Up @@ -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([])));
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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))]);
Expand Down
102 changes: 44 additions & 58 deletions test/readonly-threads.cjs
Expand Up @@ -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);
Expand All @@ -76,9 +61,10 @@ if (isMainThread) {
}, 1);
setTimeout(() => {
clearInterval(interval)
db.close();
parentPort.postMessage("");
}, 100);
}
});
}, 10);
}
});
}

0 comments on commit c8cfe18

Please sign in to comment.