78 lines
2.4 KiB
JavaScript
78 lines
2.4 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert');
|
|
const { createPauseGate } = require('../src/features/pauseGate.js');
|
|
|
|
const tick = () => new Promise((r) => setTimeout(r, 20));
|
|
|
|
test('Ohne Pause läuft wait() sofort durch', async () => {
|
|
const gate = createPauseGate();
|
|
await gate.wait();
|
|
await gate.wait(new AbortController().signal);
|
|
assert.strictEqual(gate.isPaused(), false);
|
|
});
|
|
|
|
test('Während der Pause bleibt wait() stehen und läuft nach resume() weiter', async () => {
|
|
const gate = createPauseGate();
|
|
gate.pause();
|
|
let done = false;
|
|
const p = gate.wait().then(() => { done = true; });
|
|
await tick();
|
|
assert.strictEqual(done, false);
|
|
assert.strictEqual(gate.waitingCount(), 1);
|
|
gate.resume();
|
|
await p;
|
|
assert.strictEqual(done, true);
|
|
assert.strictEqual(gate.waitingCount(), 0);
|
|
});
|
|
|
|
test('resume() gibt alle Wartenden frei', async () => {
|
|
const gate = createPauseGate();
|
|
gate.pause();
|
|
const all = Promise.all([gate.wait(), gate.wait(), gate.wait()]);
|
|
await tick();
|
|
assert.strictEqual(gate.waitingCount(), 3);
|
|
gate.resume();
|
|
await all;
|
|
});
|
|
|
|
test('Abbruch während der Pause löst wait() mit dem Abbruchfehler aus', async () => {
|
|
const gate = createPauseGate();
|
|
gate.pause();
|
|
const c = new AbortController();
|
|
const p = gate.wait(c.signal);
|
|
await tick();
|
|
c.abort();
|
|
await assert.rejects(p, (e) => e.code === 'UPLOAD_CANCELLED');
|
|
assert.strictEqual(gate.waitingCount(), 0);
|
|
});
|
|
|
|
test('Bereits abgebrochenes Signal wirft sofort, auch ohne Pause', async () => {
|
|
const gate = createPauseGate();
|
|
const c = new AbortController();
|
|
c.abort();
|
|
await assert.rejects(gate.wait(c.signal), (e) => e.code === 'UPLOAD_CANCELLED');
|
|
});
|
|
|
|
test('onChange meldet nur echte Wechsel', () => {
|
|
const states = [];
|
|
const gate = createPauseGate({ onChange: (p) => states.push(p) });
|
|
assert.strictEqual(gate.pause(), true);
|
|
assert.strictEqual(gate.pause(), false);
|
|
assert.strictEqual(gate.resume(), true);
|
|
assert.strictEqual(gate.resume(), false);
|
|
assert.deepStrictEqual(states, [true, false]);
|
|
});
|
|
|
|
test('Ein abgebrochener Wartender blockiert die übrigen nicht', async () => {
|
|
const gate = createPauseGate();
|
|
gate.pause();
|
|
const c = new AbortController();
|
|
const a = gate.wait(c.signal).catch((e) => e.code);
|
|
const b = gate.wait();
|
|
await tick();
|
|
c.abort();
|
|
assert.strictEqual(await a, 'UPLOAD_CANCELLED');
|
|
gate.resume();
|
|
await b;
|
|
});
|