119 lines
3.5 KiB
TypeScript
119 lines
3.5 KiB
TypeScript
import { tap, expect } from '@push.rocks/tapbundle';
|
|
import * as lik from '../ts/index.js';
|
|
|
|
let testArray: lik.BackpressuredArray<string>;
|
|
|
|
tap.test('should create a BackpressuredArray with default high water mark', async () => {
|
|
testArray = new lik.BackpressuredArray<string>();
|
|
expect(testArray).toBeInstanceOf(lik.BackpressuredArray);
|
|
expect(testArray.length).toEqual(0);
|
|
});
|
|
|
|
tap.test('should push items and return true while under high water mark', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(4);
|
|
expect(arr.push(1)).toBeTrue();
|
|
expect(arr.push(2)).toBeTrue();
|
|
expect(arr.push(3)).toBeTrue();
|
|
expect(arr.length).toEqual(3);
|
|
});
|
|
|
|
tap.test('should return false when at high water mark', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(2);
|
|
arr.push(1);
|
|
const result = arr.push(2);
|
|
expect(result).toBeFalse();
|
|
expect(arr.length).toEqual(2);
|
|
});
|
|
|
|
tap.test('should shift items correctly', async () => {
|
|
const arr = new lik.BackpressuredArray<string>(4);
|
|
arr.push('a');
|
|
arr.push('b');
|
|
expect(arr.shift()).toEqual('a');
|
|
expect(arr.shift()).toEqual('b');
|
|
expect(arr.shift()).toBeUndefined();
|
|
});
|
|
|
|
tap.test('should peek without removing', async () => {
|
|
const arr = new lik.BackpressuredArray<string>(4);
|
|
arr.push('first');
|
|
arr.push('second');
|
|
expect(arr.peek()).toEqual('first');
|
|
expect(arr.length).toEqual(2);
|
|
});
|
|
|
|
tap.test('should peek return undefined on empty', async () => {
|
|
const arr = new lik.BackpressuredArray<string>(4);
|
|
expect(arr.peek()).toBeUndefined();
|
|
});
|
|
|
|
tap.test('should pushMany items', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(10);
|
|
const result = arr.pushMany([1, 2, 3]);
|
|
expect(arr.length).toEqual(3);
|
|
expect(result).toBeTrue();
|
|
});
|
|
|
|
tap.test('checkHasItems returns correct boolean', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(4);
|
|
expect(arr.checkHasItems()).toBeFalse();
|
|
arr.push(1);
|
|
expect(arr.checkHasItems()).toBeTrue();
|
|
});
|
|
|
|
tap.test('checkSpaceAvailable works correctly', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(2);
|
|
expect(arr.checkSpaceAvailable()).toBeTrue();
|
|
arr.push(1);
|
|
expect(arr.checkSpaceAvailable()).toBeTrue();
|
|
arr.push(2);
|
|
expect(arr.checkSpaceAvailable()).toBeFalse();
|
|
});
|
|
|
|
tap.test('waitForItems resolves when items are pushed', async () => {
|
|
const arr = new lik.BackpressuredArray<string>(4);
|
|
let resolved = false;
|
|
const waitPromise = arr.waitForItems().then(() => {
|
|
resolved = true;
|
|
});
|
|
arr.push('hello');
|
|
await waitPromise;
|
|
expect(resolved).toBeTrue();
|
|
});
|
|
|
|
tap.test('waitForSpace resolves when items are shifted', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(1);
|
|
arr.push(1);
|
|
let resolved = false;
|
|
const waitPromise = arr.waitForSpace().then(() => {
|
|
resolved = true;
|
|
});
|
|
arr.shift();
|
|
await waitPromise;
|
|
expect(resolved).toBeTrue();
|
|
});
|
|
|
|
tap.test('Symbol.iterator works', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(10);
|
|
arr.pushMany([10, 20, 30]);
|
|
const collected: number[] = [];
|
|
for (const item of arr) {
|
|
collected.push(item);
|
|
}
|
|
expect(collected).toEqual([10, 20, 30]);
|
|
});
|
|
|
|
tap.test('destroy completes subjects and unblocks waiters', async () => {
|
|
const arr = new lik.BackpressuredArray<number>(1);
|
|
arr.push(1);
|
|
let spaceResolved = false;
|
|
const waitPromise = arr.waitForSpace().then(() => {
|
|
spaceResolved = true;
|
|
});
|
|
arr.destroy();
|
|
await waitPromise;
|
|
expect(spaceResolved).toBeTrue();
|
|
});
|
|
|
|
export default tap.start();
|