dev: speedup unit tests

Directly improve a couple slow tests (crash_handler actually generating crash
dumps on some systems, e.g. mine). The runWebApiTest prefers to poll for work
when possible rather than a blind sleep -> check loop. (e.g. if we have
websocket connections, prefer an http tick).

For me, it's 22s -> 17s.
This commit is contained in:
Karl Seguin committed 2026-09-18 15:17:39 +08:00
1 parent e3aa36f83a
commit e2299e2c05
4 files changed
+24 -10

No files matched your search

+12 -4
View File
@@ -20,6 +20,7 @@ const std = @import("std");
const lp = @import("lightpanda");
const js = @import("js/js.zig");
const Frame = @import("Frame.zig");
const Browser = @import("Browser.zig");
const Session = @import("Session.zig");
const HttpClient = @import("../network/HttpClient.zig");
@@ -521,9 +522,16 @@ test "Runner: networkidle notifies child frames" {
// A `.networkidle` wait resolves via `is_done` once the page is fully
// idle, which can happen before the 500ms idle-notification hold. Keep
// ticking (like the CDP serve loop does) until the notifications fire.
// ticking (like the CDP serve loop does) until the notifications fire,
// backdating each started hold so the test doesn't spend it.
const held_since = lp.datetime.milliTimestamp(.boot) -| 600;
var attempts: usize = 0;
while (frame._notified_network_idle != .done and attempts < 50) : (attempts += 1) {
const children = frame.child_frames.items;
for ([_]*Frame{ frame, children[0], children[1] }) |f| {
if (f._notified_network_idle == .triggered) f._notified_network_idle = .{ .triggered = held_since };
if (f._notified_network_almost_idle == .triggered) f._notified_network_almost_idle = .{ .triggered = held_since };
}
_ = try runner.tickForFrame(page.frame_id, 20, .{ .until = .networkidle });
lp.io.sleep(.fromMilliseconds(25), .awake) catch {};
}
@@ -611,9 +619,9 @@ test "Runner: waits out a throttled navigation" {
const http_client = &session.browser.http_client;
// Enable the per-host navigation throttle for this test only, and spend
// 127.0.0.1's slot so the navigation below has to wait ~300ms.
// 127.0.0.1's slot so the navigation below has to wait ~100ms.
const network = http_client.network;
network.rate_limiter = @import("../network/RateLimiter.zig").init(testing.allocator, 300, 1);
network.rate_limiter = @import("../network/RateLimiter.zig").init(testing.allocator, 100, 1);
defer {
network.rate_limiter.?.deinit();
network.rate_limiter = null;
@@ -631,7 +639,7 @@ test "Runner: waits out a throttled navigation" {
var runner = session.runner(.{});
try runner.waitForFrame(page.frame_id, 2000, .{ .until = .done });
const elapsed = lp.datetime.milliTimestamp(.boot) - start;
try testing.expectEqual(true, elapsed >= 250);
try testing.expectEqual(true, elapsed >= 80);
try testing.expectEqual(0, http_client.delayed_count);
const el = try runner.waitForSelector(page.frame_id, "#sel1", 10);
+1 -1
View File
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<meta charset="UTF-8">
<iframe loading="lazy" src="runner1.html?delay_ms=500"></iframe>
<iframe loading="lazy" src="runner1.html?delay_ms=150"></iframe>
<iframe src="about:blank"></iframe>
+2 -3
View File
@@ -474,9 +474,8 @@ fn testSignal(sig: std.posix.SIG, mode: SignalTestMode) !void {
return error.ForkFailed;
}
if (pid == 0) {
// Keep a regression from hanging the runner or writing a large core.
const limit: std.posix.rlimit = .{ .cur = 0, .max = 0 };
_ = std.c.setrlimit(.CORE, &limit);
// Keep a regression from hanging the runner or writing a core.
_ = std.os.linux.prctl(@intFromEnum(std.os.linux.PR.SET_DUMPABLE), 0, 0, 0, 0);
const default: std.posix.Sigaction = .{ .handler = .{ .handler = std.posix.SIG.DFL }, .mask = std.posix.sigemptyset(), .flags = 0 };
std.posix.sigaction(.ALRM, &default, null);
std.posix.sigaction(.PIPE, &default, null);
+9 -2
View File
@@ -465,7 +465,7 @@ fn runWebApiTest(test_file: [:0]const u8, timeout_ms: u32) !void {
return;
}
const sleep_ms: usize = switch (try runner.tickForFrame(page.frame_id, 20, .{ .until = .done })) {
.done => 20,
.done => @min(test_session.browser.msToNextTask() orelse 20, 20), // could be at BLOCKING_NESTING, so wait a bit more
.ok => |next_ms| @min(next_ms, 20),
};
@@ -477,7 +477,14 @@ fn runWebApiTest(test_file: [:0]const u8, timeout_ms: u32) !void {
return error.TestTimedOut;
}
wait_ms -= @intCast(ms_elapsed);
lp.io.sleep(.fromMilliseconds(@intCast(sleep_ms)), .awake) catch {};
// WebSocket connection doesn't count as pending work, but we much prefer
// waiting on on activity than a blind sleep.
const http_client = &test_session.browser.http_client;
const waited = http_client.activity().ws_conns > 0 and try http_client.tick(@intCast(sleep_ms));
if (waited == false) {
lp.io.sleep(.fromMilliseconds(@intCast(sleep_ms)), .awake) catch {};
}
}
}