forked from WiseLibs/better-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
44.worker-threads.js
66 lines (65 loc) · 2.46 KB
/
44.worker-threads.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
if (parseInt(process.versions.node) >= 12) {
const threads = require('worker_threads');
const Database = require('../.');
if (threads.isMainThread) {
describe('Worker Threads', function () {
afterEach(function () {
if (this.db) this.db.close();
return this.cleanup;
});
it('are properly supported', function () {
this.slow(1000);
return new Promise((resolve, reject) => {
const db = this.db = Database(util.next()).defaultSafeIntegers();
expect(db.prepare('select 555').constructor.foo).to.be.undefined;
db.prepare('select 555').constructor.foo = 5;
expect(db.prepare('select 555').constructor.foo).to.equal(5);
const worker = new threads.Worker(__filename);
worker.on('exit', code => reject(new Error(`worker exited with code ${code}`)));
worker.on('error', reject);
worker.on('message', ({ msg, info, data }) => {
try {
if (msg === 'hello') {
db.exec('create table data (a, b)');
worker.postMessage({ msg: 'hello', filename: util.current() });
} else if (msg === 'success') {
const checkedData = db.prepare("select * from data").all();
expect(info.changes).to.equal(checkedData.length);
expect(data).to.not.equal(checkedData);
expect(data).to.deep.equal(checkedData);
expect(db.prepare('select 555').constructor.foo).to.equal(5);
resolve();
this.cleanup = worker.terminate();
} else {
throw new Error('unexpected message from worker');
}
} catch (err) {
reject(err);
this.cleanup = worker.terminate();
}
});
});
});
});
} else {
const { expect } = require('chai');
threads.parentPort.on('message', ({ msg, filename }) => {
if (msg === 'hello') {
const db = Database(filename).defaultSafeIntegers();
expect(db.prepare('select 555').constructor.foo).to.be.undefined;
db.prepare('select 555').constructor.foo = 27;
expect(db.prepare('select 555').constructor.foo).to.equal(27);
const info = db.prepare("insert into data values (1, 2), ('foo', 5.5)").run();
const data = db.prepare("select * from data").all();
expect(info.changes).to.be.a('number');
expect(info.lastInsertRowid).to.be.a('bigint');
expect(data.length).to.equal(2);
threads.parentPort.postMessage({ msg: 'success', info, data });
} else {
throw new Error('unexpected message from main thread');
}
});
threads.parentPort.postMessage({ msg: 'hello' });
}
}