Skip to content

Commit

Permalink
test: reorganize and rewrite shared components
Browse files Browse the repository at this point in the history
  • Loading branch information
weyoss committed Feb 16, 2024
1 parent 66d59e9 commit e98dc77
Show file tree
Hide file tree
Showing 36 changed files with 1,384 additions and 283 deletions.
89 changes: 89 additions & 0 deletions tests/event/test00001.test.ts
@@ -0,0 +1,89 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

import { promisify, promisifyAll } from 'bluebird';
import { EventBusConnectionError } from '../../src/event/errors';
import { EventBus } from '../../src/event';

type TEvent = {
e1: (arg: string) => void;
'eventBus.disconnect': () => void;
};

test('EventBus: case 1', async () => {
const getInstanceAsync = await promisify(EventBus.getInstance);
const eventBusAsync0 = promisifyAll(await getInstanceAsync<TEvent>());
const eventBusAsync = promisifyAll(await getInstanceAsync<TEvent>());
expect(eventBusAsync).toBe(eventBusAsync0);

// on
const callback = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback);
eventBusAsync.emit('e1', 'hello');
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenNthCalledWith(1, 'hello');

// once
const callback2 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.once('e1', callback2);
eventBusAsync.emit('e1', 'hello1');
eventBusAsync.emit('e1', 'hello2');
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenNthCalledWith(1, 'hello1');

// removeListener
const callback3 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback3);
eventBusAsync.emit('e1', 'hello3');
expect(callback3).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenNthCalledWith(1, 'hello3');
eventBusAsync.removeListener('e1', callback3);
eventBusAsync.emit('e1', 'hello4');
expect(callback3).toHaveBeenCalledTimes(1);

// removeAllListeners of an event
const callback4 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback4);
eventBusAsync.emit('e1', 'hello5');
expect(callback4).toHaveBeenCalledTimes(1);
expect(callback4).toHaveBeenNthCalledWith(1, 'hello5');
eventBusAsync.removeAllListeners('e1');
eventBusAsync.emit('e1', 'hello6');
expect(callback4).toHaveBeenCalledTimes(1);

// removeAllListeners
const callback5 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback5);
eventBusAsync.emit('e1', 'hello6');
expect(callback5).toHaveBeenCalledTimes(1);
expect(callback5).toHaveBeenNthCalledWith(1, 'hello6');
eventBusAsync.removeAllListeners();
eventBusAsync.emit('e1', 'hello7');
expect(callback5).toHaveBeenCalledTimes(1);

await eventBusAsync.disconnectAsync();
expect(() => eventBusAsync.on('e1', () => void 0)).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.once('e1', () => void 0)).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.removeListener('e1', () => void 0)).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.removeAllListeners('e1')).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.removeAllListeners()).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.emit('e1', 'hello8')).toThrow(
EventBusConnectionError,
);
});
105 changes: 105 additions & 0 deletions tests/event/test00002.test.ts
@@ -0,0 +1,105 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

import { delay, promisify, promisifyAll } from 'bluebird';
import { EventBusConnectionError } from '../../src/event/errors';
import { redisConfig } from '../config';
import { EventBusRedis } from '../../src/event';

type TEvent = {
e1: (arg: string) => void;
};

test('EventBusRedis: case 1', async () => {
const getInstanceAsync = await promisify(EventBusRedis.getInstance);
const eventBusAsync0 = promisifyAll(
await getInstanceAsync<TEvent>(redisConfig),
);
const eventBusAsync = promisifyAll(
await getInstanceAsync<TEvent>(redisConfig),
);
expect(eventBusAsync).toBe(eventBusAsync0);

// on
const callback = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback);
eventBusAsync.emit('e1', 'hello');
await delay(2000);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenNthCalledWith(1, 'hello');

// once
const callback2 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.once('e1', callback2);
eventBusAsync.emit('e1', 'hello1');
eventBusAsync.emit('e1', 'hello2');
await delay(2000);
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenNthCalledWith(1, 'hello1');

// removeListener
const callback3 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback3);
eventBusAsync.emit('e1', 'hello3');
await delay(2000);
expect(callback3).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenNthCalledWith(1, 'hello3');
eventBusAsync.removeListener('e1', callback3);
eventBusAsync.emit('e1', 'hello4');
await delay(2000);
expect(callback3).toHaveBeenCalledTimes(1);

// removeAllListeners of an event
const callback4 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback4);
eventBusAsync.emit('e1', 'hello5');
await delay(2000);
expect(callback4).toHaveBeenCalledTimes(1);
expect(callback4).toHaveBeenNthCalledWith(1, 'hello5');
eventBusAsync.removeAllListeners('e1');
eventBusAsync.emit('e1', 'hello6');
await delay(2000);
expect(callback4).toHaveBeenCalledTimes(1);

// removeAllListeners
const callback5 = jest.fn<unknown, unknown[], unknown>();
eventBusAsync.on('e1', callback5);
eventBusAsync.emit('e1', 'hello6');
await delay(2000);
expect(callback5).toHaveBeenCalledTimes(1);
expect(callback5).toHaveBeenNthCalledWith(1, 'hello6');
eventBusAsync.removeAllListeners();
await delay(2000);
eventBusAsync.emit('e1', 'hello7');
expect(callback5).toHaveBeenCalledTimes(1);

await eventBusAsync.disconnectAsync();

// second time
await eventBusAsync.disconnectAsync();

expect(() => eventBusAsync.on('e1', () => void 0)).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.once('e1', () => void 0)).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.removeListener('e1', () => void 0)).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.removeAllListeners('e1')).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.removeAllListeners()).toThrow(
EventBusConnectionError,
);
expect(() => eventBusAsync.emit('e1', 'hello8')).toThrow(
EventBusConnectionError,
);
});
63 changes: 63 additions & 0 deletions tests/event/test00003.test.ts
@@ -0,0 +1,63 @@
/*
* Copyright (c)
* Weyoss <weyoss@protonmail.com>
* https://github.com/weyoss
*
* This source code is licensed under the MIT license found in the LICENSE file
* in the root directory of this source tree.
*/

import { EventEmitter } from '../../src/event';

type TEvent = {
e1: (arg: string) => void;
};

test('EventEmitter', async () => {
const eventEmitter = new EventEmitter<TEvent>();

// on
const callback = jest.fn<unknown, unknown[], unknown>();
eventEmitter.on('e1', callback);
eventEmitter.emit('e1', 'hello');
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenNthCalledWith(1, 'hello');

// once
const callback2 = jest.fn<unknown, unknown[], unknown>();
eventEmitter.once('e1', callback2);
eventEmitter.emit('e1', 'hello1');
eventEmitter.emit('e1', 'hello2');
expect(callback2).toHaveBeenCalledTimes(1);
expect(callback2).toHaveBeenNthCalledWith(1, 'hello1');

// removeListener
const callback3 = jest.fn<unknown, unknown[], unknown>();
eventEmitter.on('e1', callback3);
eventEmitter.emit('e1', 'hello3');
expect(callback3).toHaveBeenCalledTimes(1);
expect(callback3).toHaveBeenNthCalledWith(1, 'hello3');
eventEmitter.removeListener('e1', callback3);
eventEmitter.emit('e1', 'hello4');
expect(callback3).toHaveBeenCalledTimes(1);

// removeAllListeners of an event
const callback4 = jest.fn<unknown, unknown[], unknown>();
eventEmitter.on('e1', callback4);
eventEmitter.emit('e1', 'hello5');
expect(callback4).toHaveBeenCalledTimes(1);
expect(callback4).toHaveBeenNthCalledWith(1, 'hello5');
eventEmitter.removeAllListeners('e1');
eventEmitter.emit('e1', 'hello6');
expect(callback4).toHaveBeenCalledTimes(1);

// removeAllListeners
const callback5 = jest.fn<unknown, unknown[], unknown>();
eventEmitter.on('e1', callback5);
eventEmitter.emit('e1', 'hello6');
expect(callback5).toHaveBeenCalledTimes(1);
expect(callback5).toHaveBeenNthCalledWith(1, 'hello6');
eventEmitter.removeAllListeners();
eventEmitter.emit('e1', 'hello7');
expect(callback5).toHaveBeenCalledTimes(1);
});
1 change: 1 addition & 0 deletions tests/jest.setup.ts
Expand Up @@ -17,6 +17,7 @@ afterAll(noop);

beforeEach(async () => {
await startUp();
jest.resetModules();
});

afterEach(async () => {
Expand Down
45 changes: 0 additions & 45 deletions tests/lock/test00001.test.ts

This file was deleted.

30 changes: 0 additions & 30 deletions tests/lock/test00002.test.ts

This file was deleted.

30 changes: 0 additions & 30 deletions tests/lock/test00004.test.ts

This file was deleted.

0 comments on commit e98dc77

Please sign in to comment.