Skip to content

Commit a0c3e10

Browse files
committed
Pass filename and options from Thread to Worker
Refs: #416
1 parent d30af90 commit a0c3e10

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

lib/locks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ if (!isMainThread) {
8181
}
8282

8383
class Thread {
84-
constructor() {
85-
const worker = new Worker(__filename);
84+
constructor(filename, options) {
85+
const worker = new Worker(filename, options);
8686
this.worker = worker;
8787
threads.add(this);
8888
worker.on('message', message => {

test/locks.js

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
'use strict';
22

3-
const { isMainThread } = require('worker_threads');
4-
const { locks } = require('..');
5-
const { Thread } = locks;
6-
const metatests = require('metatests');
3+
const common = require('@metarhia/common');
4+
const nodeVerion = common.between(process.version, 'v', '.');
75

8-
const sleep = msec =>
9-
new Promise(resolve => {
10-
setTimeout(resolve, msec);
11-
});
6+
if (nodeVerion >= 11) {
7+
const { isMainThread } = require('worker_threads');
8+
const { locks } = require('..');
9+
const { Thread } = locks;
10+
const metatests = require('metatests');
1211

13-
if (isMainThread) {
14-
metatests.test('locks: enter and leave', test => {
15-
new Thread();
16-
new Thread();
12+
const sleep = msec =>
13+
new Promise(resolve => {
14+
setTimeout(resolve, msec);
15+
});
1716

18-
setTimeout(() => {
19-
locks.request('A', async lock => {
20-
test.end();
21-
});
22-
}, 100);
23-
});
24-
} else {
25-
locks.request('A', async lock => {
26-
await sleep(100);
27-
});
17+
if (isMainThread) {
18+
metatests.test('locks: enter and leave', test => {
19+
new Thread(__filename);
20+
new Thread(__filename);
21+
22+
setTimeout(() => {
23+
locks.request('A', async lock => {
24+
test.end();
25+
});
26+
}, 100);
27+
});
28+
} else {
29+
locks.request('A', async lock => {
30+
await sleep(100);
31+
});
32+
}
2833
}

0 commit comments

Comments
 (0)