Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce queue ✨ #464

Merged
merged 12 commits into from
Aug 14, 2022
146 changes: 78 additions & 68 deletions app/__tests__/server-binance.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable global-require */
// eslint-disable-next-line max-classes-per-file
const { logger } = require('../helpers');

describe('server-binance', () => {
let PubSubMock;
let loggerMock;
let cacheMock;
let mongoMock;
let mockPubSub;
let mockCache;
let mockMongo;
let mockQueue;

let mockGetGlobalConfiguration;

Expand Down Expand Up @@ -37,19 +39,48 @@ describe('server-binance', () => {
jest.clearAllMocks().resetModules();
jest.useFakeTimers();

const { PubSub, logger, cache, mongo, slack } = require('../helpers');

jest.mock('config');
jest.mock('../cronjob');

config = require('config');

PubSubMock = PubSub;
loggerMock = logger;
cacheMock = cache;
mongoMock = mongo;

mockSlack = slack;
mockSlack.sendMessage = jest.fn().mockResolvedValue(true);
mockCache = {
hget: jest
.fn()
.mockResolvedValue(
JSON.stringify(require('./fixtures/exchange-symbols.json'))
),
hset: jest.fn().mockResolvedValue(true)
};
mockMongo = {
deleteAll: jest.fn().mockResolvedValue(true)
};
mockQueue = {
init: jest.fn().mockResolvedValue(true)
};
mockSlack = {
sendMessage: jest.fn().mockResolvedValue(true)
};
mockPubSub = {
subscribe: jest.fn(),
publish: jest.fn()
};

jest.mock('../helpers', () => ({
logger: {
info: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
debug: jest.fn(),
child: jest.fn()
},
slack: mockSlack,
mongo: mockMongo,
PubSub: mockPubSub,
cache: mockCache
}));

jest.mock('../cronjob/trailingTradeHelper/queue', () => mockQueue);
});

describe('when the bot is running live mode', () => {
Expand Down Expand Up @@ -132,30 +163,19 @@ describe('server-binance', () => {
getWebsocketTickersClean: mockGetWebsocketTickersClean
}));

mongoMock.deleteAll = jest.fn().mockResolvedValue(true);

PubSubMock.subscribe = jest.fn().mockResolvedValue(true);

cacheMock.hget = jest
.fn()
.mockResolvedValue(
JSON.stringify(require('./fixtures/exchange-symbols.json'))
);
cacheMock.hset = jest.fn().mockResolvedValue(true);

const { runBinance } = require('../server-binance');
await runBinance(loggerMock);
await runBinance(logger);
});

it('triggers PubSub.subscribe for reset-all-websockets', () => {
expect(PubSubMock.subscribe).toHaveBeenCalledWith(
expect(mockPubSub.subscribe).toHaveBeenCalledWith(
'reset-all-websockets',
expect.any(Function)
);
});

it('triggers PubSub.subscribe for reset-symbol-websockets', () => {
expect(PubSubMock.subscribe).toHaveBeenCalledWith(
expect(mockPubSub.subscribe).toHaveBeenCalledWith(
'reset-symbol-websockets',
expect.any(Function)
);
Expand All @@ -170,50 +190,54 @@ describe('server-binance', () => {
});

it('triggers refreshCandles', () => {
expect(mongoMock.deleteAll).toHaveBeenCalledWith(
loggerMock,
expect(mockMongo.deleteAll).toHaveBeenCalledWith(
logger,
'trailing-trade-candles',
{}
);
expect(mongoMock.deleteAll).toHaveBeenCalledWith(
loggerMock,
expect(mockMongo.deleteAll).toHaveBeenCalledWith(
logger,
'trailing-trade-ath-candles',
{}
);
});

it('triggers syncCandles', () => {
expect(mockSyncCandles).toHaveBeenCalledWith(loggerMock, [
expect(mockSyncCandles).toHaveBeenCalledWith(logger, [
'BTCUSDT',
'BNBUSDT'
]);
});

it('triggers syncATHCandles', () => {
expect(mockSyncATHCandles).toHaveBeenCalledWith(loggerMock, [
expect(mockSyncATHCandles).toHaveBeenCalledWith(logger, [
'BTCUSDT',
'BNBUSDT'
]);
});

it('triggers syncOpenOrders', () => {
expect(mockSyncOpenOrders).toHaveBeenCalledWith(loggerMock, [
expect(mockSyncOpenOrders).toHaveBeenCalledWith(logger, [
'BTCUSDT',
'BNBUSDT'
]);
});

it('triggers syncDatabaseOrders', () => {
expect(mockSyncDatabaseOrders).toHaveBeenCalledWith(loggerMock);
expect(mockSyncDatabaseOrders).toHaveBeenCalledWith(logger);
});

it('triggers cache.hset', () => {
expect(cacheMock.hset).toHaveBeenCalledWith(
expect(mockCache.hset).toHaveBeenCalledWith(
'trailing-trade-streams',
`count`,
1
);
});

it('triggers queue.init', () => {
expect(mockQueue.init).toHaveBeenCalled();
});
});

describe('calculates number of open streams', () => {
Expand Down Expand Up @@ -303,15 +327,9 @@ describe('server-binance', () => {
getWebsocketTickersClean: mockGetWebsocketTickersClean
}));

PubSubMock.subscribe = jest.fn().mockResolvedValue(true);

mongoMock.deleteAll = jest.fn().mockResolvedValue(true);

cacheMock.hset = jest.fn().mockResolvedValue(true);

const { runBinance } = require('../server-binance');

await runBinance(loggerMock);
await runBinance(logger);
});

it('triggers getWebsocketTickersClean', () => {
Expand All @@ -331,7 +349,7 @@ describe('server-binance', () => {
});

it('triggers cache.hset', () => {
expect(cacheMock.hset).toHaveBeenCalledWith(
expect(mockCache.hset).toHaveBeenCalledWith(
'trailing-trade-streams',
`count`,
1 + 5
Expand Down Expand Up @@ -419,27 +437,23 @@ describe('server-binance', () => {
getWebsocketTickersClean: mockGetWebsocketTickersClean
}));

PubSubMock.subscribe = jest.fn().mockImplementation((_key, cb) => {
mockPubSub.subscribe = jest.fn().mockImplementation((_key, cb) => {
cb('message', 'data');
});

mongoMock.deleteAll = jest.fn().mockResolvedValue(true);

cacheMock.hset = jest.fn().mockResolvedValue(true);

const { runBinance } = require('../server-binance');
await runBinance(loggerMock);
await runBinance(logger);
});

it('triggers PubSub.subscribe for reset-all-websockets', () => {
expect(PubSubMock.subscribe).toHaveBeenCalledWith(
expect(mockPubSub.subscribe).toHaveBeenCalledWith(
'reset-all-websockets',
expect.any(Function)
);
});

it('triggers PubSub.subscribe for reset-symbol-websockets', () => {
expect(PubSubMock.subscribe).toHaveBeenCalledWith(
expect(mockPubSub.subscribe).toHaveBeenCalledWith(
'reset-symbol-websockets',
expect.any(Function)
);
Expand All @@ -454,13 +468,13 @@ describe('server-binance', () => {
});

it('triggers refreshCandles', () => {
expect(mongoMock.deleteAll).toHaveBeenCalledWith(
loggerMock,
expect(mockMongo.deleteAll).toHaveBeenCalledWith(
logger,
'trailing-trade-candles',
{}
);
expect(mongoMock.deleteAll).toHaveBeenCalledWith(
loggerMock,
expect(mockMongo.deleteAll).toHaveBeenCalledWith(
logger,
'trailing-trade-ath-candles',
{}
);
Expand All @@ -471,35 +485,35 @@ describe('server-binance', () => {
});

it('triggers syncCandles', () => {
expect(mockSyncCandles).toHaveBeenCalledWith(loggerMock, [
expect(mockSyncCandles).toHaveBeenCalledWith(logger, [
'BTCUSDT',
'ETHUSDT',
'LTCUSDT'
]);
});

it('triggers syncATHCandles', () => {
expect(mockSyncATHCandles).toHaveBeenCalledWith(loggerMock, [
expect(mockSyncATHCandles).toHaveBeenCalledWith(logger, [
'BTCUSDT',
'ETHUSDT',
'LTCUSDT'
]);
});

it('triggers syncOpenOrders', () => {
expect(mockSyncOpenOrders).toHaveBeenCalledWith(loggerMock, [
expect(mockSyncOpenOrders).toHaveBeenCalledWith(logger, [
'BTCUSDT',
'ETHUSDT',
'LTCUSDT'
]);
});

it('triggers syncDatabaseOrders', () => {
expect(mockSyncDatabaseOrders).toHaveBeenCalledWith(loggerMock);
expect(mockSyncDatabaseOrders).toHaveBeenCalledWith(logger);
});

it('triggers cache.hset', () => {
expect(cacheMock.hset).toHaveBeenCalledWith(
expect(mockCache.hset).toHaveBeenCalledWith(
'trailing-trade-streams',
`count`,
1
Expand Down Expand Up @@ -586,15 +600,11 @@ describe('server-binance', () => {
getWebsocketTickersClean: mockGetWebsocketTickersClean
}));

PubSubMock.subscribe = jest.fn().mockResolvedValue(true);

mongoMock.deleteAll = jest.fn().mockResolvedValue(true);

cacheMock.hset = jest.fn().mockResolvedValue(true);
mockPubSub.subscribe = jest.fn().mockResolvedValue(true);

const { runBinance } = require('../server-binance');
await runBinance(loggerMock);
await runBinance(loggerMock);
await runBinance(logger);
await runBinance(logger);
});

it('triggers cacheExchangeSymbols', () => {
Expand Down
13 changes: 13 additions & 0 deletions app/__tests__/server-frontend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('server-frontend', () => {
let mockConfigureWebServer;
let mockConfigureWebSocket;
let mockConfigureLocalTunnel;
let mockConfigureBullBoard;

let mockRateLimiterRedisGet;
let mockRateLimiterRedis;
Expand Down Expand Up @@ -59,6 +60,7 @@ describe('server-frontend', () => {
mockConfigureWebServer = jest.fn().mockReturnValue(true);
mockConfigureWebSocket = jest.fn().mockReturnValue(true);
mockConfigureLocalTunnel = jest.fn().mockReturnValue(true);
mockConfigureBullBoard = jest.fn().mockReturnValue(true);

mockExpressStatic = jest.fn().mockReturnValue(true);

Expand Down Expand Up @@ -126,6 +128,10 @@ describe('server-frontend', () => {
jest.mock('../frontend/local-tunnel/configure', () => ({
configureLocalTunnel: mockConfigureLocalTunnel
}));

jest.mock('../frontend/bull-board/configure', () => ({
configureBullBoard: mockConfigureBullBoard
}));
});

describe('web server', () => {
Expand Down Expand Up @@ -169,6 +175,13 @@ describe('server-frontend', () => {
});
});

it('triggers configureBullBoard', () => {
expect(mockConfigureBullBoard).toHaveBeenCalledWith(
expect.any(Object),
loggerMock
);
});

it('triggers server.listen', () => {
expect(mockExpressListen).toHaveBeenCalledWith(80);
});
Expand Down
Loading