diff options
| author | Loris Cro <kappaloris@gmail.com> | 2023-06-18 09:06:40 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-18 09:06:40 +0200 |
| commit | 216ef10dc471e4db60a30208be178d6c59efeaaf (patch) | |
| tree | 8c239dab283ae9cb3b7fe099bae240bcc53f894e /lib/std/net | |
| parent | 0fc1d396495c1ab482197021dedac8bea3f9401c (diff) | |
| parent | 729a051e9e38674233190aea23c0ac8c134f2d67 (diff) | |
| download | zig-216ef10dc471e4db60a30208be178d6c59efeaaf.tar.gz zig-216ef10dc471e4db60a30208be178d6c59efeaaf.zip | |
Merge branch 'master' into autodoc-searchkey
Diffstat (limited to 'lib/std/net')
| -rw-r--r-- | lib/std/net/test.zig | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/lib/std/net/test.zig b/lib/std/net/test.zig index f59d73d621..817d6c2593 100644 --- a/lib/std/net/test.zig +++ b/lib/std/net/test.zig @@ -1,4 +1,4 @@ -const std = @import("../std.zig"); +const std = @import("std"); const builtin = @import("builtin"); const net = std.net; const mem = std.mem; @@ -182,7 +182,7 @@ test "listen on a port, send bytes, receive bytes" { try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]); } -test "listen on a port, send bytes, receive bytes" { +test "listen on a port, send bytes, receive bytes, async-only" { if (!std.io.is_async) return error.SkipZigTest; if (builtin.os.tag != .linux and !builtin.os.tag.isDarwin()) { @@ -300,22 +300,23 @@ test "listen on a unix socket, send bytes, receive bytes" { var server = net.StreamServer.init(.{}); defer server.deinit(); - const socket_path = "socket.unix"; + var socket_path = try generateFileName("socket.unix"); + defer testing.allocator.free(socket_path); var socket_addr = try net.Address.initUnix(socket_path); defer std.fs.cwd().deleteFile(socket_path) catch {}; try server.listen(socket_addr); const S = struct { - fn clientFn() !void { - const socket = try net.connectUnixSocket(socket_path); + fn clientFn(path: []const u8) !void { + const socket = try net.connectUnixSocket(path); defer socket.close(); _ = try socket.writer().writeAll("Hello world!"); } }; - const t = try std.Thread.spawn(.{}, S.clientFn, .{}); + const t = try std.Thread.spawn(.{}, S.clientFn, .{socket_path}); defer t.join(); var client = try server.accept(); @@ -326,3 +327,13 @@ test "listen on a unix socket, send bytes, receive bytes" { try testing.expectEqual(@as(usize, 12), n); try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]); } + +fn generateFileName(base_name: []const u8) ![]const u8 { + const random_bytes_count = 12; + const sub_path_len = comptime std.fs.base64_encoder.calcSize(random_bytes_count); + var random_bytes: [12]u8 = undefined; + std.crypto.random.bytes(&random_bytes); + var sub_path: [sub_path_len]u8 = undefined; + _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes); + return std.fmt.allocPrint(testing.allocator, "{s}-{s}", .{ sub_path[0..], base_name }); +} |
