4 const assert
= require('assert');
5 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
7 const Worker
= require('../../src/worker');
8 const Config
= require('../../config');
10 const stubLogger
= require('../stub-logger');
12 const noExpectedException
= 'did not get expected exception';
14 describe('Worker', function () {
19 beforeEach(function () {
20 config
= new Config('test');
21 promiseGiver
= sinon
.stub();
22 worker
= new Worker(stubLogger
, promiseGiver
, config
);
25 afterEach(function () {
29 describe('constructor', function () {
30 it('instantiates', function () {
34 it('requires a promiseGiver function', function () {
36 worker
= new Worker(stubLogger
, undefined, config
);
37 assert
.fail('should require function argument');
39 assert(e
instanceof TypeError
);
44 describe('start', function () {
45 it('starts without polling', function () {
46 config
.worker
.pollingEnabled
= false;
47 worker
= new Worker(stubLogger
, promiseGiver
, config
);
49 assert
.strictEqual(worker
.running
, false);
51 it('starts with polling', function () {
52 config
.worker
.pollingEnabled
= true;
53 worker
= new Worker(stubLogger
, promiseGiver
, config
);
54 sinon
.stub(worker
, '_recurr');
56 clearTimeout(worker
.nextTimeout
);
57 assert
.strictEqual(worker
.running
, true);
61 describe('stop', function () {
62 it('stops', function () {
63 worker
= new Worker(stubLogger
, promiseGiver
, config
);
66 assert
.strictEqual(worker
.running
, false);
67 assert
.strictEqual(worker
.nextTimeout
, undefined);
71 describe('watchedPromise', function () {
73 it('watches a resolvable promise', async
function () {
75 promise
= Promise
.resolve(res
);
76 const watched
= Worker
.watchedPromise(promise
);
77 const result
= await watched
;
78 assert
.strictEqual(result
, res
);
79 assert
.strictEqual(watched
.resolved
, res
);
80 assert(watched
.isSettled
);
82 it('watches a rejectable promise', async
function () {
83 const rej
= new Error('boo');
84 promise
= Promise
.reject(rej
);
85 const watched
= Worker
.watchedPromise(promise
);
88 assert
.fail(noExpectedException
);
90 assert
.deepStrictEqual(e
, rej
);
91 assert
.deepStrictEqual(watched
.rejected
, rej
);
92 assert(watched
.isSettled
);
95 it('covers wrapped promise', async
function () {
97 promise
= Promise
.resolve(res
);
98 const watched
= Worker
.watchedPromise(promise
);
99 const rewatched
= Worker
.watchedPromise(watched
);
100 const result
= await rewatched
;
101 assert
.strictEqual(result
, res
);
102 assert
.strictEqual(rewatched
.resolved
, res
);
103 assert(rewatched
.isSettled
);
105 }); // watchedPromise
107 describe('_handleWatchedList', function () {
109 beforeEach(function () {
110 handler
= sinon
.stub();
112 it('handled resolveds', function () {
114 { isSettled: false, resolved: undefined, rejected: undefined },
115 { isSettled: true, resolved: 'value', rejected: undefined },
116 { isSettled: true, resolved: undefined, rejected: 'error' },
117 { isSettled: false, resolved: undefined, rejected: undefined },
119 const result
= worker
._handleWatchedList(handler
);
120 assert
.strictEqual(result
, 2);
121 assert
.strictEqual(worker
.inFlight
.length
, 2);
122 assert
.strictEqual(handler
.callCount
, 2);
124 }); // _handleWatchedList
126 describe('_getWork', function () {
127 it('gets tasks', async
function () {
129 Promise
.resolve('first'),
130 Promise
.reject('bad'),
131 Promise
.resolve('second'),
133 worker
.promiseGiver
.resolves(expected
);
134 const result
= await worker
._getWork();
135 assert
.deepStrictEqual(result
, expected
);
136 assert
.strictEqual(worker
.inFlight
.length
, expected
.length
);
138 it('covers none wanted', async
function () {
139 worker
.concurrency
= 3;
141 Promise
.resolve('first'),
142 Promise
.reject('bad'),
143 Promise
.resolve('second'),
145 const result
= await worker
._getWork();
146 assert(!worker
.promiseGiver
.called
);
147 assert
.deepStrictEqual(result
, []);
151 describe('_watchedHandler', function () {
152 it('covers resolved', function () {
153 worker
._watchedHandler('resolved', undefined);
155 it('covers rejected', function () {
156 worker
._watchedHandler(undefined, 'rejected');
158 }); // _watchedHandler
160 describe('_recurr', function () {
161 it('covers', function (done
) {
162 worker
.recurrSleepMs
= 10;
163 this.slow(worker
.recurrSleepMs
* 3);
164 sinon
.stub(worker
, 'process').callsFake(done
);
165 worker
.running
= true;
168 it('covers not running', function () {
169 worker
.running
= false;
174 describe('process', function () {
175 beforeEach(function () {
176 sinon
.stub(worker
, '_getWork');
177 sinon
.stub(worker
, '_recurr');
179 it('covers', async
function () {
181 Worker
.watchedPromise(Promise
.resolve('one')),
182 Worker
.watchedPromise(Promise
.reject('foo')),
184 await worker
.process();
185 assert
.strictEqual(worker
._getWork
.callCount
, 2);
186 assert
.strictEqual(worker
._recurr
.callCount
, 1);
188 it('covers no work', async
function () {
189 await worker
.process();
190 assert
.strictEqual(worker
._getWork
.callCount
, 1);
191 assert
.strictEqual(worker
._recurr
.callCount
, 1);