-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy paththread-management.test.ts
More file actions
124 lines (111 loc) · 3.79 KB
/
thread-management.test.ts
File metadata and controls
124 lines (111 loc) · 3.79 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
/**
* Thread management integration tests.
*
* Tests worker thread functionality including:
* - Concurrent request handling across threads
* - Server resilience after errors
*/
import { suite, test, before, after } from 'node:test';
import { strictEqual } from 'node:assert/strict';
import { startHarper, teardownHarper, type ContextWithHarper } from '@harperfast/integration-testing';
suite('Thread Management', (ctx: ContextWithHarper) => {
before(async () => {
await startHarper(ctx, { config: {}, env: {} });
});
after(async () => {
await teardownHarper(ctx);
});
test('server handles concurrent requests across threads', async () => {
// Send multiple concurrent requests to verify thread handling
const requests = [];
for (let i = 0; i < 20; i++) {
requests.push(
fetch(ctx.harper.operationsAPIURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${Buffer.from(`${ctx.harper.admin.username}:${ctx.harper.admin.password}`).toString('base64')}`,
},
body: JSON.stringify({ operation: 'describe_all' }),
})
);
}
const responses = await Promise.all(requests);
for (const response of responses) {
strictEqual(response.status, 200, 'All concurrent requests should succeed');
}
});
test('server recovers from malformed requests without affecting subsequent requests', async () => {
// Send multiple malformed requests
const badRequests = [];
for (let i = 0; i < 5; i++) {
badRequests.push(
fetch(ctx.harper.operationsAPIURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${Buffer.from(`${ctx.harper.admin.username}:${ctx.harper.admin.password}`).toString('base64')}`,
},
body: 'not json',
})
);
}
const badResponses = await Promise.all(badRequests);
for (const response of badResponses) {
strictEqual(response.status, 400);
}
// Server should still handle good requests after bad ones
const goodRequests = [];
for (let i = 0; i < 5; i++) {
goodRequests.push(
fetch(ctx.harper.operationsAPIURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${Buffer.from(`${ctx.harper.admin.username}:${ctx.harper.admin.password}`).toString('base64')}`,
},
body: JSON.stringify({ operation: 'describe_all' }),
})
);
}
const goodResponses = await Promise.all(goodRequests);
for (const response of goodResponses) {
strictEqual(response.status, 200, 'Server should recover and handle valid requests');
}
});
test('server handles mixed concurrent valid and invalid requests', async () => {
// Mix of good and bad requests simultaneously
const requests = [];
for (let i = 0; i < 20; i++) {
if (i % 3 === 0) {
// Bad request
requests.push(
fetch(ctx.harper.operationsAPIURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${Buffer.from(`${ctx.harper.admin.username}:${ctx.harper.admin.password}`).toString('base64')}`,
},
body: 'invalid json',
}).then((r) => ({ status: r.status, expected: 400 }))
);
} else {
// Good request
requests.push(
fetch(ctx.harper.operationsAPIURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${Buffer.from(`${ctx.harper.admin.username}:${ctx.harper.admin.password}`).toString('base64')}`,
},
body: JSON.stringify({ operation: 'describe_all' }),
}).then((r) => ({ status: r.status, expected: 200 }))
);
}
}
const results = await Promise.all(requests);
for (const result of results) {
strictEqual(result.status, result.expected, `Expected ${result.expected}, got ${result.status}`);
}
});
});