Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 120x 2x 21x 6x 6x 6x 6x 69x 69x 69x 69x 6x 6x 1x 1x 1x 1x 1x 19x 26x 1x 1x 1x 10x 1x 1x 10x 1x 1x 1x 1x 9x 1x 1x 10x 1x 1x 1x 1x 1x 10x 10x 10x 1x 50x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { AsyncLimitedQueue, IAsyncLimitedQueue } from './AsyncLimitedQueue'; import { NoElementError } from './AsyncQueue'; import * as chai from 'chai'; import 'mocha'; import { TIME_STEP, wait, expectInstantly, expectTimelyIn, expectNever } from '../timing.common-spec'; import chaiAsPromised = require('chai-as-promised'); import { runCommonQueueTests } from './Queue.common-spec'; chai.use(chaiAsPromised); const expect = chai.expect; function *range(min: number, exclusiveMax: number) { Iif (!Number.isInteger(min) || !Number.isInteger(exclusiveMax)) { throw new Error('range: arguments must be whole integers'); } Iif (min > exclusiveMax) { throw new Error('range: min <= max must hold.'); } for (let i = min; i < exclusiveMax; i++) { yield i; } } async function *syncToAsyncIterable<T>(iterable: Iterable<T>): AsyncIterable<T> { for (const element of iterable) { yield element; } } async function dequeueMany<T>(queue: IAsyncLimitedQueue<T>, count: number): Promise<T[]> { Iif (!(count >= 0 && Number.isInteger(count))) { throw new Error('dequeueMany: count must be an integer >= 0.'); } Iif (count === 0) { return []; } const buffer: T[] = []; let dequeuedElements = 0; for await (const element of queue) { buffer.push(element); dequeuedElements++; if (dequeuedElements === count) { break; } } return buffer; } describe('AsyncLimitedQueue', () => { /** * Number of test entries which some unit tests insert and then dequeue * into and out of the queue. * * Must be >= 1 and <= {@link LIMIT} */ const COUNT = 10; /** * Must be >= 1. */ const LIMIT = 10; let queue: IAsyncLimitedQueue<number> = new AsyncLimitedQueue(LIMIT); beforeEach(() => { queue = new AsyncLimitedQueue(LIMIT); }); runCommonQueueTests(() => new AsyncLimitedQueue(LIMIT), COUNT); it('reject invalid limits on number of elements', async () => { const invalidLimits = [ NaN, -Infinity, -2, -1.5, -1, -0, +0, 1.5, Infinity, 1.5 ]; for (const invalidLimit of invalidLimits) { expect(() => new AsyncLimitedQueue(invalidLimit)).to.throw(); } }); it('x queues (x >= limit), x dequeues', async () => { // Non-blocking queues for (let i = 0; i < LIMIT; i++) { await queue.queue(i); } wait(TIME_STEP).then(async () => { await expectInstantly(expect( dequeueMany(queue, LIMIT + COUNT) ).to.eventually.deep.equal([...range(0, LIMIT + COUNT)])); }); // COUNT-many queues // Only the first queue has to wait for the above timeout to occur await expectTimelyIn(queue.queue(LIMIT), TIME_STEP); for (let i = 1; i < COUNT; i++) { await expectInstantly(queue.queue(i + LIMIT)); } }); it('x = limit queues, 1 offer', async () => { // Non-blocking queues for (let i = 0; i < LIMIT; i++) { await queue.queue(i); } expect(queue.offer(LIMIT + 1)).to.be.false; await expectInstantly( expect(dequeueMany(queue, LIMIT)).to.eventually.deep.equal([...range(0, LIMIT)]) ); await expectNever(queue.dequeue()); }); it('alternating polls/offers', async () => { for (let i = 0; i < LIMIT; i++) { expect(() => queue.poll()).to.throw(NoElementError); queue.offer(i); expect(queue.poll()).to.equal(i); } // Must ideally throw for an infinite amount of calls, we just // test 50 times. for (let i = 0; i < 50; i++) { expect(() => queue.poll()).to.throw(NoElementError); } }); it('offerAll: less than LIMIT elements', async () => { let numbers = range(0, LIMIT - 1); expect(queue.offerAll(numbers)).to.equal(LIMIT - 1); await expectInstantly( expect(dequeueMany(queue, LIMIT - 1)).to.eventually.deep.equal([...range(0, LIMIT - 1)]) ); expect(queue.size()).to.equal(0); }); it('offerAll: more than LIMIT elements', async () => { let numbers = range(0, LIMIT + COUNT); expect(queue.offerAll(numbers)).to.equal(LIMIT); await expectInstantly( expect(dequeueMany(queue, LIMIT)).to.eventually.deep.equal([...range(0, LIMIT)]) ); expect(queue.size()).to.equal(0); }); it('offerAllAsync: exactly LIMIT elements', async() => { let numbers = syncToAsyncIterable(range(0, LIMIT)); await expect(queue.offerAllAsync(numbers)).to.eventually.equal(LIMIT); await expectInstantly( expect(dequeueMany(queue, LIMIT)).to.eventually.deep.equal([...range(0, LIMIT)]) ); expect(queue.size()).to.equal(0); }); it('offerAllAsync: more than LIMIT elements', async () => { let numbers = syncToAsyncIterable(range(0, LIMIT + COUNT)); await expect(queue.offerAllAsync(numbers)).to.eventually.equal(LIMIT); await expectInstantly( expect(dequeueMany(queue, LIMIT)).to.eventually.deep.equal([...range(0, LIMIT)]) ); expect(queue.size()).to.equal(0); }); it('queueAll correctly resolves after queueing all elements', async () => { // No await const queueAllPromise = queue.queueAll(range(0, LIMIT)).then(() => { expect(queue.size()).to.equal(LIMIT); }); expect(queue.size()).to.equal(0); await queueAllPromise; }); }); |