forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcacheWarm.test.js
More file actions
138 lines (116 loc) · 4.73 KB
/
Copy pathcacheWarm.test.js
File metadata and controls
138 lines (116 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
jest.mock('../src/logger', () => ({
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
}));
jest.mock('../src/services/priceOracle', () => ({
fetchFreshPrice: jest.fn(),
getQueriedAssets: jest.fn(async () => []),
}));
const logger = require('../src/logger');
const priceOracle = require('../src/services/priceOracle');
const { warmCache } = require('../src/startup/cacheWarm');
function asset(code, issuer = null) {
return { code, issuer };
}
describe('startup cache warming', () => {
beforeEach(() => {
jest.useRealTimers();
jest.clearAllMocks();
priceOracle.getQueriedAssets.mockResolvedValue([]);
});
test('skips warming when no assets are configured', async () => {
const summary = await warmCache([], priceOracle, { log: logger });
expect(summary).toEqual({
total: 0,
succeeded: 0,
failed: 0,
timedOut: false,
durationMs: 0,
});
expect(priceOracle.fetchFreshPrice).not.toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledWith('Cache warm skipped: no watched assets configured');
});
test('fetches all configured assets and counts cached successes', async () => {
priceOracle.fetchFreshPrice
.mockResolvedValueOnce({ price_usd: 0.12, redis_unavailable: false })
.mockResolvedValueOnce({ price_usd: 1.0, redis_unavailable: false })
.mockResolvedValueOnce({ price_usd: null, redis_unavailable: false });
const assets = [
asset('XLM'),
asset('USDC', 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'),
asset('BAD'),
];
const summary = await warmCache(assets, priceOracle, { log: logger });
expect(priceOracle.fetchFreshPrice).toHaveBeenCalledTimes(3);
expect(priceOracle.fetchFreshPrice).toHaveBeenNthCalledWith(1, 'XLM', null);
expect(priceOracle.fetchFreshPrice).toHaveBeenNthCalledWith(
2,
'USDC',
'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
);
expect(priceOracle.fetchFreshPrice).toHaveBeenNthCalledWith(3, 'BAD', null);
expect(summary).toMatchObject({ total: 3, succeeded: 2, failed: 1, timedOut: false });
expect(logger.info).toHaveBeenCalledWith('Cache warm complete', expect.objectContaining({
total: 3,
succeeded: 2,
failed: 1,
timedOut: false,
}));
});
test('includes user-queried assets in startup cache warming', async () => {
priceOracle.getQueriedAssets.mockResolvedValueOnce([asset('BTC'), asset('ETH')]);
priceOracle.fetchFreshPrice
.mockResolvedValueOnce({ price_usd: 0.12, redis_unavailable: false })
.mockResolvedValueOnce({ price_usd: 60000, redis_unavailable: false })
.mockResolvedValueOnce({ price_usd: 3000, redis_unavailable: false });
const summary = await warmCache([asset('XLM')], priceOracle, { log: logger });
expect(priceOracle.fetchFreshPrice).toHaveBeenCalledTimes(3);
expect(priceOracle.fetchFreshPrice).toHaveBeenNthCalledWith(1, 'XLM', null);
expect(priceOracle.fetchFreshPrice).toHaveBeenNthCalledWith(2, 'BTC', null);
expect(priceOracle.fetchFreshPrice).toHaveBeenNthCalledWith(3, 'ETH', null);
expect(summary).toMatchObject({ total: 3, succeeded: 3, failed: 0, timedOut: false });
});
test('starts all asset fetches before awaiting settlement', async () => {
let resolveXlm;
let resolveUsdc;
const xlmPromise = new Promise((resolve) => { resolveXlm = resolve; });
const usdcPromise = new Promise((resolve) => { resolveUsdc = resolve; });
priceOracle.fetchFreshPrice
.mockReturnValueOnce(xlmPromise)
.mockReturnValueOnce(usdcPromise);
const warming = warmCache([asset('XLM'), asset('USDC')], priceOracle, { log: logger });
await new Promise((r) => setImmediate(r));
expect(priceOracle.fetchFreshPrice).toHaveBeenCalledTimes(2);
resolveXlm({ price_usd: 0.12, redis_unavailable: false });
resolveUsdc({ price_usd: 1.0, redis_unavailable: false });
await expect(warming).resolves.toMatchObject({ succeeded: 2, failed: 0 });
});
test('returns a timeout summary when warming takes too long', async () => {
jest.useFakeTimers();
priceOracle.getQueriedAssets.mockResolvedValue([]);
priceOracle.fetchFreshPrice.mockReturnValue(new Promise(() => {}));
const warming = warmCache([asset('XLM')], priceOracle, {
timeoutMs: 25,
log: logger,
});
await Promise.resolve();
jest.advanceTimersByTime(25);
await expect(warming).resolves.toEqual({
total: 1,
succeeded: 0,
failed: 1,
timedOut: true,
durationMs: 25,
});
expect(logger.warn).toHaveBeenCalledWith('Cache warm timed out; starting server anyway', {
total: 1,
succeeded: 0,
failed: 1,
timedOut: true,
durationMs: 25,
});
});
});