Files
pulse/tests/worker.test.js
T

90 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict';
// Unit tests for the worker's command execution (worker/worker.js), with no
// server: results are captured from sendResult instead of a WebSocket.
const { PulseWorker } = require('../worker/worker');
function makeWorker(opts = {}) {
const w = new PulseWorker();
w.maxConcurrentTasks = opts.max || 5;
w.results = [];
w.sendResult = r => w.results.push(r);
return w;
}
const run = (w, command, extra = {}) =>
w.executeCommand({ command, execution_id: 'ex-1', command_id: 'cmd-1', ...extra });
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
});
afterAll(() => jest.restoreAllMocks());
test('a successful command reports stdout, stderr and duration', async () => {
const w = makeWorker();
await run(w, 'echo out; echo err >&2');
expect(w.results).toHaveLength(1);
const r = w.results[0];
expect(r).toMatchObject({
type: 'command_result', execution_id: 'ex-1', command_id: 'cmd-1',
worker_id: w.workerId, success: true, stdout: 'out\n', stderr: 'err\n',
});
expect(typeof r.duration).toBe('number');
expect(w.activeTasks).toBe(0);
});
test('a failing command reports its exit code and output (#14)', async () => {
const w = makeWorker();
await run(w, 'echo partial; echo broke >&2; exit 7');
const r = w.results[0];
expect(r).toMatchObject({ success: false, exit_code: 7, stdout: 'partial\n', stderr: 'broke\n' });
expect(r.timed_out).toBeUndefined();
expect(r.error).toMatch(/Command failed/);
expect(w.activeTasks).toBe(0);
});
test('a command that runs past its timeout is killed and reported as timed out', async () => {
const w = makeWorker();
const started = Date.now();
await run(w, 'sleep 5', { timeout: 300 });
expect(Date.now() - started).toBeLessThan(3000);
expect(w.results[0]).toMatchObject({ success: false, timed_out: true, signal: 'SIGTERM' });
expect(w.activeTasks).toBe(0);
});
test('at capacity, extra commands get an immediate failure instead of silence (#7)', async () => {
const w = makeWorker({ max: 1 });
const first = run(w, 'sleep 0.5; echo first', { execution_id: 'ex-a', command_id: 'a' });
await run(w, 'echo second', { execution_id: 'ex-b', command_id: 'b' });
const rejected = w.results.find(r => r.command_id === 'b');
expect(rejected).toMatchObject({ success: false, execution_id: 'ex-b' });
expect(rejected.error).toMatch(/capacity/);
await first;
expect(w.results.find(r => r.command_id === 'a')).toMatchObject({ success: true, stdout: 'first\n' });
expect(w.activeTasks).toBe(0);
});
test('a slot frees up again after a command fails', async () => {
const w = makeWorker({ max: 1 });
await run(w, 'exit 1');
await run(w, 'echo ok', { command_id: 'next' });
expect(w.results.find(r => r.command_id === 'next')).toMatchObject({ success: true });
});
test('ping is answered with pong over the socket', async () => {
const w = makeWorker();
const sent = [];
w.ws = { readyState: 1, send: m => sent.push(JSON.parse(m)) }; // 1 = WebSocket.OPEN
await w.handleMessage({ type: 'ping' });
expect(sent).toEqual([{ type: 'pong', worker_id: w.workerId }]);
});
test('results are not sent while disconnected', () => {
const w = new PulseWorker();
const send = jest.fn();
w.ws = { readyState: 3, send }; // 3 = CLOSED
w.sendResult({ type: 'command_result' });
expect(send).not.toHaveBeenCalled();
});