Skip to content

Lightpanda: the headless browser designed for AI and automation

server tests: wait for loopback readiness on macOS

Navid EMADc9d79fc46b68
diff --git a/src/server/Server.zig b/src/server/Server.zig
index 0d6bed931..efc9346e4 100644
--- a/src/server/Server.zig
+++ b/src/server/Server.zig
@@ -2134,6 +2134,7 @@ const LoopTest = struct {
try posix.setsockopt(client, posix.SOL.SOCKET, posix.SO.RCVTIMEO, &timeout);
const before = self.server.http_connections.last;
+ try waitReadable(self.server.listener);
try self.server.accept(lp.datetime.milliTimestamp(.boot));
const node = self.server.http_connections.last orelse return error.NotAccepted;
@@ -2142,6 +2143,16 @@ const LoopTest = struct {
}
return .{ client, @fieldParentPtr("node", node) };
}
+
+ // macOS delivers loopback traffic asynchronously: right after connect()
+ // or write() returns, the peer's accept queue or read buffer can still be
+ // empty. Linux processes it inline, so these tests never waited there.
+ fn waitReadable(fd: posix.socket_t) !void {
+ var pfd = [_]posix.pollfd{.{ .fd = fd, .events = posix.POLL.IN, .revents = 0 }};
+ if (try posix.poll(&pfd, 1000) == 0) {
+ return error.Timeout;
+ }
+ }
};
// epoll and kqueue both report in the order things became ready, so making the
@@ -2151,13 +2162,14 @@ test "server: a shutdown in the same batch as a readable connection" {
var lt = try LoopTest.init();
defer lt.deinit();
- const client, _ = try lt.accept();
+ const client, const conn = try lt.accept();
defer sys_net.close(client);
// shutdown first...
lt.server.shutdown();
// ...then the request, so it lands behind it in the batch
try sys_net.writeAll(client, "GET /json/version HTTP/1.1\r\n\r\n");
+ try LoopTest.waitReadable(conn.socket);
// beginShutdown drops every http connection, so running it where it
// arrived left the rest of the batch pointing at a recycled (or freed)
@@ -2171,7 +2183,7 @@ test "server: an accept at the connection limit in the same batch as a readable
var lt = try LoopTest.init();
defer lt.deinit();
- const client, _ = try lt.accept();
+ const client, const conn = try lt.accept();
defer sys_net.close(client);
// one connection, and no room for another: the next accept has to make
@@ -2184,6 +2196,7 @@ test "server: an accept at the connection limit in the same batch as a readable
defer sys_net.close(second);
// ...then the request, so it lands behind it in the batch
try sys_net.writeAll(client, "GET /json/version HTTP/1.1\r\n\r\n");
+ try LoopTest.waitReadable(conn.socket);
// saturated() picks the idle connection to drop, which is the one the
// batch is still holding a pointer to. Its request comes first.
@@ -2256,7 +2269,8 @@ test "server: accepted sockets get TCP keepalive" {
var value: c_int = 0;
var len: posix.socklen_t = @sizeOf(c_int);
try testing.expectEqual(0, std.c.getsockopt(conn.socket, posix.SOL.SOCKET, posix.SO.KEEPALIVE, &value, &len));
- try testing.expectEqual(1, value);
+ // BSD reports the option bit (8), Linux reports 1
+ try testing.expect(value != 0);
http.disconnect(lt.server, conn);
}
@@ -2287,6 +2301,7 @@ test "server: http connections stay ordered by deadline" {
const client_a, const conn_a = try lt.accept();
defer sys_net.close(client_a);
try sys_net.writeAll(client_a, "GET /json/version HTTP/1.1\r\n\r\n");
+ try LoopTest.waitReadable(conn_a.socket);
const readable: IOEvent.ReadWrite = .{ .target = .{ .http = conn_a }, .readable = true, .writable = false, .hangup = false };
http.processEvent(lt.server, conn_a, readable, lp.datetime.milliTimestamp(.boot));