forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriceRefresh.test.js
More file actions
184 lines (148 loc) · 5.12 KB
/
Copy pathpriceRefresh.test.js
File metadata and controls
184 lines (148 loc) · 5.12 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
const mockRefreshAll = jest.fn();
const mockEvaluateAll = jest.fn();
const mockNotifyPriceUpdates = jest.fn();
const mockLogger = {
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
};
jest.mock('../src/services/priceOracle', () => ({
refreshAllCachedPrices: mockRefreshAll,
}));
jest.mock('../src/services/alerts', () => ({
evaluateAll: mockEvaluateAll,
}));
jest.mock('../src/ws/PriceSubscriptionManager', () => ({
notifyPriceUpdates: mockNotifyPriceUpdates,
}));
jest.mock('../src/config', () => ({
price: {
refreshInterval: 30,
refreshMaxCycleMs: 90000,
},
}));
jest.mock('../src/logger', () => mockLogger);
// We need to mock node-cron to control when the callback fires
jest.mock('node-cron', () => {
let capturedCallback = null;
return {
schedule: jest.fn((_expr, opts, cb) => {
// Support both (expr, cb) and (expr, opts, cb) signatures
if (typeof opts === 'function') {
capturedCallback = opts;
} else {
capturedCallback = cb;
}
return {
stop: jest.fn(),
};
}),
__getCronCallback: () => capturedCallback,
__reset: () => { capturedCallback = null; },
};
});
const cron = require('node-cron');
const priceRefresh = require('../src/jobs/priceRefresh');
beforeEach(() => {
jest.clearAllMocks();
cron.__reset();
mockRefreshAll.mockResolvedValue({ XLM: { price: 0.1, source: 'stellar_dex' } });
mockEvaluateAll.mockResolvedValue(undefined);
});
describe('priceRefresh job reentrancy guard (#71)', () => {
test('starts and registers a cron callback', () => {
priceRefresh.start();
expect(cron.schedule).toHaveBeenCalled();
expect(cron.__getCronCallback()).toBeDefined();
});
test('skips tick when previous cycle is still running', async () => {
priceRefresh.start();
const cb = cron.__getCronCallback();
// Make the first call slow
let resolveFirst;
mockRefreshAll.mockImplementationOnce(() => new Promise((r) => { resolveFirst = r; }));
// Fire first tick
const p1 = cb();
// Give it a microtick to enter the running state
await new Promise((r) => setTimeout(r, 5));
// Fire second tick — should be skipped
await cb();
expect(mockRefreshAll).toHaveBeenCalledTimes(1);
expect(mockLogger.warn).toHaveBeenCalledWith(
'Skipping price refresh tick, previous cycle still running',
expect.objectContaining({ runningForMs: expect.any(Number) })
);
// Resolve the first tick
resolveFirst();
await p1;
});
test('logs error when cycle exceeds max duration', async () => {
priceRefresh.start();
const cb = cron.__getCronCallback();
// Make refresh take longer than the mock max cycle (we'll override config)
const configMock = require('../src/config');
const originalMax = configMock.price.refreshMaxCycleMs;
configMock.price.refreshMaxCycleMs = 50; // 50ms
try {
mockRefreshAll.mockImplementationOnce(() => new Promise((r) => setTimeout(r, 200)));
await cb();
expect(mockLogger.error).toHaveBeenCalledWith(
'Price refresh cycle exceeded max duration',
expect.objectContaining({ maxCycleMs: 50 })
);
} finally {
configMock.price.refreshMaxCycleMs = originalMax;
}
});
test('resets running flag after cycle completes', async () => {
priceRefresh.start();
const cb = cron.__getCronCallback();
mockRefreshAll.mockResolvedValueOnce({ XLM: { price: 0.1, source: 'stellar_dex' } });
await cb();
// Second tick should not be skipped
mockRefreshAll.mockResolvedValueOnce({ XLM: { price: 0.2, source: 'stellar_dex' } });
await cb();
expect(mockRefreshAll).toHaveBeenCalledTimes(2);
expect(mockLogger.warn).not.toHaveBeenCalledWith(
'Skipping price refresh tick, previous cycle still running',
expect.anything()
);
});
test('resets running flag even when cycle throws', async () => {
priceRefresh.start();
const cb = cron.__getCronCallback();
mockRefreshAll.mockRejectedValueOnce(new Error('boom'));
await cb();
// Second tick should run normally
mockRefreshAll.mockResolvedValueOnce({});
await cb();
expect(mockRefreshAll).toHaveBeenCalledTimes(2);
});
test('getHealth returns healthy state after successful run', async () => {
priceRefresh.start();
const cb = cron.__getCronCallback();
await cb();
const health = priceRefresh.getHealth();
expect(health.healthy).toBe(true);
expect(health.lastSuccessAt).toBeDefined();
expect(health.lastError).toBeNull();
expect(health.stalled).toBe(false);
expect(mockEvaluateAll).toHaveBeenCalledTimes(1);
});
test('getHealth reports error after failed cycle', async () => {
priceRefresh.start();
const cb = cron.__getCronCallback();
mockRefreshAll.mockRejectedValueOnce(new Error('redis down'));
await cb();
const health = priceRefresh.getHealth();
expect(health.lastError).toBe('redis down');
});
test('stop clears the scheduled task', () => {
priceRefresh.start();
priceRefresh.stop();
const health = priceRefresh.getHealth();
expect(health.healthy).toBe(false);
});
});