tests: add top level function tests

These are mostly just tests to make sure the wrappers function as
written, rather than testing for any specific output. This commit
demonstrates the simple value of actually referencing the various
wrapper functions, as a variety of really basic compilation errors
were caught and addressed.
This commit is contained in:
2023-08-27 14:50:44 -07:00
parent 226d678e68
commit 4cf5049882
4 changed files with 143 additions and 31 deletions

View File

@@ -1,5 +1,4 @@
const std = @import("std");
test {
_ = @import("./nats.zig");
_ = @import("./message.zig");
}

96
tests/nats.zig Normal file
View File

@@ -0,0 +1,96 @@
const std = @import("std");
const nats = @import("nats");
test "version" {
const version = nats.getVersion();
const vernum = nats.getVersionNumber();
try std.testing.expectEqualStrings("3.6.1", version);
try std.testing.expectEqual(@as(u32, 0x03_06_01), vernum);
try std.testing.expect(nats.checkCompatibility());
}
test "time" {
const now = nats.now();
const nownano = nats.nowInNanoseconds();
nats.sleep(1);
const later = nats.now();
const laternano = nats.nowInNanoseconds();
try std.testing.expect(later >= now);
try std.testing.expect(laternano >= nownano);
}
test "init" {
{
try nats.init(nats.default_spin_count);
defer nats.deinit();
}
{
// a completely random number
try nats.init(900_142_069);
nats.deinit();
}
{
try nats.init(0);
try nats.deinitWait(1000);
}
}
test "misc" {
{
try nats.init(nats.default_spin_count);
defer nats.deinit();
try nats.setMessageDeliveryPoolSize(500);
}
{
try nats.init(nats.default_spin_count);
defer nats.deinit();
// just test that the function is wrapped properly
nats.releaseThreadMemory();
}
blk: {
try nats.init(nats.default_spin_count);
defer nats.deinit();
// this is a mess of a test that is designed to fail because actually we're
// testing out the error reporting functions instead of signing. Nice bait
// and switch.
const signed = nats.sign("12345678", "12345678") catch {
const err = nats.getLastError();
std.debug.print("signing failed: {s}\n", .{err.desc});
var stackmem = [_]u8{0} ** 512;
var stackbuf: []u8 = &stackmem;
nats.getLastErrorStack(&stackbuf) catch {
std.debug.print("Actually, the error stack was too big\n", .{});
break :blk;
};
std.debug.print("stack: {s}\n", .{stackbuf});
break :blk;
};
std.heap.raw_c_allocator.free(signed);
}
}
test "inbox" {
try nats.init(nats.default_spin_count);
defer nats.deinit();
const inbox = try nats.createInbox();
defer nats.destroyInbox(inbox);
std.debug.print("inbox: {s}\n", .{inbox});
}