| ... | @@ -130,6 +130,44 @@ test "listen on a port, send bytes, receive bytes" { | ... | @@ -130,6 +130,44 @@ test "listen on a port, send bytes, receive bytes" { |
| 130 | try await client_frame; | 130 | try await client_frame; |
| 131 | } | 131 | } |
| 132 | | 132 | |
| | 133 | test "listen on ipv4 try connect on ipv6 then ipv4" { |
| | 134 | if (!std.io.is_async) return error.SkipZigTest; |
| | 135 | |
| | 136 | if (std.builtin.os.tag != .linux and !std.builtin.os.tag.isDarwin()) { |
| | 137 | // TODO build abstractions for other operating systems |
| | 138 | return error.SkipZigTest; |
| | 139 | } |
| | 140 | |
| | 141 | // TODO doing this at comptime crashed the compiler |
| | 142 | const localhost = try net.Address.parseIp("127.0.0.1", 0); |
| | 143 | |
| | 144 | var server = net.StreamServer.init(net.StreamServer.Options{}); |
| | 145 | defer server.deinit(); |
| | 146 | try server.listen(localhost); |
| | 147 | |
| | 148 | var server_frame = async testServer(&server); |
| | 149 | var client_frame = async testClientToHost( |
| | 150 | testing.allocator, |
| | 151 | "localhost", |
| | 152 | server.listen_address.getPort(), |
| | 153 | ); |
| | 154 | |
| | 155 | try await server_frame; |
| | 156 | try await client_frame; |
| | 157 | } |
| | 158 | |
| | 159 | fn testClientToHost(allocator: *mem.Allocator, name: []const u8, port: u16) anyerror!void { |
| | 160 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| | 161 | |
| | 162 | const connection = try net.tcpConnectToHost(allocator, name, port); |
| | 163 | defer connection.close(); |
| | 164 | |
| | 165 | var buf: [100]u8 = undefined; |
| | 166 | const len = try connection.read(&buf); |
| | 167 | const msg = buf[0..len]; |
| | 168 | testing.expect(mem.eql(u8, msg, "hello from server\n")); |
| | 169 | } |
| | 170 | |
| 133 | fn testClient(addr: net.Address) anyerror!void { | 171 | fn testClient(addr: net.Address) anyerror!void { |
| 134 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | 172 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 135 | | 173 | |