Skip to content

Commit

Permalink
Pass filename and options from Thread to Worker
Browse files Browse the repository at this point in the history
Refs: #416
  • Loading branch information
tshemsedinov committed Mar 28, 2019
1 parent c196045 commit 161590c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ if (!isMainThread) {
}

class Thread {
constructor() {
const worker = new Worker(__filename);
constructor(filename, options) {
const worker = new Worker(filename, options);
this.worker = worker;
threads.add(this);
worker.on('message', message => {
Expand Down
49 changes: 27 additions & 22 deletions test/locks.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
'use strict';

const { isMainThread } = require('worker_threads');
const { locks } = require('..');
const { Thread } = locks;
const metatests = require('metatests');
const common = require('@metarhia/common');
const nodeVerion = common.between(process.version, 'v', '.');

const sleep = msec =>
new Promise(resolve => {
setTimeout(resolve, msec);
});
if (nodeVerion >= 11) {
const { isMainThread } = require('worker_threads');
const { locks } = require('..');
const { Thread } = locks;
const metatests = require('metatests');

if (isMainThread) {
metatests.test('locks: enter and leave', test => {
new Thread();
new Thread();
const sleep = msec =>
new Promise(resolve => {
setTimeout(resolve, msec);
});

setTimeout(() => {
locks.request('A', async lock => {
test.end();
});
}, 100);
});
} else {
locks.request('A', async lock => {
await sleep(100);
});
if (isMainThread) {
metatests.test('locks: enter and leave', test => {
new Thread(__filename);
new Thread(__filename);

setTimeout(() => {
locks.request('A', async lock => {
test.end();
});
}, 100);
});
} else {
locks.request('A', async lock => {
await sleep(100);
});
}
}

0 comments on commit 161590c

Please sign in to comment.