Skip to content

Commit

Permalink
Merge pull request #28 from FinBook/event
Browse files Browse the repository at this point in the history
Event
  • Loading branch information
yizhoucao committed Feb 26, 2019
2 parents 470530d + c82e421 commit 1f4d4db
Show file tree
Hide file tree
Showing 19 changed files with 571 additions and 127 deletions.
3 changes: 2 additions & 1 deletion bash/aws1.sh → bash/aws.sh
Expand Up @@ -3,4 +3,5 @@ rm *.log
npm run trades assets=ETH,USD server aws &> trades.ALL.log &
npm run commit server aws pair=ETH_USD &>> commit.log &
npm run cleanDB server aws &>> cleanDB.log &
npm run round contractType=Vivaldi tenor=100C-3H debug server aws &>> vivaldi-100C-3H.log &
npm run round contractType=Vivaldi tenor=100C-3H debug server aws &>> vivaldi-100C-3H.log &
npm run events server events=StartReset,StartPreReset,Others $1 &>> events.log &
3 changes: 0 additions & 3 deletions bash/aws2.sh

This file was deleted.

3 changes: 2 additions & 1 deletion bash/azure1.sh → bash/azure.sh
Expand Up @@ -3,4 +3,5 @@ rm *.log
npm run trades assets=ETH,USD server azure &> trades.ALL.log &
npm run commit server azure pair=ETH_USD &>> commit.log &
npm run cleanDB server azure &>> cleanDB.log &
npm run round contractType=Vivaldi tenor=100C-3H debug server azure &>> vivaldi-100C-3H.log &
npm run round contractType=Vivaldi tenor=100C-3H debug server azure &>> vivaldi-100C-3H.log &
npm run events server events=StartReset,StartPreReset,Others $1 &>> events.log &
3 changes: 0 additions & 3 deletions bash/azure2.sh

This file was deleted.

3 changes: 2 additions & 1 deletion bash/gcp1.sh → bash/gcp.sh
Expand Up @@ -3,4 +3,5 @@ rm *.log
npm run trades assets=ETH,USD server gcp &> trades.ALL.log &
npm run commit server gcp pair=ETH_USD &>> commit.log &
npm run cleanDB server gcp &>> cleanDB.log &
npm run round contractType=Vivaldi tenor=100C-3H debug server gcp &>> vivaldi-100C-3H.log &
npm run round contractType=Vivaldi tenor=100C-3H debug server gcp &>> vivaldi-100C-3H.log &
npm run events server events=StartReset,StartPreReset,Others $1 &>> events.log &
3 changes: 0 additions & 3 deletions bash/gcp2.sh

This file was deleted.

2 changes: 1 addition & 1 deletion bash/public.sh
Expand Up @@ -3,4 +3,4 @@ rm *.log
npm run trades assets=ETH,USD server dynamo &> trades.ALL.log &
npm run prices period=1 dev dynamo &> prices.1.log &
npm run prices period=60 dev dynamo &> prices.60.log &
npm run fetchEvents server dynamo $1 &>> others.log &
npm run events server dynamo events=Others $1 &>> events.log &
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -9,14 +9,13 @@
"tslint": "tslint -c tslint.json -p tsconfig.json src/**/*.ts{,x} --fix",
"commit": "ts-node ./src/index commit",
"trades": "ts-node src/index trades",
"trigger": "ts-node ./src/index trigger",
"prices": "ts-node src/index prices",
"node": "ts-node ./src/index node",
"cleanDB": "ts-node ./src/index cleanDB",
"fetchPrice": "ts-node ./src/index fetchPrice",
"fetchEvents": "ts-node ./src/index fetchEvents",
"startCustodian": "ts-node ./src/index startCustodian",
"round": "ts-node ./src/index round"
"round": "ts-node ./src/index round",
"events": "ts-node ./src/index events"
},
"jest": {
"roots": [
Expand Down
7 changes: 6 additions & 1 deletion src/common/constants.ts
Expand Up @@ -6,8 +6,13 @@ export const CLEAN_DB = 'cleanDB';
export const FETCH_PRICE = 'fetchPrice';
export const START_CUSTODIAN = 'startCustodian';
export const FETCH_EVENTS = 'fetchEvents';
export const EVENTS = 'events';
export const ROUND = 'round';

export const EVENTS_START_RESET = 'StartReset';
export const EVENTS_START_PRE_RESET = 'StartPreReset';
export const EVENTS_OTHERS = 'Others';

// db setting
export const DB_SQL_SCHEMA_PRICEFEED = 'priceFeedDB';
export const DB_SQL_TRADE = 'trades';
Expand Down Expand Up @@ -79,4 +84,4 @@ export const REDEEM_GAS_TH = 0.005;
export const CREATE_INTERVAL = 10; // in seconds
export const CREATE_GAS_TH = 0.01;
export const EVENT_FETCH_BLOCK_INTERVAL = 100;
export const EVENT_FETCH_TIME_INTERVAL = 600000;
export const EVENT_FETCH_TIME_INTERVAL = 300000;
1 change: 1 addition & 0 deletions src/common/types.ts
Expand Up @@ -17,6 +17,7 @@ export interface IOption {
sources: string[];
exSources: string[];
assets: string[];
events: string[];
live: boolean;
dbLive: boolean;
server: boolean;
Expand Down
7 changes: 2 additions & 5 deletions src/index.ts
Expand Up @@ -34,8 +34,8 @@ dbUtil.init(tool, option).then(() => {
case CST.TRADES:
new MarketDataService().startFetching(tool, option);
break;
case CST.FETCH_EVENTS:
new ContractService(tool, option).fetchEvent();
case CST.EVENTS:
new MarketDataService().startFetchingEvent(tool, option);
break;
case DataConstants.DB_PRICES:
new MarketDataService().startAggregate(option.period);
Expand All @@ -46,9 +46,6 @@ dbUtil.init(tool, option).then(() => {
case CST.START_CUSTODIAN:
new ContractService(tool, option).startCustodian(option);
break;
case CST.TRIGGER:
new ContractService(tool, option).trigger();
break;
case CST.COMMIT:
new ContractService(tool, option).commitPrice();
break;
Expand Down
6 changes: 3 additions & 3 deletions src/services/ContractService.test.ts
Expand Up @@ -173,7 +173,7 @@ test('fetchEvent', async () => {

test('startCustodian, worng type', async () => {
contractService.createDuoWrappers = jest.fn();
await contractService.startCustodian(option);
await contractService.startCustodian(option as any);
expect(contractService.createDuoWrappers as jest.Mock).not.toBeCalled();
});

Expand Down Expand Up @@ -203,7 +203,7 @@ test('startCustodian, dualClass', async () => {
}
} as any)
);
await contractService1.startCustodian(option);
await contractService1.startCustodian(option as any);
expect((startCustodian as jest.Mock).mock.calls).toMatchSnapshot();
});

Expand Down Expand Up @@ -233,7 +233,7 @@ test('startCustodian, wrong type', async () => {
}
} as any)
);
await contractService1.startCustodian(option);
await contractService1.startCustodian(option as any);
expect(startCustodian as jest.Mock).not.toBeCalled();
});

Expand Down
165 changes: 142 additions & 23 deletions src/services/MarketDataService.test.ts
Expand Up @@ -8,12 +8,24 @@ import osUtil from '../utils/osUtil';
import priceUtil from '../utils/priceUtil';
import util from '../utils/util';
import MarketDataService from './MarketDataService';

jest.mock('@finbook/duo-contract-wrapper', () => ({
Web3Wrapper: jest.fn(() => ({
contractAddresses: kovan
}))
}));

const triggerMock = jest.fn();
const fetchEventMock = jest.fn();
jest.mock('../services/ContractService', () =>
jest.fn().mockImplementation(() => ({
trigger: triggerMock,
fetchEvent: fetchEventMock
}))
);

import ContractService from '../services/ContractService';

const marketDataService = new MarketDataService();
test('retry after long enought time', () => {
const launchMock = jest.fn();
Expand All @@ -28,23 +40,13 @@ test('retry after long enought time', () => {
failCount: 2,
instance: undefined as any
};
marketDataService.retry(
'tool',
{
forceREST: false,
debug: false,
live: false
} as any,
'source',
['asset1', 'asset2', 'asset3']
);
marketDataService.retry('source', launchMock);
expect(marketDataService.subProcesses['source']).toMatchSnapshot();
expect((global.setTimeout as jest.Mock).mock.calls).toMatchSnapshot();
(global.setTimeout as jest.Mock).mock.calls[0][0]();
expect(launchMock.mock.calls).toMatchSnapshot();
expect(launchMock as jest.Mock).toBeCalledTimes(1);
marketDataService.launchSource = launchOriginal;
});

test('retry within short time', () => {
child_process.exec = jest.fn() as any;
util.getUTCNowTimestamp = jest.fn(() => 1234567890);
Expand All @@ -55,22 +57,13 @@ test('retry within short time', () => {
failCount: 2,
instance: undefined as any
};
marketDataService.retry(
'tool',
{
forceREST: false,
debug: false,
live: false
} as any,
'source',
['asset1', 'asset2', 'asset3']
);
marketDataService.retry('source', () => jest.fn());
expect(marketDataService.subProcesses['source']).toMatchSnapshot();
});

test('launchSource fail windows', () => {
osUtil.isWindows = jest.fn(() => true);
child_process.exec = jest.fn() as any;
child_process.exec = jest.fn(() => false) as any;
util.getUTCNowTimestamp = jest.fn(() => 1234567890);
marketDataService.retry = jest.fn();
marketDataService.subProcesses['source'] = {
Expand All @@ -86,9 +79,14 @@ test('launchSource fail windows', () => {
dynamo: true,
azure: true
} as any);
const originalLaunchSource = marketDataService.launchSource;
marketDataService.launchSource = jest.fn();
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
expect((marketDataService.retry as jest.Mock<void>).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect((marketDataService.launchSource as jest.Mock).mock.calls).toMatchSnapshot();
marketDataService.launchSource = originalLaunchSource;
});

test('launchSource success windows', () => {
Expand All @@ -113,12 +111,17 @@ test('launchSource success windows', () => {
live: false,
aws: true
} as any);
const originalLaunchSource = marketDataService.launchSource;
marketDataService.launchSource = jest.fn();
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
expect(execOn.mock.calls).toMatchSnapshot();
execOn.mock.calls[0][1]();
execOn.mock.calls[0][1](1);
expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect((marketDataService.launchSource as jest.Mock).mock.calls).toMatchSnapshot();
marketDataService.launchSource = originalLaunchSource;
});

test('launchSource forceREST windows', () => {
Expand Down Expand Up @@ -285,6 +288,89 @@ test('launchSource live not windows', () => {
expect(marketDataService.subProcesses).toMatchSnapshot();
});

test('launchEvent ', () => {
osUtil.isWindows = jest.fn(() => false);
child_process.exec = jest.fn() as any;
util.getUTCNowTimestamp = jest.fn(() => 1234567890);
marketDataService.subProcesses['event'] = {
source: 'event',
lastFailTimestamp: 0,
failCount: 0,
instance: undefined as any
};
marketDataService.launchEvent('tool', 'event', {
forceREST: false,
debug: false,
live: true,
event: 'event',
gcp: true
} as any);
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
});

test('launchEvent azure', () => {
osUtil.isWindows = jest.fn(() => false);
const on = jest.fn();
child_process.exec = jest.fn(() => ({
on: on
})) as any;
marketDataService.retry = jest.fn();
util.getUTCNowTimestamp = jest.fn(() => 1234567890);
marketDataService.subProcesses['event'] = {
source: 'event',
lastFailTimestamp: 0,
failCount: 0,
instance: undefined as any
};
marketDataService.launchEvent('tool', 'event', {
forceREST: false,
debug: false,
live: true,
event: 'event',
azure: true
} as any);
const originalLaunchEvent = marketDataService.launchEvent;
marketDataService.launchEvent = jest.fn();
expect(((child_process.exec as any) as jest.Mock).mock.calls).toMatchSnapshot();
expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot();
(on as jest.Mock).mock.calls[0][1]();
(on as jest.Mock).mock.calls[0][1]('code');
expect((on as jest.Mock).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect(marketDataService.subProcesses).toMatchSnapshot();
expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot();
marketDataService.launchEvent = originalLaunchEvent;
});

test('launchEvent with existingInstance, no windows', () => {
osUtil.isWindows = jest.fn(() => true);
child_process.exec = jest.fn(() => null) as any;
util.getUTCNowTimestamp = jest.fn(() => 1234567890);
marketDataService.retry = jest.fn();
marketDataService.subProcesses['Others'] = {
source: 'Others',
lastFailTimestamp: 0,
failCount: 0,
instance: undefined as any
};
marketDataService.launchEvent('tool', 'Others', {
forceREST: false,
debug: false,
live: true,
event: 'Others',
server: true,
aws: true
} as any);
const originalLaunchEvent = marketDataService.launchEvent;
marketDataService.launchEvent = jest.fn();
expect((marketDataService.retry as jest.Mock).mock.calls).toMatchSnapshot();
(marketDataService.retry as jest.Mock).mock.calls[0][1]();
expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
marketDataService.launchEvent = originalLaunchEvent;
});

test('startFetching no source', async () => {
util.sleep = jest.fn();
marketDataService.launchSource = jest.fn();
Expand Down Expand Up @@ -362,6 +448,39 @@ test('startFetching source no sourcePairs', async () => {
expect(geminiApi.fetchTrades as jest.Mock).not.toBeCalled();
});

test('startFetchingEvent no event', async () => {
util.sleep = jest.fn();
marketDataService.launchEvent = jest.fn();

await marketDataService.startFetchingEvent('tool', {
event: '',
debug: false,
events: ['event1', 'event2']
} as any);

expect((marketDataService.launchEvent as jest.Mock).mock.calls).toMatchSnapshot();
expect(marketDataService.subProcesses).toMatchSnapshot();
});

test('startFetchingEvent with event', async () => {
util.sleep = jest.fn();
marketDataService.launchEvent = jest.fn();

await marketDataService.startFetchingEvent('tool', {
event: 'event',
debug: false
} as any);

await marketDataService.startFetchingEvent('tool', {
event: 'StartReset',
debug: false
} as any);

expect(triggerMock).toBeCalledTimes(1);
expect(fetchEventMock).toBeCalledTimes(1);
expect((ContractService as any).mock.calls).toMatchSnapshot();
});

test('cleanDb', async () => {
dbUtil.cleanDB = jest.fn();
dbUtil.insertHeartbeat = jest.fn();
Expand Down

0 comments on commit 1f4d4db

Please sign in to comment.