Compare commits
No commits in common. "40d898a55e489b18b45fc1f8b762995aa4095f5a" and "9a4c80861c4e7032ea1f2f3b105b3566fec595e6" have entirely different histories.
40d898a55e
...
9a4c80861c
24
build.zig
24
build.zig
@ -8,8 +8,15 @@ pub fn build(b: *std.Build) void {
|
|||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const nats = b.addModule("nats", .{
|
// const nats = b.addModule("nats", .{
|
||||||
.source_file = .{ .path = "src/nats.zig" },
|
// .source_file = .{ .path = "source/nats.zig" },
|
||||||
|
// });
|
||||||
|
|
||||||
|
const nats = b.addExecutable(.{
|
||||||
|
.name = "nats_test",
|
||||||
|
.root_source_file = .{ .path = "src/nats.zig" },
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
const nats_c = nats_build.nats_c_lib(
|
const nats_c = nats_build.nats_c_lib(
|
||||||
@ -17,17 +24,18 @@ pub fn build(b: *std.Build) void {
|
|||||||
.{ .name = "nats-c", .target = target, .optimize = optimize },
|
.{ .name = "nats-c", .target = target, .optimize = optimize },
|
||||||
);
|
);
|
||||||
|
|
||||||
const tests = b.addTest(.{
|
nats.linkLibrary(nats_c);
|
||||||
.root_source_file = .{ .path = "tests/main.zig" },
|
b.installArtifact(nats);
|
||||||
|
|
||||||
|
const main_tests = b.addTest(.{
|
||||||
|
.root_source_file = .{ .path = "src/nats.zig" },
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
tests.addModule("nats", nats);
|
main_tests.linkLibrary(nats_c);
|
||||||
tests.linkLibrary(nats_c);
|
|
||||||
|
|
||||||
b.installArtifact(tests);
|
const run_main_tests = b.addRunArtifact(main_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(&run_main_tests.step);
|
test_step.dependOn(&run_main_tests.step);
|
||||||
}
|
}
|
||||||
|
@ -19,33 +19,31 @@ pub fn nats_c_lib(
|
|||||||
.optimize = options.optimize,
|
.optimize = options.optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
const cflags = [_][]const u8{
|
lib.disable_sanitize_c = true;
|
||||||
"-fno-sanitize=undefined",
|
|
||||||
};
|
|
||||||
|
|
||||||
lib.linkLibC();
|
lib.linkLibC();
|
||||||
lib.addCSourceFiles(&common_sources, &cflags);
|
lib.addCSourceFiles(&common_sources, &.{"-fno-sanitize=undefined"});
|
||||||
lib.addIncludePath(.{ .path = nats_src_prefix ++ "include" });
|
lib.addIncludePath(.{ .path = nats_src_prefix ++ "include" });
|
||||||
// if building with streaming support
|
// if building with streaming support
|
||||||
// lib.addIncludePath(.{ .path = nats_src_prefix ++ "stan" });
|
// lib.addIncludePath(.{ .path = nats_src_prefix ++ "stan" });
|
||||||
// lib.addCSourceFiles(&streaming_sources, &cflags);
|
// lib.addCSourceFiles(&streaming_sources, &.{"-fno-sanitize=undefined"});
|
||||||
|
|
||||||
const tinfo = lib.target_info.target;
|
const tinfo = lib.target_info.target;
|
||||||
switch (tinfo.os.tag) {
|
switch (tinfo.os.tag) {
|
||||||
.windows => {
|
.windows => {
|
||||||
lib.addCSourceFiles(&win_sources, &cflags);
|
lib.addCSourceFiles(&win_sources, &.{"-fno-sanitize=undefined"});
|
||||||
if (tinfo.abi != .msvc) {
|
if (tinfo.abi != .msvc) {
|
||||||
lib.addCSourceFiles(&.{"src/win-crosshack.c"}, &cflags);
|
lib.addCSourceFiles(&.{"src/win-crosshack.c"}, &.{"-fno-sanitize=undefined"});
|
||||||
}
|
}
|
||||||
lib.defineCMacro("_WIN32", null);
|
lib.defineCMacro("_WIN32", null);
|
||||||
lib.linkSystemLibrary("ws2_32");
|
lib.linkSystemLibrary("ws2_32");
|
||||||
},
|
},
|
||||||
.macos => {
|
.macos => {
|
||||||
lib.addCSourceFiles(&unix_sources, &cflags);
|
lib.addCSourceFiles(&unix_sources, &.{"-fno-sanitize=undefined"});
|
||||||
lib.defineCMacro("DARWIN", null);
|
lib.defineCMacro("DARWIN", null);
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
lib.addCSourceFiles(&unix_sources, &cflags);
|
lib.addCSourceFiles(&unix_sources, &.{"-fno-sanitize=undefined"});
|
||||||
lib.defineCMacro("_GNU_SOURCE", null);
|
lib.defineCMacro("_GNU_SOURCE", null);
|
||||||
lib.defineCMacro("LINUX", null);
|
lib.defineCMacro("LINUX", null);
|
||||||
// may need to link pthread and rt. Not sure if those are inluded with linkLibC
|
// may need to link pthread and rt. Not sure if those are inluded with linkLibC
|
||||||
|
@ -113,3 +113,63 @@ pub const Message = opaque {
|
|||||||
// NATS_EXTERN const char* stanMsg_GetData(const stanMsg *msg);
|
// NATS_EXTERN const char* stanMsg_GetData(const stanMsg *msg);
|
||||||
// NATS_EXTERN int stanMsg_GetDataLength(const stanMsg *msg);
|
// NATS_EXTERN int stanMsg_GetDataLength(const stanMsg *msg);
|
||||||
// NATS_EXTERN void stanMsg_Destroy(stanMsg *msg);
|
// NATS_EXTERN void stanMsg_Destroy(stanMsg *msg);
|
||||||
|
|
||||||
|
test "message: create message" {
|
||||||
|
const subject = "hello";
|
||||||
|
const reply = "reply";
|
||||||
|
const data = "world";
|
||||||
|
|
||||||
|
const nats = @import("./nats.zig");
|
||||||
|
|
||||||
|
// have to initialize the library so the reference counter can correctly destroy
|
||||||
|
// objects, otherwise we segfault on trying to free the memory.
|
||||||
|
try nats.init(-1);
|
||||||
|
defer nats.deinit();
|
||||||
|
|
||||||
|
const message = try Message.create(subject, reply, data);
|
||||||
|
defer message.destroy();
|
||||||
|
|
||||||
|
const message2 = try Message.create(subject, null, data);
|
||||||
|
defer message2.destroy();
|
||||||
|
|
||||||
|
const message3 = try Message.create(subject, data, null);
|
||||||
|
defer message3.destroy();
|
||||||
|
|
||||||
|
const message4 = try Message.create(subject, null, null);
|
||||||
|
defer message4.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
test "message: get subject" {
|
||||||
|
const nats = @import("./nats.zig");
|
||||||
|
|
||||||
|
try nats.init(-1);
|
||||||
|
defer nats.deinit();
|
||||||
|
|
||||||
|
const subject = "hello";
|
||||||
|
const message = try Message.create(subject, null, null);
|
||||||
|
defer message.destroy();
|
||||||
|
|
||||||
|
const received = message.getSubject();
|
||||||
|
try std.testing.expectEqualStrings(subject, received);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "message: get reply" {
|
||||||
|
const nats = @import("./nats.zig");
|
||||||
|
|
||||||
|
try nats.init(-1);
|
||||||
|
defer nats.deinit();
|
||||||
|
|
||||||
|
const subject = "hello";
|
||||||
|
const reply = "reply";
|
||||||
|
const message = try Message.create(subject, reply, null);
|
||||||
|
defer message.destroy();
|
||||||
|
|
||||||
|
const received = message.getReply() orelse return error.TestUnexpectedResult;
|
||||||
|
try std.testing.expectEqualStrings(reply, received);
|
||||||
|
|
||||||
|
const message2 = try Message.create(subject, null, null);
|
||||||
|
defer message2.destroy();
|
||||||
|
|
||||||
|
const received2 = message2.getReply();
|
||||||
|
try std.testing.expect(received2 == null);
|
||||||
|
}
|
||||||
|
@ -158,3 +158,7 @@ pub const MessageList = opaque {
|
|||||||
nats_c.natsMsgList_Destroy(@ptrCast(self));
|
nats_c.natsMsgList_Destroy(@ptrCast(self));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
test {
|
||||||
|
std.testing.refAllDecls(@This());
|
||||||
|
}
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
|
|
||||||
test {
|
|
||||||
_ = @import("./message.zig");
|
|
||||||
}
|
|
@ -1,59 +0,0 @@
|
|||||||
const std = @import("std");
|
|
||||||
const nats = @import("nats");
|
|
||||||
|
|
||||||
// const nats = @import("../src/nats.zig");
|
|
||||||
// const message = @import("../src/message.zig");
|
|
||||||
|
|
||||||
test "message: create message" {
|
|
||||||
const subject = "hello";
|
|
||||||
const reply = "reply";
|
|
||||||
const data = "world";
|
|
||||||
|
|
||||||
// have to initialize the library so the reference counter can correctly destroy
|
|
||||||
// objects, otherwise we segfault on trying to free the memory.
|
|
||||||
try nats.init(-1);
|
|
||||||
defer nats.deinit();
|
|
||||||
|
|
||||||
const message = try nats.Message.create(subject, reply, data);
|
|
||||||
defer message.destroy();
|
|
||||||
|
|
||||||
const message2 = try nats.Message.create(subject, null, data);
|
|
||||||
defer message2.destroy();
|
|
||||||
|
|
||||||
const message3 = try nats.Message.create(subject, data, null);
|
|
||||||
defer message3.destroy();
|
|
||||||
|
|
||||||
const message4 = try nats.Message.create(subject, null, null);
|
|
||||||
defer message4.destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
test "message: get subject" {
|
|
||||||
try nats.init(-1);
|
|
||||||
defer nats.deinit();
|
|
||||||
|
|
||||||
const subject = "hello";
|
|
||||||
const message = try nats.Message.create(subject, null, null);
|
|
||||||
defer message.destroy();
|
|
||||||
|
|
||||||
const received = message.getSubject();
|
|
||||||
try std.testing.expectEqualStrings(subject, received);
|
|
||||||
}
|
|
||||||
|
|
||||||
test "message: get reply" {
|
|
||||||
try nats.init(-1);
|
|
||||||
defer nats.deinit();
|
|
||||||
|
|
||||||
const subject = "hello";
|
|
||||||
const reply = "reply";
|
|
||||||
const message = try nats.Message.create(subject, reply, null);
|
|
||||||
defer message.destroy();
|
|
||||||
|
|
||||||
const received = message.getReply() orelse return error.TestUnexpectedResult;
|
|
||||||
try std.testing.expectEqualStrings(reply, received);
|
|
||||||
|
|
||||||
const message2 = try nats.Message.create(subject, null, null);
|
|
||||||
defer message2.destroy();
|
|
||||||
|
|
||||||
const received2 = message2.getReply();
|
|
||||||
try std.testing.expect(received2 == null);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user