Skip to content

Commit

Permalink
Make orderBookUpdated event deffered
Browse files Browse the repository at this point in the history
  • Loading branch information
maxima-net committed Aug 16, 2022
1 parent dbd9f15 commit d79b6da
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/clients/webSocket/webSocketAtomexClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AuthorizationManager } from '../../authorization/index';
import type { Transaction } from '../../blockchain/index';
import type { AtomexNetwork, CurrenciesProvider } from '../../common/index';
import { EventEmitter, type ToEventEmitter } from '../../core';
import { DeferredEventEmitter, EventEmitter, ToDeferredEventEmitter, type ToEventEmitter } from '../../core';
import type {
Order, OrderBook, Quote, ExchangeSymbol, NewOrderRequest,
OrdersSelector, CancelOrderRequest,
Expand Down Expand Up @@ -29,7 +29,7 @@ export class WebSocketAtomexClient implements AtomexClient {
swapUpdated: new EventEmitter(),
orderUpdated: new EventEmitter(),
orderBookSnapshot: new EventEmitter(),
orderBookUpdated: new EventEmitter(),
orderBookUpdated: new DeferredEventEmitter(),
topOfBookUpdated: new EventEmitter()
};

Expand Down Expand Up @@ -179,7 +179,7 @@ export class WebSocketAtomexClient implements AtomexClient {
const updatedOrderBooks = mapWebSocketOrderBookEntryDtoToOrderBooks(entryDtos, this.orderBookProvider);
for (const updatedOrderBook of updatedOrderBooks) {
this.orderBookProvider.setOrderBook(updatedOrderBook.symbol, updatedOrderBook);
(this.events.orderBookUpdated as ToEventEmitter<typeof this.events.orderBookUpdated>).emit(updatedOrderBook);
(this.events.orderBookUpdated as ToDeferredEventEmitter<string, typeof this.events.orderBookUpdated>).emitDeferred(updatedOrderBook.symbol, updatedOrderBook);
}
}
}
171 changes: 158 additions & 13 deletions tests/clients/testCases/validWsOrderBookUpdatedTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { OrderBook, } from '../../../src/exchange/index';

const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
message: string,
testValue: readonly [snapshotDtos: WebSocketOrderBookSnapshotResponseDto[], entryDtos: WebSocketOrderBookEntriesResponseDto[], orderBook: OrderBook]
testValue: readonly [snapshotDtos: WebSocketOrderBookSnapshotResponseDto[], entryDtos: WebSocketOrderBookEntriesResponseDto[], updatedOrderBooks: OrderBook[]]
]> = [
[
'Remove entry',
Expand Down Expand Up @@ -36,7 +36,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: []
}]
}],
{
[{
updateId: 57552,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
Expand All @@ -48,7 +48,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [1.5]
}
]
}
}]
]
],
[
Expand Down Expand Up @@ -91,13 +91,13 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: []
}]
}],
{
[{
updateId: 57553,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
baseCurrency: 'BTC',
entries: []
}
}]
]
],
[
Expand Down Expand Up @@ -140,7 +140,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [10]
}]
}],
{
[{
updateId: 57553,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
Expand All @@ -157,7 +157,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [10]
}
]
}
}]
]
],
[
Expand Down Expand Up @@ -189,7 +189,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [2.0]
}]
}],
{
[{
updateId: 57552,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
Expand All @@ -206,7 +206,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [1.5]
}
]
}
}]
]
],
[
Expand Down Expand Up @@ -270,7 +270,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [15]
}]
}],
{
[{
updateId: 57555,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
Expand All @@ -287,7 +287,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [15]
}
]
}
}]
]
],
[
Expand Down Expand Up @@ -340,7 +340,7 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [15]
}]
}],
{
[{
updateId: 57555,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
Expand All @@ -357,7 +357,152 @@ const validWsOrderBookUpdatedTestCases: ReadonlyArray<readonly [
qtyProfile: [15]
}
]
}
}]
]
],
[
'Update several order books',
[
[{
event: 'snapshot',
data: {
updateId: 57551,
symbol: 'ETH/BTC',
entries: [{
side: 'Buy',
price: 0.06688964,
qtyProfile: [4.0]
}, {
side: 'Sell',
price: 0.06754710,
qtyProfile: [1.5]
}]
}
},
{
event: 'snapshot',
data: {
updateId: 57552,
symbol: 'XTZ/ETH',
entries: [{
side: 'Buy',
price: 0.05555555,
qtyProfile: [5.0]
}, {
side: 'Sell',
price: 0.06666666,
qtyProfile: [2.5]
}]
}
}],
[{
event: 'entries',
data: [{
updateId: 57553,
symbol: 'ETH/BTC',
side: 'Buy',
price: 0.06688964,
qtyProfile: []
}]
},
{
event: 'entries',
data: [{
updateId: 57554,
symbol: 'ETH/BTC',
side: 'Sell',
price: 0.06754710,
qtyProfile: []
}]
},
{
event: 'entries',
data: [
{
updateId: 57555,
symbol: 'ETH/BTC',
side: 'Buy',
price: 0.06691233,
qtyProfile: [10]
}]
},
{
event: 'entries',
data: [{
updateId: 57556,
symbol: 'XTZ/ETH',
side: 'Buy',
price: 0.05555555,
qtyProfile: []
}]
},
{
event: 'entries',
data: [{
updateId: 57557,
symbol: 'XTZ/ETH',
side: 'Buy',
price: 0.04444444,
qtyProfile: [1]
}]
},
{
event: 'entries',
data: [
{
updateId: 57558,
symbol: 'ETH/BTC',
side: 'Sell',
price: 0.06754435,
qtyProfile: [15]
}]
},
{
event: 'entries',
data: [{
updateId: 57559,
symbol: 'XTZ/ETH',
side: 'Sell',
price: 0.06666666,
qtyProfile: [5.5]
}]
}],
[{
updateId: 57558,
symbol: 'ETH/BTC',
quoteCurrency: 'ETH',
baseCurrency: 'BTC',
entries: [
{
side: 'Buy',
price: new BigNumber(0.06691233),
qtyProfile: [10]
},
{
side: 'Sell',
price: new BigNumber(0.06754435),
qtyProfile: [15]
},
]
},
{
updateId: 57559,
symbol: 'XTZ/ETH',
quoteCurrency: 'XTZ',
baseCurrency: 'ETH',
entries: [
{
side: 'Sell',
price: new BigNumber(0.06666666),
qtyProfile: [5.5]
},
{
side: 'Buy',
price: new BigNumber(0.04444444),
qtyProfile: [1]
},
]
}]
]
]
];
Expand Down
12 changes: 8 additions & 4 deletions tests/clients/webSocketAtomexClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { AuthToken } from '../../src/authorization/index';
import { WebSocketAtomexClient } from '../../src/clients/index';
import type { AtomexNetwork } from '../../src/common/index';
import { InMemoryOrderBookProvider } from '../../src/exchange/index';
import { TestCurrenciesProvider, TestAuthorizationManager, TestExchangeSymbolsProvider } from '../testHelpers/index';
import { TestCurrenciesProvider, TestAuthorizationManager, TestExchangeSymbolsProvider, wait } from '../testHelpers/index';
import {
validWsOrderBookUpdatedTestCases, validWsOrderUpdatedTestCases,
validWsSwapUpdatedTestCases, validWsTopOfBookUpdatedTestCases
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('WebSocket Atomex Client', () => {
expect(onTopOfBookUpdatedCallback).toHaveBeenCalledWith(expectedQuotes);
});

test.each(validWsOrderBookUpdatedTestCases)('emits orderBookUpdated event with correct data (%s)', async (_, [snapshotDtos, entryDtos, expectedOrderBook]) => {
test.each(validWsOrderBookUpdatedTestCases)('emits orderBookUpdated event with correct data (%s)', async (_, [snapshotDtos, entryDtos, expectedOrderBooks]) => {
const onOrderUpdatedCallback = jest.fn();
const onSwapUpdatedCallback = jest.fn();
const onTopOfBookUpdatedCallback = jest.fn();
Expand All @@ -199,12 +199,16 @@ describe('WebSocket Atomex Client', () => {
snapshotDtos.forEach(snapshot => exchangeWsServer.send(snapshot));
entryDtos.forEach(entries => exchangeWsServer.send(entries));

await wait(2000);

expect(onSwapUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onTopOfBookUpdatedCallback).toHaveBeenCalledTimes(0);
expect(onOrderBookSnapshotCallback).toHaveBeenCalledTimes(snapshotDtos.length);
expect(onOrderBookUpdatedCallback).toHaveBeenCalledTimes(entryDtos.length);
expect(onOrderBookUpdatedCallback).toHaveBeenLastCalledWith(expectedOrderBook);
expect(onOrderBookUpdatedCallback).toHaveBeenCalledTimes(expectedOrderBooks.length);
for (let i = 0; i < expectedOrderBooks.length; i++) {
expect(onOrderBookUpdatedCallback).toHaveBeenNthCalledWith(i + 1, expectedOrderBooks[i]);
}
});

test('creates connection for every unique user', async () => {
Expand Down
1 change: 1 addition & 0 deletions tests/testHelpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export { TestAuthorizationManager } from './testAuthManager';
export { TestCurrenciesProvider } from './testCurrenciesProvider';
export { TestExchangeSymbolsProvider } from './testExchangeSymbolsProvider';
export { TestAtomexClient } from './testAtomexClient';
export { wait } from './wait';
export * as testLegacyHelpers from './testLegacyHelpers';
7 changes: 7 additions & 0 deletions tests/testHelpers/wait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const wait = (timeoutMs: number) => {
return new Promise<void>(resolve => {
setTimeout(() => {
resolve();
}, timeoutMs);
});
};

0 comments on commit d79b6da

Please sign in to comment.