Spaces:
Running
Running
File size: 3,971 Bytes
b1cfe1b 11486ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import { ChannelCapacityQueueError, createChannelCapacityQueue } from './channel-capacity-queue';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
describe('createChannelCapacityQueue', () => {
it('queues requests per key and releases them in FIFO order', async () => {
let currentTime = 1000;
const queue = createChannelCapacityQueue({
enabled: true,
capacityPerKey: 1,
maxWaitMs: 1000,
maxSize: 10,
now: () => currentTime
});
const first = await queue.acquire('credential-1');
const secondPromise = queue.acquire('credential-1');
const thirdPromise = queue.acquire('credential-1');
assert.deepEqual(queue.summary().keys, [{ key: 'credential-1', active: 1, queued: 2 }]);
currentTime = 1250;
first.release();
const second = await secondPromise;
assert.equal(second.queued, true);
assert.equal(second.waitMs, 250);
currentTime = 1400;
second.release();
const third = await thirdPromise;
assert.equal(third.queued, true);
assert.equal(third.waitMs, 400);
third.release();
assert.equal(queue.summary().active, 0);
assert.equal(queue.summary().queued, 0);
});
it('does not block different keys', async () => {
const queue = createChannelCapacityQueue({
enabled: true,
capacityPerKey: 1,
maxWaitMs: 1000,
maxSize: 10
});
const first = await queue.acquire('credential-1');
const second = await queue.acquire('credential-2');
assert.equal(first.queued, false);
assert.equal(second.queued, false);
assert.equal(queue.summary().active, 2);
first.release();
second.release();
});
it('rejects when the per-key queue is full', async () => {
const queue = createChannelCapacityQueue({
enabled: true,
capacityPerKey: 1,
maxWaitMs: 1000,
maxSize: 1
});
const first = await queue.acquire('credential-1');
const secondPromise = queue.acquire('credential-1');
await assert.rejects(
queue.acquire('credential-1'),
(error) => error instanceof ChannelCapacityQueueError && error.code === 'channel_capacity_queue_full'
);
first.release();
const second = await secondPromise;
second.release();
});
it('times out queued requests and removes them from the queue', async () => {
const queue = createChannelCapacityQueue({
enabled: true,
capacityPerKey: 1,
maxWaitMs: 5,
maxSize: 10
});
const first = await queue.acquire('credential-1');
await assert.rejects(
queue.acquire('credential-1'),
(error) => error instanceof ChannelCapacityQueueError && error.code === 'channel_capacity_queue_timeout'
);
assert.equal(queue.summary().queued, 0);
first.release();
});
it('aborts queued requests and keeps release idempotent', async () => {
const queue = createChannelCapacityQueue({
enabled: true,
capacityPerKey: 1,
maxWaitMs: 1000,
maxSize: 10
});
const abortController = new AbortController();
const first = await queue.acquire('credential-1');
const secondPromise = queue.acquire('credential-1', { signal: abortController.signal });
abortController.abort();
await assert.rejects(
secondPromise,
(error) => error instanceof ChannelCapacityQueueError && error.code === 'channel_capacity_queue_aborted'
);
first.release();
first.release();
assert.equal(queue.summary().active, 0);
assert.equal(queue.summary().queued, 0);
});
});
|