-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tcp.zig
48 lines (38 loc) · 1.46 KB
/
test_tcp.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const std = @import("std");
const Scheduler = @import("scheduler.zig").Scheduler;
const Fiber = @import("fiber.zig").Fiber;
const xev = @import("xev");
const TcpConnection = @import("tcp_connection.zig").TcpConnection;
test "tcp" {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();
defer {
const deinit_status = gpa.deinit();
//fail test; can't try in defer as defer is executed after we return
if (deinit_status == .leak) std.testing.expect(false) catch @panic("TEST FAIL");
}
var scheduler = Scheduler.new();
var loop = try xev.Loop.init(.{});
defer loop.deinit();
var stop = false;
const f = try Fiber.new(&allocator, struct {
pub fn f(me: *Fiber, loop_: *xev.Loop, stop_: *bool) !void {
std.debug.print("Connecting\n", .{});
var conn = try TcpConnection.connect(loop_, me);
std.debug.print("Connected!\n", .{});
std.debug.print("Writing\n", .{});
conn.write("Hello world!", me);
std.debug.print("Done!\n", .{});
std.debug.print("Reading\n", .{});
var buf: [256]u8 = undefined;
const n = conn.read(&buf, me);
std.debug.print("Done: buf=[{s}]\n", .{buf[0..n]});
stop_.* = true;
}
}.f, .{ &loop, &stop });
scheduler.schedule(f);
while (!stop) {
try scheduler.run();
try loop.run(xev.RunMode.no_wait);
}
}