-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.zig
96 lines (74 loc) · 2.68 KB
/
test.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const std = @import("std");
const snow = @import("snow.zig");
const sync = @import("sync.zig");
const pike = @import("pike");
const net = std.net;
const mem = std.mem;
const testing = std.testing;
test "client / server" {
const Protocol = struct {
const Self = @This();
event: sync.Event = .{},
pub fn handshake(self: *Self, comptime side: snow.Side, socket: anytype) !void {
return {};
}
pub fn close(self: *Self, comptime side: snow.Side, socket: anytype) void {
return {};
}
pub fn purge(self: *Self, comptime side: snow.Side, socket: anytype, items: []const []const u8) void {
return {};
}
pub fn read(self: *Self, comptime side: snow.Side, socket: anytype, reader: anytype) !void {
while (true) {
const line = try reader.readLine();
defer reader.shift(line.len);
self.event.notify();
}
}
pub fn write(self: *Self, comptime side: snow.Side, socket: anytype, writer: anytype, items: [][]const u8) !void {
for (items) |message| {
if (mem.indexOfScalar(u8, message, '\n') != null) {
return error.UnexpectedDelimiter;
}
const frame = try writer.peek(message.len + 1);
mem.copy(u8, frame[0..message.len], message);
frame[message.len..][0] = '\n';
}
try writer.flush();
}
};
const opts: snow.Options = .{ .protocol_type = *Protocol };
const Test = struct {
fn run(notifier: *const pike.Notifier, protocol: *Protocol, stopped: *bool) !void {
defer stopped.* = true;
var server = try snow.Server(opts).init(
protocol,
testing.allocator,
notifier,
net.Address.initIp4(.{ 0, 0, 0, 0 }, 0),
);
defer server.deinit();
try server.serve();
var client = snow.Client(opts).init(
protocol,
testing.allocator,
notifier,
try server.socket.getBindAddress(),
);
defer client.deinit();
inline for (.{ "A", "B", "C", "D" }) |message| {
try client.write(message);
protocol.event.wait();
}
}
};
const notifier = try pike.Notifier.init();
defer notifier.deinit();
var protocol: Protocol = .{};
var stopped = false;
var frame = async Test.run(¬ifier, &protocol, &stopped);
while (!stopped) {
try notifier.poll(10_000);
}
try nosuspend await frame;
}