-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: dydxwill <119354122+dydxwill@users.noreply.github.com>
- Loading branch information
1 parent
6029fa3
commit 141e4cc
Showing
8 changed files
with
425 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
indexer/services/roundtable/__tests__/tasks/pnl-instrumentation.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { logger, stats } from '@dydxprotocol-indexer/base'; | ||
import { | ||
BlockTable, | ||
PnlTicksTable, | ||
SubaccountTable, | ||
TransferTable, | ||
testMocks, | ||
dbHelpers, | ||
} from '@dydxprotocol-indexer/postgres'; | ||
import runTask from '../../src/tasks/pnl-instrumentation'; | ||
import { DateTime } from 'luxon'; | ||
import config from '../../src/config'; | ||
|
||
describe('pnl-instrumentation', () => { | ||
beforeAll(async () => { | ||
await dbHelpers.migrate(); | ||
await dbHelpers.clearData(); | ||
jest.spyOn(stats, 'gauge'); | ||
jest.spyOn(logger, 'error'); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testMocks.seedData(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await dbHelpers.teardown(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await dbHelpers.clearData(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('succeeds with no stale PNL subaccounts', async () => { | ||
jest.spyOn(BlockTable, 'getLatest').mockResolvedValue({ | ||
blockHeight: '12345', | ||
} as any); | ||
|
||
jest.spyOn(SubaccountTable, 'getSubaccountsWithTransfers').mockResolvedValue([ | ||
{ id: 'subaccount1' }, | ||
{ id: 'subaccount2' }, | ||
] as any); | ||
|
||
jest.spyOn(PnlTicksTable, 'findMostRecentPnlTickTimeForEachAccount').mockResolvedValue({ | ||
subaccount1: DateTime.utc().minus({ hours: 1 }).toISO(), | ||
subaccount2: DateTime.utc().minus({ hours: 1 }).toISO(), | ||
}); | ||
|
||
await runTask(); | ||
|
||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts`, 0); | ||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts_with_prior_pnl`, 0); | ||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts_without_prior_pnl`, 0); | ||
expect(logger.error).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('succeeds with stale PNL subaccounts', async () => { | ||
jest.spyOn(BlockTable, 'getLatest').mockResolvedValue({ | ||
blockHeight: '12345', | ||
} as any); | ||
|
||
jest.spyOn(SubaccountTable, 'getSubaccountsWithTransfers').mockResolvedValue([ | ||
{ id: 'subaccount1' }, | ||
{ id: 'subaccount2' }, | ||
] as any); | ||
|
||
jest.spyOn(PnlTicksTable, 'findMostRecentPnlTickTimeForEachAccount').mockResolvedValue({ | ||
subaccount1: DateTime.utc().minus({ hours: 3 }).toISO(), | ||
subaccount2: DateTime.utc().minus({ hours: 3 }).toISO(), | ||
}); | ||
|
||
await runTask(); | ||
|
||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts`, 2); | ||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts_with_prior_pnl`, 2); | ||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts_without_prior_pnl`, 0); | ||
expect(logger.error).toHaveBeenCalledWith({ | ||
at: 'pnl-instrumentation#statPnl', | ||
message: 'Subaccount ids with stale PNL data', | ||
stalePnlSubaccounts: ['subaccount1', 'subaccount2'], | ||
staleTransferSubaccounts: [], | ||
}); | ||
}); | ||
|
||
it('succeeds with stale transfer subaccounts', async () => { | ||
jest.spyOn(BlockTable, 'getLatest').mockResolvedValue({ | ||
blockHeight: '12345', | ||
} as any); | ||
|
||
jest.spyOn(SubaccountTable, 'getSubaccountsWithTransfers').mockResolvedValue([ | ||
{ id: 'subaccount1' }, | ||
{ id: 'subaccount2' }, | ||
] as any); | ||
|
||
jest.spyOn(PnlTicksTable, 'findMostRecentPnlTickTimeForEachAccount').mockResolvedValue({ | ||
subaccount1: DateTime.utc().minus({ hours: 1 }).toISO(), | ||
}); | ||
|
||
jest.spyOn(TransferTable, 'getLastTransferTimeForSubaccounts').mockResolvedValue({ | ||
subaccount2: DateTime.utc().minus({ hours: 3 }).toISO(), | ||
}); | ||
|
||
await runTask(); | ||
|
||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts`, 1); | ||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts_with_prior_pnl`, 0); | ||
expect(stats.gauge).toHaveBeenCalledWith(`${config.SERVICE_NAME}.pnl_stale_subaccounts_without_prior_pnl`, 1); | ||
expect(logger.error).toHaveBeenCalledWith({ | ||
at: 'pnl-instrumentation#statPnl', | ||
message: 'Subaccount ids with stale PNL data', | ||
stalePnlSubaccounts: [], | ||
staleTransferSubaccounts: ['subaccount2'], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.