forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress-hardening.test.ts
More file actions
256 lines (211 loc) · 7.62 KB
/
Copy pathstress-hardening.test.ts
File metadata and controls
256 lines (211 loc) · 7.62 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { describe, expect, it } from "vitest";
import {
AgentInstanceManager,
AgentRuntime,
InMemoryMemoryStore,
InMemoryRuntimeLogger,
InMemoryRuntimeStateStore,
RuntimeEventBus
} from "../../src/index.js";
describe("AgentLily Runtime Stress & Hardening Test Suite", () => {
describe("RuntimeEventBus High-Volume & Error Isolation Stress", () => {
it("handles 20,000 event dispatches across multiple concurrent subscribers without leaking listeners", () => {
const bus = new RuntimeEventBus();
let subscriber1Count = 0;
let subscriber2Count = 0;
let subscriber3Count = 0;
const unsub1 = bus.on("runtime.task.received", () => {
subscriber1Count++;
});
const unsub2 = bus.on("runtime.task.received", () => {
subscriber2Count++;
});
const unsub3 = bus.on("runtime.task.received", () => {
subscriber3Count++;
});
expect(bus.listenerCount("runtime.task.received")).toBe(3);
const iterations = 20_000;
for (let i = 0; i < iterations; i++) {
bus.emit({
name: "runtime.task.received",
payload: {
runtimeId: "rt-stress",
taskId: `task-${i}`,
agentId: `agent-${i % 10}`
}
});
}
expect(subscriber1Count).toBe(iterations);
expect(subscriber2Count).toBe(iterations);
expect(subscriber3Count).toBe(iterations);
// Unsubscribe all and verify clean map teardown
unsub1();
unsub2();
unsub3();
expect(bus.listenerCount("runtime.task.received")).toBe(0);
expect(bus.listenerCount()).toBe(0);
});
it("isolates synchronous and asynchronous listener exceptions without breaking dispatch or leaking unhandled rejections", async () => {
const errorsCaught: unknown[] = [];
const bus = new RuntimeEventBus((err) => {
errorsCaught.push(err);
});
let normalSubscriberReceived = 0;
// Faulty listener 1: Throws synchronously
bus.on("runtime.task.completed", () => {
throw new Error("Faulty sync listener exploded");
});
// Normal listener
bus.on("runtime.task.completed", () => {
normalSubscriberReceived++;
});
// Faulty listener 2: Rejects asynchronously
bus.on("runtime.task.completed", async () => {
throw new Error("Faulty async listener rejected");
});
bus.emit({
name: "runtime.task.completed",
payload: {
runtimeId: "rt-isolate",
taskId: "t-1",
agentId: "a-1",
toolName: "calc"
}
});
// Wait a tick for async rejection catch handler
await new Promise((resolve) => setTimeout(resolve, 20));
expect(normalSubscriberReceived).toBe(1);
expect(errorsCaught.length).toBe(2);
});
it("supports once listeners and unsubscribing during active dispatch", () => {
const bus = new RuntimeEventBus();
let onceFired = 0;
let dynamicFired = 0;
bus.once("runtime.started", () => {
onceFired++;
});
let unsubDynamic: (() => void) | null = null;
unsubDynamic = bus.on("runtime.started", () => {
dynamicFired++;
if (unsubDynamic) {
unsubDynamic();
}
});
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-dyn", occurredAt: new Date().toISOString() }
});
expect(onceFired).toBe(1);
expect(dynamicFired).toBe(1);
expect(bus.listenerCount("runtime.started")).toBe(0);
// Emit again: should not fire anything
bus.emit({
name: "runtime.started",
payload: { runtimeId: "rt-dyn", occurredAt: new Date().toISOString() }
});
expect(onceFired).toBe(1);
expect(dynamicFired).toBe(1);
});
});
describe("InMemoryMemoryStore Eviction & High-Load Capacity", () => {
it("strictly bounds memory entries and evicts FIFO under 15,000 appends", async () => {
const store = new InMemoryMemoryStore({
maxEntries: 200,
maxEntriesPerAgent: 50
});
const totalAppends = 15_000;
for (let i = 0; i < totalAppends; i++) {
await store.append({
agentId: `agent-${i % 10}`,
taskId: `task-${i}`,
input: `Input query ${i}`,
output: { resultIndex: i },
recordedAt: new Date().toISOString()
});
}
const size = store.size;
expect(size).toBeLessThanOrEqual(200);
// Verify each agent does not exceed per-agent cap of 50
for (let a = 0; a < 10; a++) {
const agentEntries = await store.listByAgent(`agent-${a}`);
expect(agentEntries.length).toBeLessThanOrEqual(50);
}
// Pagination test
const page1 = await store.listByAgent("agent-0", {
limit: 10,
offset: 0
});
expect(page1.length).toBe(10);
// Clear test
await store.clear();
expect(store.size).toBe(0);
});
});
describe("InMemoryRuntimeStateStore & Logger Bounded Capacity", () => {
it("bounds state store keys under high throughput", async () => {
const stateStore = new InMemoryRuntimeStateStore({ maxEntries: 100 });
for (let i = 0; i < 5_000; i++) {
await stateStore.put(`key-${i}`, { val: i });
}
expect(await stateStore.size()).toBe(100);
expect(await stateStore.has("key-4999")).toBe(true);
expect(await stateStore.has("key-0")).toBe(false); // Evicted FIFO
await stateStore.delete("key-4999");
expect(await stateStore.size()).toBe(99);
await stateStore.clear();
expect(await stateStore.size()).toBe(0);
});
it("bounds in-memory logger entries under continuous logging", () => {
const logger = new InMemoryRuntimeLogger({ maxEntries: 50 });
for (let i = 0; i < 2_000; i++) {
logger.info(`Log message ${i}`);
}
expect(logger.size()).toBe(50);
expect(logger.entries[49]?.message).toBe("Log message 1999");
logger.clear();
expect(logger.size()).toBe(0);
});
});
describe("AgentInstanceManager Ephemeral Capacity", () => {
it("caps agent instance map size to avoid memory leaks from unbounded ephemeral agents", () => {
const manager = new AgentInstanceManager({ maxInstances: 50 });
for (let i = 0; i < 1_000; i++) {
manager.getOrCreate(`ephemeral-agent-${i}`);
}
expect(manager.size()).toBe(50);
expect(manager.has("ephemeral-agent-999")).toBe(true);
expect(manager.has("ephemeral-agent-0")).toBe(false);
manager.clear();
expect(manager.size()).toBe(0);
});
});
describe("AgentRuntime Teardown & In-Flight Draining", () => {
it("gracefully waits for in-flight tasks when draining during stop", async () => {
const runtime = new AgentRuntime({ runtimeId: "rt-drain" });
runtime.registerTool({
name: "work",
description: "Simulate async work",
execute: async () => {
await new Promise((r) => setTimeout(r, 40));
return { done: true };
}
});
await runtime.start();
expect(runtime.isRunning()).toBe(true);
const taskPromise = runtime.executeTask({
taskId: "task-drain-1",
agentId: "agent-drain",
toolName: "work",
input: "run work",
payload: {}
});
expect(runtime.getInFlightTaskCount()).toBe(1);
// Stop with drain timeout
await runtime.stop({ drainTimeoutMs: 200, clearListeners: true });
const result = await taskPromise;
expect(result.output).toEqual({ done: true });
expect(runtime.isRunning()).toBe(false);
expect(runtime.getInFlightTaskCount()).toBe(0);
});
});
});