diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 13:06:17 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 13:13:37 -0300 |
| commit | 8c32753594ce0d6852336206b86cbdd88a8878b1 (patch) | |
| tree | 5e4c0c2f8db9a40e3dfaa8963920a2a20f2620b9 | |
| parent | f44744d8bfce2f94cae9a658c416a3c2a0364a6a (diff) | |
| download | cloud9-8c32753594ce0d6852336206b86cbdd88a8878b1.tar.gz cloud9-8c32753594ce0d6852336206b86cbdd88a8878b1.zip | |
test: a consumer package, run by the library's own test step
cloud9's build is used from other build.zig files (pardes, sketchbook),
and nothing in its own suite used it that way: a break in what a
consumer writes was found by the consumer. test/consumer is a package
of its own whose manifest names cloud9 by ../.. and whose build.zig
takes both exported modules by name with no option,
b.dependency("cloud9", .{}).module("cloud9") and .module("9proc"),
for its own target and optimize. Its test is a client and a server,
each a cloud9 session (Client, Server) on its own end of a Unix socket
pair, through a version, an attach and a read; and a call into 9proc,
which shares the consumer's cloud9 module. The library's test step runs
it as a nested zig build test in test/consumer, as pardes's lib-test
runs test/lib (the consumer depends back on cloud9, so it cannot be a
dependency of it). It has its own .zig-cache there (39 MB after a run).
build.zig.zon's paths already carry test/ and every directory build.zig
imports, so a fetched copy builds as before: unchanged.
zig build test: the library's 108 of 108 and test/consumer's 2 of 2 at
load 3.6 (at load 14, 107: serve's "repeated connect/disconnect", which
fails on the base under load too, from the same test binary); a warm
zig build test, the nested one included, 42-50 ms. 9proc-test and
9proc-check-freestanding pass: 9proc's library test binary is the
base's (one cache key), so the first change's copy of the module is the
module it was.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | build.zig | 9 | ||||
| -rw-r--r-- | test/consumer/build.zig | 20 | ||||
| -rw-r--r-- | test/consumer/build.zig.zon | 10 | ||||
| -rw-r--r-- | test/consumer/main.zig | 82 |
4 files changed, 120 insertions, 1 deletions
@@ -23,7 +23,14 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); const tests = b.addTest(.{ .root_module = module }); - b.step("test", "Run protocol and session tests").dependOn(&b.addRunArtifact(tests).step); + const test_step = b.step("test", "Run protocol and session tests, and test/consumer's"); + test_step.dependOn(&b.addRunArtifact(tests).step); + // The build API as a package that depends on cloud9 sees it: + // test/consumer's own `zig build test`. + const consumer = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test" }); + consumer.setCwd(b.path("test/consumer")); + consumer.has_side_effects = true; + test_step.dependOn(&consumer.step); const wasm_target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }); const wasm_core = b.createModule(.{ .root_source_file = b.path("src/root.zig"), diff --git a/test/consumer/build.zig b/test/consumer/build.zig new file mode 100644 index 0000000..9fc2c77 --- /dev/null +++ b/test/consumer/build.zig @@ -0,0 +1,20 @@ +//! A package that depends on cloud9 as any other does: cloud9's `test` +//! step runs `zig build test` here, so its build API is used the way a +//! consumer uses it (modules by name, no option). +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const cloud9 = b.dependency("cloud9", .{}); + const tests = b.addTest(.{ .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "cloud9", .module = cloud9.module("cloud9") }, + .{ .name = "9proc", .module = cloud9.module("9proc") }, + }, + }) }); + b.step("test", "a client and a server on cloud9 talk over a socket pair").dependOn(&b.addRunArtifact(tests).step); +} diff --git a/test/consumer/build.zig.zon b/test/consumer/build.zig.zon new file mode 100644 index 0000000..088a635 --- /dev/null +++ b/test/consumer/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = .cloud9_consumer, + .version = "0.0.0", + .dependencies = .{ + .cloud9 = .{ .path = "../.." }, + }, + .minimum_zig_version = "0.17.0", + .paths = .{""}, + .fingerprint = 0x7babe157bcd22cee, +} diff --git a/test/consumer/main.zig b/test/consumer/main.zig new file mode 100644 index 0000000..3dc555d --- /dev/null +++ b/test/consumer/main.zig @@ -0,0 +1,82 @@ +//! cloud9 from a package that depends on it: a client and a server, each +//! a cloud9 session over its own end of a socket pair, and 9proc. +const std = @import("std"); +const c9 = @import("cloud9"); +const testing = std.testing; +const File = std.Io.File; + +test "a client and a server talk 9P over a socket pair" { + if (@import("builtin").os.tag != .linux) return error.SkipZigTest; + const linux = std.os.linux; + var fds: [2]linux.fd_t = undefined; + try testing.expectEqual(0, linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM, 0, &fds)); + const near: File = .{ .handle = fds[0], .flags = .{ .nonblocking = false } }; + const far: File = .{ .handle = fds[1], .flags = .{ .nonblocking = false } }; + defer near.close(testing.io); + defer far.close(testing.io); + + var bufs: [4][8192]u8 = undefined; + var client: c9.Client = .init(.{ .in = &bufs[0], .out = &bufs[1] }); + var server: c9.Server = .init(.{ .in = &bufs[2], .out = &bufs[3] }); + const root: c9.Qid = .{ .type = c9.qtdir, .version = 0, .path = 1 }; + const hello = "hello from the server"; + + _ = try client.submit(.{ .version = .{} }); + try send(near, &client); + const version = (try request(far, &server)).msg.tversion; + try server.negotiate(version.msize, version.version); + server.release(); + try send(far, &server); + try testing.expectEqualStrings("9P2000", (try result(near, &client)).result.version.version); + + _ = try client.submit(.{ .attach = .{ .fid = 0, .uname = "glenda" } }); + try send(near, &client); + const attach = try request(far, &server); + try testing.expectEqualStrings("glenda", attach.msg.tattach.uname); + try server.reply(attach.tag, .{ .rattach = .{ .qid = root } }); + server.release(); + try send(far, &server); + try testing.expectEqual(root.path, (try result(near, &client)).result.attach.path); + + _ = try client.submit(.{ .read = .{ .fid = 0, .offset = 0, .count = 64 } }); + try send(near, &client); + const read = try request(far, &server); + try server.reply(read.tag, .{ .rread = .{ .data = hello } }); + server.release(); + try send(far, &server); + try testing.expectEqualStrings(hello, (try result(near, &client)).result.read); +} + +test "9proc comes from the same dependency, on the same cloud9" { + try @import("9proc").core.validName("pane"); + try testing.expectError(error.BadName, @import("9proc").core.validName("a/b")); +} + +/// Writes what `end` (a client or a server) has to say on `file`. +fn send(file: File, end: anytype) !void { + const bytes = end.output(); + try file.writeStreamingAll(testing.io, bytes); + end.wrote(bytes.len); +} + +/// Reads from `file` into `server` until it holds a whole request. +fn request(file: File, server: *c9.Server) !c9.Decoded { + while (true) { + if (try server.receive()) |got| return got; + try recv(file, server); + } +} + +/// Reads from `file` into `client` until it holds a whole answer. +fn result(file: File, client: *c9.Client) !c9.Client.Done { + while (true) { + if (client.take()) |done| return done; + try recv(file, client); + } +} + +fn recv(file: File, end: anytype) !void { + var buf: [8192]u8 = undefined; + const n = try file.readStreaming(testing.io, &.{&buf}); + try testing.expectEqual(n, end.push(buf[0..n])); +} |
