-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathrotation-proxy-state.test.ts
More file actions
200 lines (175 loc) · 6.52 KB
/
Copy pathrotation-proxy-state.test.ts
File metadata and controls
200 lines (175 loc) · 6.52 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { AccountManager } from "../lib/accounts.js";
import type { RotationProxyStateInit } from "../lib/runtime/rotation-proxy-state.js";
import type { AccountStorageV3 } from "../lib/storage.js";
const { recordRuntimeResetMock, recordRuntimeReloadMock } = vi.hoisted(() => ({
recordRuntimeResetMock: vi.fn(),
recordRuntimeReloadMock: vi.fn(),
}));
vi.mock("../lib/runtime/runtime-observability.js", async (importOriginal) => {
const actual = await importOriginal<
typeof import("../lib/runtime/runtime-observability.js")
>();
return {
...actual,
recordRuntimeReset: recordRuntimeResetMock,
recordRuntimeReload: recordRuntimeReloadMock,
};
});
const { createRotationProxyState, recoverStaleRuntimeState } = await import(
"../lib/runtime/rotation-proxy-state.js"
);
const NOW = Date.now();
function storageWith(count: number): AccountStorageV3 {
return {
version: 3,
activeIndex: 0,
activeIndexByFamily: {},
accounts: Array.from({ length: count }, (_unused, index) => ({
email: `account-${index + 1}@example.com`,
accountId: `acc_${index + 1}`,
refreshToken: `refresh-${index + 1}`,
accessToken: `access-${index + 1}`,
expiresAt: NOW + 3_600_000,
addedAt: NOW - 60_000,
lastUsed: NOW - 60_000,
enabled: true,
})),
};
}
function stateInit(): RotationProxyStateInit {
return {
activeAccountManager: new AccountManager(undefined, storageWith(1)),
routingMutexMode: "enabled",
schedulingStrategy: "hybrid",
fetchImpl: fetch,
upstreamBaseUrl: "https://upstream.example",
clientApiKey: "key",
now: () => NOW,
tokenRefreshSkewMs: 30_000,
networkErrorCooldownMs: 10_000,
serverErrorCooldownMs: 10_000,
tokenInvalidationCooldownMs: 300_000,
minRotationIntervalMs: 0,
pidOffsetEnabled: false,
fetchTimeoutMs: 30_000,
streamStallTimeoutMs: 30_000,
maxRuntimeAccountAttempts: 3,
maxRequestBodyBytes: 1024,
quotaRemainingPercentThreshold: 0,
sessionAffinityStore: null,
lastObservedAffinityGeneration: 0,
};
}
let loadFromDisk: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
vi.clearAllMocks();
loadFromDisk = vi.spyOn(AccountManager, "loadFromDisk");
});
afterEach(() => {
loadFromDisk.mockRestore();
});
describe("createRotationProxyState", () => {
it("seeds the known-manager set and a zeroed status from the injected clock", () => {
const init = stateInit();
const state = createRotationProxyState(init);
expect([...state.knownAccountManagers]).toEqual([
init.activeAccountManager,
]);
expect(state.status).toStrictEqual({
startedAt: NOW,
totalRequests: 0,
upstreamRequests: 0,
retries: 0,
rotations: 0,
streamsStarted: 0,
lastError: null,
lastAccountIndex: null,
lastAccountLabel: null,
lastAccountId: null,
lastAccountUpdatedAt: null,
});
expect(state.lastGlobalAccountIndex).toBeNull();
expect(state.lastStaleRuntimeReloadAt).toBe(0);
});
});
describe("recoverStaleRuntimeState", () => {
it("reloads from disk, swaps the active manager, and records observability", async () => {
const state = createRotationProxyState(stateInit());
const previousManager = state.activeAccountManager;
const reloaded = new AccountManager(undefined, storageWith(2));
loadFromDisk.mockResolvedValue(reloaded);
const result = await recoverStaleRuntimeState(state);
expect(result).toBe(reloaded);
expect(state.activeAccountManager).toBe(reloaded);
// The previous manager stays known so in-flight requests can finish.
expect(state.knownAccountManagers.has(previousManager)).toBe(true);
expect(state.knownAccountManagers.has(reloaded)).toBe(true);
// The configured mutex mode carries over to the reloaded pool.
expect(reloaded.getRoutingMutexMode()).toBe("enabled");
expect(state.lastStaleRuntimeReloadAt).toBeGreaterThan(0);
expect(recordRuntimeResetMock).toHaveBeenCalledExactlyOnceWith(
"pool-exhausted-no-account",
);
expect(recordRuntimeReloadMock).toHaveBeenCalledExactlyOnceWith(
"pool-exhausted-no-account",
);
});
it("dedupes within the 1s window and reloads again once it expires", async () => {
// The dedupe guard runs on the real wall clock (deliberately not the
// injected now()), so fake timers make the window deterministic.
vi.useFakeTimers();
try {
const state = createRotationProxyState(stateInit());
const reloaded = new AccountManager(undefined, storageWith(2));
loadFromDisk.mockResolvedValue(reloaded);
await recoverStaleRuntimeState(state);
const second = await recoverStaleRuntimeState(state);
// The second call lands inside the 1s dedupe window: no second reload.
expect(second).toBe(reloaded);
expect(loadFromDisk).toHaveBeenCalledTimes(1);
// Once the window expires, the next call reloads from disk again.
vi.advanceTimersByTime(1_001);
const freshest = new AccountManager(undefined, storageWith(3));
loadFromDisk.mockResolvedValue(freshest);
await expect(recoverStaleRuntimeState(state)).resolves.toBe(freshest);
expect(loadFromDisk).toHaveBeenCalledTimes(2);
} finally {
vi.useRealTimers();
}
});
it("shares one in-flight reload between concurrent callers", async () => {
const state = createRotationProxyState(stateInit());
const reloaded = new AccountManager(undefined, storageWith(2));
let releaseReload!: () => void;
const gate = new Promise<void>((resolve) => {
releaseReload = resolve;
});
loadFromDisk.mockImplementation(async () => {
await gate;
return reloaded;
});
const firstPending = recoverStaleRuntimeState(state);
const secondPending = recoverStaleRuntimeState(state);
releaseReload();
const [first, second] = await Promise.all([firstPending, secondPending]);
expect(first).toBe(reloaded);
expect(second).toBe(reloaded);
expect(loadFromDisk).toHaveBeenCalledTimes(1);
});
it("reports a failed reload and allows the next attempt to retry", async () => {
const state = createRotationProxyState(stateInit());
loadFromDisk.mockRejectedValueOnce(new Error("disk exploded"));
const failed = await recoverStaleRuntimeState(state);
expect(failed).toBeNull();
expect(state.status.lastError).toBe("disk exploded");
// The failure must not arm the dedupe window.
expect(state.lastStaleRuntimeReloadAt).toBe(0);
// The failure does not arm the dedupe window or leak a stale promise:
// the next call retries the reload.
const reloaded = new AccountManager(undefined, storageWith(2));
loadFromDisk.mockResolvedValue(reloaded);
await expect(recoverStaleRuntimeState(state)).resolves.toBe(reloaded);
expect(loadFromDisk).toHaveBeenCalledTimes(2);
});
});