X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Fsrc%2Fworker.js;h=7f99de178b884cdd852bc3a642c846936625948d;hb=8cd88ab4087a7fab2ccd6e231c64d7f0f1299f26;hp=a9047a0e0ccef3e5013a136cf437dab8c34786cc;hpb=9696c012e6b9a6c58904baa397ca0ebf78112316;p=websub-hub diff --git a/test/src/worker.js b/test/src/worker.js index a9047a0..7f99de1 100644 --- a/test/src/worker.js +++ b/test/src/worker.js @@ -1,13 +1,13 @@ -/* eslint-env mocha */ 'use strict'; -const assert = require('assert'); -const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require +const assert = require('node:assert'); +const sinon = require('sinon'); const Worker = require('../../src/worker'); const Config = require('../../config'); const stubLogger = require('../stub-logger'); +const stubDb = require('../stub-db'); const noExpectedException = 'did not get expected exception'; @@ -19,7 +19,8 @@ describe('Worker', function () { beforeEach(function () { config = new Config('test'); promiseGiver = sinon.stub(); - worker = new Worker(stubLogger, promiseGiver, config); + worker = new Worker(stubLogger, stubDb, promiseGiver, config); + stubDb._reset(); }); afterEach(function () { @@ -33,7 +34,7 @@ describe('Worker', function () { it('requires a promiseGiver function', function () { try { - worker = new Worker(stubLogger, undefined, config); + worker = new Worker(stubLogger, stubDb, undefined, config); assert.fail('should require function argument'); } catch (e) { assert(e instanceof TypeError); @@ -44,13 +45,13 @@ describe('Worker', function () { describe('start', function () { it('starts without polling', function () { config.worker.pollingEnabled = false; - worker = new Worker(stubLogger, promiseGiver, config); + worker = new Worker(stubLogger, stubDb, promiseGiver, config); worker.start(); assert.strictEqual(worker.running, false); }); it('starts with polling', function () { config.worker.pollingEnabled = true; - worker = new Worker(stubLogger, promiseGiver, config); + worker = new Worker(stubLogger, stubDb, promiseGiver, config); sinon.stub(worker, '_recurr'); worker.start(); clearTimeout(worker.nextTimeout); @@ -60,7 +61,7 @@ describe('Worker', function () { describe('stop', function () { it('stops', function () { - worker = new Worker(stubLogger, promiseGiver, config); + worker = new Worker(stubLogger, stubDb, promiseGiver, config); worker.start(); worker.stop(); assert.strictEqual(worker.running, false); @@ -124,15 +125,29 @@ describe('Worker', function () { }); // _handleWatchedList describe('_getWork', function () { + let stubCtx; + beforeEach(function () { + stubCtx = {}; + }); it('gets tasks', async function () { + /** + * In older versions, could just deepStrictEqual un-awaited promises for equality, + * but post 14 or so, async_id symbol properties are included in comparison, and + * in some executions of test suites these are somehow different in value so the test + * was failing. So now we settle everything prior to comparison. + */ const expected = [ Promise.resolve('first'), Promise.reject('bad'), Promise.resolve('second'), ]; worker.promiseGiver.resolves(expected); - const result = await worker._getWork(); - assert.deepStrictEqual(result, expected); + const result = await worker._getWork(stubCtx); + + const expectedResolved = await Promise.allSettled(expected); + const resultResolved = await Promise.allSettled(result); + assert.deepStrictEqual(resultResolved, expectedResolved); + assert.strictEqual(worker.inFlight.length, expected.length); }); it('covers none wanted', async function () { @@ -142,7 +157,7 @@ describe('Worker', function () { Promise.reject('bad'), Promise.resolve('second'), ]; - const result = await worker._getWork(); + const result = await worker._getWork(stubCtx); assert(!worker.promiseGiver.called); assert.deepStrictEqual(result, []); }); @@ -190,6 +205,26 @@ describe('Worker', function () { assert.strictEqual(worker._getWork.callCount, 1); assert.strictEqual(worker._recurr.callCount, 1); }); + it('covers error', async function () { + const expected = new Error('blah'); + stubDb.context.restore(); + sinon.stub(stubDb, 'context').rejects(expected); + await worker.process(); + assert.strictEqual(worker._getWork.callCount, 0); + assert.strictEqual(worker._recurr.callCount, 1); + }); + it('covers double invocation', async function () { + const snooze = async (ms) => new Promise((resolve) => setTimeout(resolve, ms)); + + this.slow(300); + worker.inFlight = [ + Worker.watchedPromise(snooze(100)), + ]; + + await Promise.all([worker.process(), worker.process()]); + assert.strictEqual(worker._getWork.callCount, 2); + assert.strictEqual(worker._recurr.callCount, 1); + }); }); // process }); // Worker