From a0c3e1022bb5c51640d69028238362d61dd562b9 Mon Sep 17 00:00:00 2001 From: tshemsedinov Date: Wed, 27 Mar 2019 13:24:20 +0200 Subject: [PATCH] Pass filename and options from Thread to Worker Refs: https://github.com/metarhia/metasync/issues/416 --- lib/locks.js | 4 ++-- test/locks.js | 49 +++++++++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/locks.js b/lib/locks.js index cb5c4400..9ead2eb7 100644 --- a/lib/locks.js +++ b/lib/locks.js @@ -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 => { diff --git a/test/locks.js b/test/locks.js index 4fd1491d..87209a6e 100644 --- a/test/locks.js +++ b/test/locks.js @@ -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); + }); + } }