| 1 | const std = @import("std"); |
| 2 | const net = @import("net"); |
| 3 | const expect = @import("expect").expect; |
| 4 | |
| 5 | test "echo server" { |
| 6 | const server = try net.Address.initIp4(.{ 127, 0, 0, 1 }, 0).listen(.{}); |
| 7 | defer server.close(); |
| 8 | var t = try std.Thread.spawn(.{}, threadEcho, .{server.address}); |
| 9 | defer t.join(); |
| 10 | const conn = try server.accept(); |
| 11 | defer conn.close(); |
| 12 | const allocator = std.testing.allocator; |
| 13 | const recvd = try conn.stream.readAllAlloc(allocator, 1024); |
| 14 | defer allocator.free(recvd); |
| 15 | try expect(recvd).toEqualString("hello world!\n"); |
| 16 | } |
| 17 | fn threadEcho(adr: net.Address) !void { |
| 18 | const conn = try adr.tcpConnect(); |
| 19 | defer conn.close(); |
| 20 | try conn.writeAll("hello world!\n"); |
| 21 | } |