Skip to content

Commit 687f09d

Browse files
committed
Make the App own the Platform
Removes optional platform, which only existed for tests. There is now a global `@import("testing.zig").test_app` available. This is setup when the test runner starts, and cleaned up at the end of tests. Individual tests don't have to worry about creating app, which I assume was the reason I Platform optional, since that woul dhave been something else that needed to be setup.
1 parent 67b479b commit 687f09d

File tree

10 files changed

+63
-74
lines changed

10 files changed

+63
-74
lines changed

src/app.zig

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const Notification = @import("notification.zig").Notification;
1414
pub const App = struct {
1515
http: Http,
1616
config: Config,
17-
platform: ?*const Platform,
17+
platform: Platform,
1818
allocator: Allocator,
1919
telemetry: Telemetry,
2020
app_dir_path: ?[]const u8,
@@ -29,7 +29,6 @@ pub const App = struct {
2929

3030
pub const Config = struct {
3131
run_mode: RunMode,
32-
platform: ?*const Platform = null,
3332
tls_verify_host: bool = true,
3433
http_proxy: ?[:0]const u8 = null,
3534
proxy_bearer_token: ?[:0]const u8 = null,
@@ -57,13 +56,16 @@ pub const App = struct {
5756
});
5857
errdefer http.deinit();
5958

59+
const platform = try Platform.init();
60+
errdefer platform.deinit();
61+
6062
const app_dir_path = getAndMakeAppDir(allocator);
6163

6264
app.* = .{
6365
.http = http,
6466
.allocator = allocator,
6567
.telemetry = undefined,
66-
.platform = config.platform,
68+
.platform = platform,
6769
.app_dir_path = app_dir_path,
6870
.notification = notification,
6971
.config = config,
@@ -85,6 +87,7 @@ pub const App = struct {
8587
self.telemetry.deinit();
8688
self.notification.deinit();
8789
self.http.deinit();
90+
self.platform.deinit();
8891
allocator.destroy(self);
8992
}
9093
};

src/browser/browser.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub const Browser = struct {
4848
pub fn init(app: *App) !Browser {
4949
const allocator = app.allocator;
5050

51-
const env = try Env.init(allocator, app.platform, .{});
51+
const env = try Env.init(allocator, &app.platform, .{});
5252
errdefer env.deinit();
5353

5454
const notification = try Notification.init(allocator, app.notification);

src/browser/html/elements.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ test "Browser.HTML.Element.DataSet" {
13471347
test "Browser.HTML.HtmlInputElement.properties" {
13481348
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io/noslashattheend" });
13491349
defer runner.deinit();
1350-
var alloc = std.heap.ArenaAllocator.init(runner.app.allocator);
1350+
var alloc = std.heap.ArenaAllocator.init(runner.allocator);
13511351
defer alloc.deinit();
13521352
const arena = alloc.allocator();
13531353

src/cdp/testing.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ const TestCDP = main.CDPT(struct {
7171
});
7272

7373
const TestContext = struct {
74-
app: *App,
7574
client: ?Client = null,
7675
cdp_: ?TestCDP = null,
7776
arena: ArenaAllocator,
@@ -80,7 +79,6 @@ const TestContext = struct {
8079
if (self.cdp_) |*c| {
8180
c.deinit();
8281
}
83-
self.app.deinit();
8482
self.arena.deinit();
8583
}
8684

@@ -89,7 +87,7 @@ const TestContext = struct {
8987
self.client = Client.init(self.arena.allocator());
9088
// Don't use the arena here. We want to detect leaks in CDP.
9189
// The arena is only for test-specific stuff
92-
self.cdp_ = TestCDP.init(self.app, &self.client.?) catch unreachable;
90+
self.cdp_ = TestCDP.init(base.test_app, &self.client.?) catch unreachable;
9391
}
9492
return &self.cdp_.?;
9593
}
@@ -221,7 +219,6 @@ const TestContext = struct {
221219

222220
pub fn context() TestContext {
223221
return .{
224-
.app = App.init(std.testing.allocator, .{ .run_mode = .serve }) catch unreachable,
225222
.arena = ArenaAllocator.init(std.testing.allocator),
226223
};
227224
}

src/main.zig

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,9 @@ fn run(alloc: Allocator) !void {
109109
log.opts.filter_scopes = lfs;
110110
}
111111

112-
const platform = try Platform.init();
113-
defer platform.deinit();
114-
115112
// _app is global to handle graceful shutdown.
116113
_app = try App.init(alloc, .{
117114
.run_mode = args.mode,
118-
.platform = &platform,
119115
.http_proxy = args.httpProxy(),
120116
.proxy_bearer_token = args.proxyBearerToken(),
121117
.tls_verify_host = args.tlsVerifyHost(),
@@ -124,6 +120,7 @@ fn run(alloc: Allocator) !void {
124120
.http_max_host_open = args.httpMaxHostOpen(),
125121
.http_max_concurrent = args.httpMaxConcurrent(),
126122
});
123+
127124
const app = _app.?;
128125
defer app.deinit();
129126
app.telemetry.record(.{ .run = {} });
@@ -715,48 +712,46 @@ fn parseCommonArg(
715712
return false;
716713
}
717714

715+
const testing = @import("testing.zig");
718716
test {
719717
std.testing.refAllDecls(@This());
720718
}
721719

722-
var test_wg: std.Thread.WaitGroup = .{};
723720
test "tests:beforeAll" {
724-
try parser.init();
725721
log.opts.level = .err;
726722
log.opts.format = .logfmt;
727723

728-
test_wg.startMany(2);
729-
const platform = try Platform.init();
724+
try testing.setup();
725+
726+
var wg: std.Thread.WaitGroup = .{};
727+
wg.startMany(2);
730728

731729
{
732-
const address = try std.net.Address.parseIp("127.0.0.1", 9582);
733-
const thread = try std.Thread.spawn(.{}, serveHTTP, .{address});
730+
const thread = try std.Thread.spawn(.{}, serveHTTP, .{&wg});
734731
thread.detach();
735732
}
736733

737734
{
738-
const address = try std.net.Address.parseIp("127.0.0.1", 9583);
739-
const thread = try std.Thread.spawn(.{}, serveCDP, .{ address, &platform });
735+
const thread = try std.Thread.spawn(.{}, serveCDP, .{&wg});
740736
thread.detach();
741737
}
742738

743739
// need to wait for the servers to be listening, else tests will fail because
744740
// they aren't able to connect.
745-
test_wg.wait();
741+
wg.wait();
746742
}
747743

748744
test "tests:afterAll" {
749-
parser.deinit();
745+
testing.shutdown();
750746
}
751747

752-
fn serveHTTP(address: std.net.Address) !void {
753-
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
754-
defer arena.deinit();
748+
fn serveHTTP(wg: *std.Thread.WaitGroup) !void {
749+
const address = try std.net.Address.parseIp("127.0.0.1", 9582);
755750

756751
var listener = try address.listen(.{ .reuse_address = true });
757752
defer listener.deinit();
758753

759-
test_wg.finish();
754+
wg.finish();
760755

761756
var read_buffer: [1024]u8 = undefined;
762757
while (true) {
@@ -799,19 +794,12 @@ fn serveHTTP(address: std.net.Address) !void {
799794
}
800795
}
801796

802-
fn serveCDP(address: std.net.Address, platform: *const Platform) !void {
803-
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
804-
var app = try App.init(gpa.allocator(), .{
805-
.run_mode = .serve,
806-
.tls_verify_host = false,
807-
.platform = platform,
808-
.http_max_concurrent = 2,
809-
});
810-
defer app.deinit();
797+
fn serveCDP(wg: *std.Thread.WaitGroup) !void {
798+
const address = try std.net.Address.parseIp("127.0.0.1", 9583);
811799

812-
test_wg.finish();
813-
var server = try Server.init(app, address);
800+
var server = try Server.init(testing.test_app, address);
814801
defer server.deinit();
802+
wg.finish();
815803
server.run(address, 5) catch |err| {
816804
std.debug.print("CDP server error: {}", .{err});
817805
return err;

src/main_wpt.zig

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ pub fn main() !void {
4848

4949
const cmd = try parseArgs(runner_arena);
5050

51-
const platform = try Platform.init();
52-
defer platform.deinit();
51+
try @import("testing.zig").setup();
52+
defer @import("testing.zig").shutdown();
5353

5454
// prepare libraries to load on each test case.
5555
var loader = FileLoader.init(runner_arena, WPT_DIR);
@@ -69,7 +69,6 @@ pub fn main() !void {
6969
var err_out: ?[]const u8 = null;
7070
const result = run(
7171
test_arena.allocator(),
72-
&platform,
7372
test_file,
7473
&loader,
7574
&err_out,
@@ -81,20 +80,18 @@ pub fn main() !void {
8180
};
8281

8382
if (result == null and err_out == null) {
84-
// We somtimes pass a non-test to `run` (we don't know it's a non
83+
// We sometimes pass a non-test to `run` (we don't know it's a non
8584
// test, we need to open the contents of the test file to find out
8685
// and that's in run).
8786
continue;
8887
}
89-
9088
try writer.process(test_file, result, err_out);
9189
}
9290
try writer.finalize();
9391
}
9492

9593
fn run(
9694
arena: Allocator,
97-
platform: *const Platform,
9895
test_file: []const u8,
9996
loader: *FileLoader,
10097
err_out: *?[]const u8,
@@ -119,10 +116,15 @@ fn run(
119116
var runner = try @import("testing.zig").jsRunner(arena, .{
120117
.url = "http://127.0.0.1",
121118
.html = html,
122-
.platform = platform,
123119
});
124120
defer runner.deinit();
125121

122+
defer if (err_out.*) |eo| {
123+
// the error might be owned by the runner, we'll dupe it with our
124+
// own arena so that it can be returned out of this function.
125+
err_out.* = arena.dupe(u8, eo) catch "failed to dupe error";
126+
};
127+
126128
try polyfill.preload(arena, runner.page.main_context);
127129

128130
// loop over the scripts.
@@ -179,6 +181,7 @@ fn run(
179181

180182
// return the detailed result.
181183
const res = try runner.eval("report.log", "report", err_out);
184+
182185
return try res.toString(arena);
183186
}
184187

src/runtime/js.zig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
153153
return struct {
154154
allocator: Allocator,
155155

156-
platform: ?*const Platform,
156+
platform: *const Platform,
157157

158158
// the global isolate
159159
isolate: v8.Isolate,
@@ -182,7 +182,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
182182

183183
const Opts = struct {};
184184

185-
pub fn init(allocator: Allocator, platform: ?*const Platform, _: Opts) !*Self {
185+
pub fn init(allocator: Allocator, platform: *const Platform, _: Opts) !*Self {
186186
// var params = v8.initCreateParams();
187187
var params = try allocator.create(v8.CreateParams);
188188
errdefer allocator.destroy(params);
@@ -301,13 +301,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
301301
}
302302

303303
pub fn pumpMessageLoop(self: *const Self) bool {
304-
// assume it's not-null.
305-
return self.platform.?.inner.pumpMessageLoop(self.isolate, false);
304+
return self.platform.inner.pumpMessageLoop(self.isolate, false);
306305
}
307306

308307
pub fn runIdleTasks(self: *const Self) void {
309-
// assume it's not-null.
310-
return self.platform.?.inner.runIdleTasks(self.isolate, 1);
308+
return self.platform.inner.runIdleTasks(self.isolate, 1);
311309
}
312310

313311
pub fn newExecutionWorld(self: *Self) !ExecutionWorld {

src/runtime/testing.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
const std = @import("std");
2020
const js = @import("js.zig");
21+
const base = @import("../testing.zig");
2122
const generate = @import("generate.zig");
2223

2324
pub const allocator = std.testing.allocator;
@@ -42,7 +43,7 @@ pub fn Runner(comptime State: type, comptime Global: type, comptime types: anyty
4243
const self = try allocator.create(Self);
4344
errdefer allocator.destroy(self);
4445

45-
self.env = try Env.init(allocator, null, .{});
46+
self.env = try Env.init(allocator, &base.test_app.platform, .{});
4647
errdefer self.env.deinit();
4748

4849
self.executor = try self.env.newExecutionWorld();

src/telemetry/telemetry.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,7 @@ test "telemetry: getOrCreateId" {
186186
}
187187

188188
test "telemetry: sends event to provider" {
189-
var app = testing.createApp(.{});
190-
defer app.deinit();
191-
192-
var telemetry = try TelemetryT(MockProvider).init(app, .serve);
189+
var telemetry = try TelemetryT(MockProvider).init(testing.test_app, .serve);
193190
defer telemetry.deinit();
194191
const mock = &telemetry.provider;
195192

0 commit comments

Comments
 (0)