Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
f99c71a750 test: add maxConnections close reopen test 2025-05-28 17:59:09 -07:00
2 changed files with 98 additions and 10 deletions

View File

@@ -89,6 +89,7 @@ function isIP(s): 0 | 4 | 6 {
const bunTlsSymbol = Symbol.for("::buntls::");
const bunSocketServerConnections = Symbol.for("::bunnetserverconnections::");
const bunSocketServerDropped = Symbol.for("::bunnetserverdropped::");
const bunSocketServerOptions = Symbol.for("::bunnetserveroptions::");
const owner_symbol = Symbol("owner_symbol");
@@ -342,16 +343,17 @@ const ServerHandlers: SocketHandler = {
const data = this.data;
if (!data) return;
data.server[bunSocketServerConnections]--;
{
if (!data[kclosed]) {
data[kclosed] = true;
//socket cannot be used after close
detachSocket(data);
SocketEmitEndNT(data, err);
data.data = null;
socket[owner_symbol] = null;
}
if (!data[bunSocketServerDropped]) {
data.server[bunSocketServerConnections]--;
}
if (!data[kclosed]) {
data[kclosed] = true;
//socket cannot be used after close
detachSocket(data);
SocketEmitEndNT(data, err);
data.data = null;
socket[owner_symbol] = null;
}
data.server._emitCloseIfDrained();
@@ -383,6 +385,7 @@ const ServerHandlers: SocketHandler = {
remotePort: _socket.remotePort,
remoteFamily: _socket.remoteFamily || "IPv4",
};
_socket[bunSocketServerDropped] = true;
socket.end();
self.emit("drop", data);
return;
@@ -398,6 +401,7 @@ const ServerHandlers: SocketHandler = {
remoteFamily: _socket.remoteFamily || "IPv4",
};
_socket[bunSocketServerDropped] = true;
socket.end();
self.emit("drop", data);

View File

@@ -0,0 +1,84 @@
require("../common");
const assert = require("assert");
const net = require("net");
// Sets the server's maxConnections property to 1.
// Open 2 connections (connection 0 and connection 1).
// Connection 0 should be accepted.
// Connection 1 should be rejected.
// Closes connection 0.
// Open 2 more connections (connection 2 and 3).
// Connection 2 should be accepted.
// Connection 3 should be rejected.
const connections = [];
const received = [];
const sent = [];
function createConnection(index) {
console.error(`creating connection ${index}`);
return new Promise(function (resolve, reject) {
const connection = net.createConnection(server.address().port, function () {
const msg = String(index);
console.error(`sending message: ${msg}`);
this.write(msg);
sent.push(msg);
});
connection.on("error", function (err) {
assert.strictEqual(err.code, "ECONNRESET");
resolve();
});
connection.on("data", function (e) {
console.error(`connection ${index} received response`);
resolve();
});
connection.on("end", function () {
console.error(`ending ${index}`);
resolve();
});
connections[index] = connection;
});
}
function closeConnection(index) {
console.error(`closing connection ${index}`);
return new Promise(function (resolve, reject) {
connections[index].on("end", function () {
resolve();
});
connections[index].end();
});
}
const server = net.createServer(function (socket) {
socket.on("data", function (data) {
console.error(`received message: ${data}`);
received.push(String(data));
socket.write("acknowledged");
});
});
server.maxConnections = 1;
server.listen(0, function () {
createConnection(0)
.then(createConnection.bind(null, 1))
.then(closeConnection.bind(null, 0))
.then(createConnection.bind(null, 2))
.then(createConnection.bind(null, 3))
.then(server.close.bind(server))
.then(closeConnection.bind(null, 2));
});
process.on("exit", function () {
// Confirm that all connections tried to send data...
assert.deepStrictEqual(sent, ["0", "1", "2", "3"]);
// ...but that only connections 0 and 2 were successful.
assert.deepStrictEqual(received, ["0", "2"]);
});