forked from lobo/lambda-zig
allow downstream projects to use test harness
This commit is contained in:
parent
26d97f2fec
commit
1ed36376d8
|
@ -24,6 +24,12 @@ pub fn build(b: *std.Build) !void {
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_ = b.addModule("lambda_runtime", .{
|
||||||
|
.root_source_file = b.path("src/lambda.zig"),
|
||||||
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
});
|
||||||
|
|
||||||
// This declares intent for the library to be installed into the standard
|
// This declares intent for the library to be installed into the standard
|
||||||
// location when the user invokes the "install" step (the default step when
|
// location when the user invokes the "install" step (the default step when
|
||||||
// running `zig build`).
|
// running `zig build`).
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
|
|
||||||
const HandlerFn = *const fn (std.mem.Allocator, []const u8) anyerror![]const u8;
|
pub const HandlerFn = *const fn (std.mem.Allocator, []const u8) anyerror![]const u8;
|
||||||
|
|
||||||
const log = std.log.scoped(.lambda);
|
const log = std.log.scoped(.lambda);
|
||||||
|
|
||||||
|
@ -355,15 +355,8 @@ fn handler(allocator: std.mem.Allocator, event_data: []const u8) ![]const u8 {
|
||||||
_ = allocator;
|
_ = allocator;
|
||||||
return event_data;
|
return event_data;
|
||||||
}
|
}
|
||||||
fn test_run(allocator: std.mem.Allocator, event_handler: HandlerFn) !std.Thread {
|
|
||||||
return try std.Thread.spawn(
|
|
||||||
.{},
|
|
||||||
run,
|
|
||||||
.{ allocator, event_handler },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lambda_request(allocator: std.mem.Allocator, request: []const u8, request_count: usize) ![]u8 {
|
pub fn test_lambda_request(allocator: std.mem.Allocator, request: []const u8, request_count: usize, handler_fn: HandlerFn) ![]u8 {
|
||||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
const aa = arena.allocator();
|
const aa = arena.allocator();
|
||||||
|
@ -405,9 +398,8 @@ fn lambda_request(allocator: std.mem.Allocator, request: []const u8, request_cou
|
||||||
// so we'll use the arena allocator
|
// so we'll use the arena allocator
|
||||||
defer server_thread.join(); // we'll be shutting everything down before we exit
|
defer server_thread.join(); // we'll be shutting everything down before we exit
|
||||||
|
|
||||||
// Now we need to start the lambda framework, following a siimilar pattern
|
// Now we need to start the lambda framework
|
||||||
const lambda_thread = try test_run(allocator, handler); // We want our function under test to report leaks
|
try run(allocator, handler_fn); // We want our function under test to report leaks
|
||||||
lambda_thread.join();
|
|
||||||
return try allocator.dupe(u8, server_request_aka_lambda_response);
|
return try allocator.dupe(u8, server_request_aka_lambda_response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -422,7 +414,7 @@ test "basic request" {
|
||||||
const expected_response =
|
const expected_response =
|
||||||
\\{"foo": "bar", "baz": "qux"}
|
\\{"foo": "bar", "baz": "qux"}
|
||||||
;
|
;
|
||||||
const lambda_response = try lambda_request(allocator, request, 1);
|
const lambda_response = try test_lambda_request(allocator, request, 1, handler);
|
||||||
defer deinit();
|
defer deinit();
|
||||||
defer allocator.free(lambda_response);
|
defer allocator.free(lambda_response);
|
||||||
try std.testing.expectEqualStrings(expected_response, lambda_response);
|
try std.testing.expectEqualStrings(expected_response, lambda_response);
|
||||||
|
@ -439,8 +431,8 @@ test "several requests do not fail" {
|
||||||
const expected_response =
|
const expected_response =
|
||||||
\\{"foo": "bar", "baz": "qux"}
|
\\{"foo": "bar", "baz": "qux"}
|
||||||
;
|
;
|
||||||
const lambda_response = try lambda_request(allocator, request, 5);
|
const lambda_response = try test_lambda_request(allocator, request, 5);
|
||||||
defer deinit();
|
defer deinit();
|
||||||
defer allocator.free(lambda_response);
|
defer allocator.free(lambda_response);
|
||||||
try std.testing.expectEqualStrings(expected_response, lambda_response);
|
try std.testing.expectEqualStrings(expected_response, lambda_response, handler);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user