tests: add basic connection test
I created a utility class to spawn a NATS server. This assumes it is installed and in PATH, which should be easy enough to accomplish for a CI environment. The current approach will not work with parallel tests, but that's not a practical reality, so this route should be fine for the time being. It might be nice to spawn a default server at the beginning of testing and tear it down at the end, to save waiting for the startup/shutdown time in many individual tests. This makes me wonder: if I initialize a server in the beginning of the `test` block in main that imports the other modules, does that scope stay live while the "child" tests are running? My default guess would be probably not, but that would be very convenient, so I might try it out and see.
This commit is contained in:
parent
d3d5849f55
commit
1c3e5ff538
18
tests/connection.zig
Normal file
18
tests/connection.zig
Normal file
@ -0,0 +1,18 @@
|
||||
const std = @import("std");
|
||||
|
||||
const nats = @import("nats");
|
||||
|
||||
const util = @import("./util.zig");
|
||||
|
||||
test "nats.Connection.connectTo" {
|
||||
var server = try util.TestServer.launch(.{});
|
||||
defer server.stop();
|
||||
|
||||
{
|
||||
try nats.init(nats.default_spin_count);
|
||||
defer nats.deinit();
|
||||
|
||||
const connection = try nats.Connection.connectTo(nats.default_server_url);
|
||||
defer connection.destroy();
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
test {
|
||||
_ = @import("./nats.zig");
|
||||
_ = @import("./connection.zig");
|
||||
_ = @import("./message.zig");
|
||||
}
|
||||
|
73
tests/util.zig
Normal file
73
tests/util.zig
Normal file
@ -0,0 +1,73 @@
|
||||
const std = @import("std");
|
||||
|
||||
const TestLaunchError = error{
|
||||
NoLaunchStringFound,
|
||||
};
|
||||
|
||||
pub const TestServer = struct {
|
||||
process: std.ChildProcess,
|
||||
|
||||
pub const LaunchOptions = struct {
|
||||
executable: []const u8 = "nats-server",
|
||||
port: u16 = 4222,
|
||||
auth: union(enum) {
|
||||
none: void,
|
||||
token: []const u8,
|
||||
password: struct { user: []const u8, pass: []const u8 },
|
||||
} = .none,
|
||||
allocator: std.mem.Allocator = std.testing.allocator,
|
||||
|
||||
fn argLen(self: LaunchOptions) usize {
|
||||
// executable, -a, 127.0.0.1, -p, 4222
|
||||
const base_len: usize = 5;
|
||||
return base_len + switch (self.auth) {
|
||||
.none => @as(usize, 0),
|
||||
.token => @as(usize, 2),
|
||||
.password => @as(usize, 4),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub fn launch(options: LaunchOptions) !TestServer {
|
||||
// const allocator = std.testing.allocator;
|
||||
var portbuf = [_]u8{0} ** 5;
|
||||
const strport = try std.fmt.bufPrint(&portbuf, "{d}", .{options.port});
|
||||
|
||||
const argsbuf: [9][]const u8 = blk: {
|
||||
const executable: [1][]const u8 = .{options.executable};
|
||||
const listen: [2][]const u8 = .{ "-a", "127.0.0.1" };
|
||||
const port: [2][]const u8 = .{ "-p", strport };
|
||||
const auth: [4][]const u8 = switch (options.auth) {
|
||||
.none => .{""} ** 4,
|
||||
.token => |tok| .{ "--auth", tok, "", "" },
|
||||
.password => |auth| .{ "--user", auth.user, "--password", auth.pass },
|
||||
};
|
||||
|
||||
break :blk executable ++ listen ++ port ++ auth;
|
||||
};
|
||||
|
||||
const args = argsbuf[0..options.argLen()];
|
||||
|
||||
var child = std.ChildProcess.init(args, options.allocator);
|
||||
child.stdin_behavior = .Ignore;
|
||||
child.stdout_behavior = .Pipe;
|
||||
child.stderr_behavior = .Pipe;
|
||||
|
||||
try child.spawn();
|
||||
var poller = std.io.poll(options.allocator, enum { stderr }, .{ .stderr = child.stderr.? });
|
||||
defer poller.deinit();
|
||||
|
||||
while (try poller.poll()) {
|
||||
if (std.mem.indexOf(u8, poller.fifo(.stderr).buf, "[INF] Server is ready")) |_| {
|
||||
return .{ .process = child };
|
||||
}
|
||||
}
|
||||
|
||||
_ = try child.kill();
|
||||
return error.NoLaunchStringFound;
|
||||
}
|
||||
|
||||
pub fn stop(self: *TestServer) void {
|
||||
_ = self.process.kill() catch return;
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user