-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
fetch-tcp-stress.test.ts
158 lines (145 loc) · 3.49 KB
/
fetch-tcp-stress.test.ts
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
// If port exhaustion occurs, these tests fail.
// These tests fail by timing out.
import { expect, test } from "bun:test";
import { getMaxFD, isMacOS } from "harness";
// Since we bumped MAX_CONNECTIONS to 4, we should halve the threshold on macOS.
const PORT_EXHAUSTION_THRESHOLD = isMacOS ? 8 * 1024 : 16 * 1024;
async function runStressTest({
onServerWritten,
onFetchWritten,
}: {
onServerWritten: (socket) => void;
onFetchWritten: (socket) => void;
}) {
const total = PORT_EXHAUSTION_THRESHOLD * 2;
let sockets = [];
const batch = 48;
let toClose = 0;
let pendingClose = Promise.withResolvers();
const objects = [];
for (let i = 0; i < total; i++) {
objects.push({
method: "POST",
body: "--BYTEMARKER: " + (10 + i) + " ",
keepalive: false,
});
}
const server = await Bun.listen({
port: 0,
socket: {
open(socket) {},
data(socket, data) {
const text = new TextDecoder().decode(data);
const i = parseInt(text.slice(text.indexOf("--BYTEMARKER: ") + "--BYTEMARKER: ".length).slice(0, 3)) - 10;
if (text.includes(objects[i].body)) {
socket.data ??= {};
socket.data.read = true;
sockets[i] = socket;
if (socket.write("200 OK\r\nCo") === "200 OK\r\nCo".length) {
socket.data.written = true;
onServerWritten(socket);
}
return;
}
console.log("Data is missing!");
},
drain(socket) {
if (!socket.data?.read || socket.data?.written) {
return;
}
if (socket.write("200 OK\r\nCo") === "200 OK\r\nCo".length) {
socket.data.written = true;
onServerWritten(socket);
}
},
error(socket, err) {
console.log(err);
},
timeout() {},
close(socket) {
toClose--;
if (toClose === 0) {
pendingClose.resolve();
}
},
},
hostname: "127.0.0.1",
});
let initialMaxFD = -1;
for (let remaining = total; remaining > 0; remaining -= batch) {
pendingClose = Promise.withResolvers();
{
const promises = [];
toClose = batch;
for (let i = 0; i < batch; i++) {
promises.push(
fetch(`http://127.0.0.1:${server.port}`, objects[i]).finally(() => {
onFetchWritten(sockets[i]);
}),
);
}
await Promise.allSettled(promises);
promises.length = 0;
}
await pendingClose.promise;
if (total) sockets = [];
if (initialMaxFD === -1) {
initialMaxFD = getMaxFD();
}
}
server.stop(true);
await Bun.sleep(10);
expect(getMaxFD()).toBeLessThan(initialMaxFD + 10);
}
test(
"shutdown after timeout",
async () => {
await runStressTest({
onServerWritten(socket) {
socket.end();
},
onFetchWritten(socket) {},
});
},
30 * 1000,
);
test(
"close after TCP fin",
async () => {
await runStressTest({
onServerWritten(socket) {
socket.shutdown();
},
onFetchWritten(socket) {
socket.end();
},
});
},
30 * 1000,
);
test(
"shutdown then terminate",
async () => {
await runStressTest({
onServerWritten(socket) {
socket.shutdown();
},
onFetchWritten(socket) {
socket.terminate();
},
});
},
30 * 1000,
);
test(
"gently close",
async () => {
await runStressTest({
onServerWritten(socket) {
socket.end();
},
onFetchWritten(socket) {},
});
},
30 * 1000,
);