UNPKG

electron-rpc-async-queue

Version:

Async queue implementation for Electron RPC

47 lines (46 loc) 2.02 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const assert = __importStar(require("assert")); const AsyncQueue_1 = require("./AsyncQueue"); const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); const TASK_DELAY = 500; const makeSyncTask = (index, executed) => () => { executed.push(`#${index}`); }; const makeAsyncTask = (index, executed) => async () => { await sleep(TASK_DELAY); executed.push(`#${index}`); }; const makeTestCase = (isFirstAsync, isSecondAsync, isThirdAsync) => { const firstTaskType = isFirstAsync ? 'Async' : 'Sync'; const secondTaskType = isSecondAsync ? 'async' : 'sync'; const thirdTaskType = isThirdAsync ? 'async' : 'sync'; it(`${firstTaskType} task #1, ${secondTaskType} task #2 and ${thirdTaskType} task #3 runs in turn`, async () => { const queue = new AsyncQueue_1.AsyncQueue(); const executed = []; const task1 = isFirstAsync ? makeAsyncTask(1, executed) : makeSyncTask(1, executed); const task2 = isSecondAsync ? makeAsyncTask(2, executed) : makeSyncTask(2, executed); const task3 = isThirdAsync ? makeAsyncTask(3, executed) : makeSyncTask(3, executed); queue.push(task1); queue.push(task2); await queue.push(task3); const result = executed.join(); const expected = '#1,#2,#3'; assert.equal(result, expected, `Executed list must equal "${expected}"`); }); }; describe('"TaskQueue" class unit tests', () => { makeTestCase(false, false, false); makeTestCase(true, false, false); makeTestCase(true, true, false); makeTestCase(true, true, true); makeTestCase(false, true, true); makeTestCase(true, false, true); });