Compare commits
No commits in common. "60632f62ad2ba969d3aa100426297a76f072ed5c" and "64c1a1604df839a53b77ea52f119dc76a310651a" have entirely different histories.
60632f62ad
...
64c1a1604d
@ -19,7 +19,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const tests = b.addTest(.{
|
const tests = b.addTest(.{
|
||||||
.name = "nats-zig-unit-tests",
|
|
||||||
.root_source_file = .{ .path = "tests/main.zig" },
|
.root_source_file = .{ .path = "tests/main.zig" },
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
@ -30,7 +29,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
|
|
||||||
const run_main_tests = b.addRunArtifact(tests);
|
const run_main_tests = b.addRunArtifact(tests);
|
||||||
const test_step = b.step("test", "Run tests");
|
const test_step = b.step("test", "Run tests");
|
||||||
test_step.dependOn(&b.addInstallArtifact(tests, .{}).step);
|
|
||||||
test_step.dependOn(&run_main_tests.step);
|
test_step.dependOn(&run_main_tests.step);
|
||||||
|
|
||||||
add_examples(b, .{
|
add_examples(b, .{
|
||||||
|
16
src/nats.zig
16
src/nats.zig
@ -114,8 +114,7 @@ pub fn sign(encoded_seed: [:0]const u8, input: [:0]const u8) Error![]const u8 {
|
|||||||
|
|
||||||
// Note: an "Inbox" is actually just a string. This API creates a random (unique)
|
// Note: an "Inbox" is actually just a string. This API creates a random (unique)
|
||||||
// string suitable for passing as the `reply` field to Message.create or
|
// string suitable for passing as the `reply` field to Message.create or
|
||||||
// Connection.publishRequest. The string is owned by the caller and should be freed
|
// Connection.publishRequest.
|
||||||
// using `destroyInbox`.
|
|
||||||
pub fn createInbox() Error![:0]u8 {
|
pub fn createInbox() Error![:0]u8 {
|
||||||
var self: [*c]u8 = undefined;
|
var self: [*c]u8 = undefined;
|
||||||
const status = Status.fromInt(nats_c.natsInbox_Create(@ptrCast(&self)));
|
const status = Status.fromInt(nats_c.natsInbox_Create(@ptrCast(&self)));
|
||||||
@ -123,19 +122,12 @@ pub fn createInbox() Error![:0]u8 {
|
|||||||
return status.toError() orelse std.mem.sliceTo(self, 0);
|
return status.toError() orelse std.mem.sliceTo(self, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn destroyInbox(inbox: [:0]u8) void {
|
pub fn destroyInbox(inbox: [:0]const u8) void {
|
||||||
nats_c.natsInbox_Destroy(@ptrCast(inbox.ptr));
|
nats_c.natsInbox_Destroy(@constCast(@ptrCast(inbox.ptr)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// I think this is also a jetstream API. This function sure does not seem at all useful
|
// I think this is also a jetstream API. This function sure does not seem at all useful
|
||||||
// by itself. Note: for some reason, most of the jetstream data structures are all
|
// by itself.
|
||||||
// public, instead of following the opaque handle style that the rest of the library
|
|
||||||
// does.
|
|
||||||
|
|
||||||
// typedef struct natsMsgList {
|
|
||||||
// natsMsg **Msgs;
|
|
||||||
// int Count;
|
|
||||||
// } natsMsgList;
|
|
||||||
pub const MessageList = opaque {
|
pub const MessageList = opaque {
|
||||||
pub fn destroy(self: *MessageList) void {
|
pub fn destroy(self: *MessageList) void {
|
||||||
nats_c.natsMsgList_Destroy(@ptrCast(self));
|
nats_c.natsMsgList_Destroy(@ptrCast(self));
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
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,5 +1,4 @@
|
|||||||
test {
|
test {
|
||||||
_ = @import("./nats.zig");
|
_ = @import("./nats.zig");
|
||||||
_ = @import("./connection.zig");
|
|
||||||
_ = @import("./message.zig");
|
_ = @import("./message.zig");
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const nats = @import("nats");
|
const nats = @import("nats");
|
||||||
|
|
||||||
|
// const nats = @import("../src/nats.zig");
|
||||||
|
// const message = @import("../src/message.zig");
|
||||||
|
|
||||||
test "message: create message" {
|
test "message: create message" {
|
||||||
const subject = "hello";
|
const subject = "hello";
|
||||||
const reply = "reply";
|
const reply = "reply";
|
||||||
@ -9,7 +11,7 @@ test "message: create message" {
|
|||||||
|
|
||||||
// have to initialize the library so the reference counter can correctly destroy
|
// have to initialize the library so the reference counter can correctly destroy
|
||||||
// objects, otherwise we segfault on trying to free the memory.
|
// objects, otherwise we segfault on trying to free the memory.
|
||||||
try nats.init(nats.default_spin_count);
|
try nats.init(-1);
|
||||||
defer nats.deinit();
|
defer nats.deinit();
|
||||||
|
|
||||||
const message = try nats.Message.create(subject, reply, data);
|
const message = try nats.Message.create(subject, reply, data);
|
||||||
@ -26,7 +28,7 @@ test "message: create message" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test "message: get subject" {
|
test "message: get subject" {
|
||||||
try nats.init(nats.default_spin_count);
|
try nats.init(-1);
|
||||||
defer nats.deinit();
|
defer nats.deinit();
|
||||||
|
|
||||||
const subject = "hello";
|
const subject = "hello";
|
||||||
@ -38,7 +40,7 @@ test "message: get subject" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test "message: get reply" {
|
test "message: get reply" {
|
||||||
try nats.init(nats.default_spin_count);
|
try nats.init(-1);
|
||||||
defer nats.deinit();
|
defer nats.deinit();
|
||||||
|
|
||||||
const subject = "hello";
|
const subject = "hello";
|
||||||
|
@ -1,73 +0,0 @@
|
|||||||
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