authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-14 20:22:31-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:15-07:00
loga1058dd27bd32fed2fba14732b8a6c7009fd1116
tree678ae3b3ad9a44ca168c35af21b12ed71dccf542
parent4aa5895d322c58cfa654e890beccc1009d49c6e5

fix std.fs unit test to not be racey


1 files changed, 22 insertions(+), 8 deletions(-)

lib/std/fs/test.zig+22-8
...@@ -1124,17 +1124,31 @@ test "open file with exclusive lock twice, make sure second lock waits" {...@@ -1124,17 +1124,31 @@ test "open file with exclusive lock twice, make sure second lock waits" {
1124test "open file with exclusive nonblocking lock twice (absolute paths)" {1124test "open file with exclusive nonblocking lock twice (absolute paths)" {
1125 if (builtin.os.tag == .wasi) return error.SkipZigTest;1125 if (builtin.os.tag == .wasi) return error.SkipZigTest;
11261126
1127 const allocator = testing.allocator;1127 var random_bytes: [12]u8 = undefined;
1128 std.crypto.random.bytes(&random_bytes);
11281129
1129 const cwd = try std.process.getCwdAlloc(allocator);1130 var random_b64: [fs.base64_encoder.calcSize(random_bytes.len)]u8 = undefined;
1130 defer allocator.free(cwd);1131 _ = fs.base64_encoder.encode(&random_b64, &random_bytes);
1131 const file_paths: [2][]const u8 = .{ cwd, "zig-test-absolute-paths.txt" };
1132 const filename = try fs.path.resolve(allocator, &file_paths);
1133 defer allocator.free(filename);
11341132
1135 const file1 = try fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });1133 const sub_path = random_b64 ++ "-zig-test-absolute-paths.txt";
11361134
1137 const file2 = fs.createFileAbsolute(filename, .{ .lock = .Exclusive, .lock_nonblocking = true });1135 const gpa = testing.allocator;
1136
1137 const cwd = try std.process.getCwdAlloc(gpa);
1138 defer gpa.free(cwd);
1139
1140 const filename = try fs.path.resolve(gpa, &[_][]const u8{ cwd, sub_path });
1141 defer gpa.free(filename);
1142
1143 const file1 = try fs.createFileAbsolute(filename, .{
1144 .lock = .Exclusive,
1145 .lock_nonblocking = true,
1146 });
1147
1148 const file2 = fs.createFileAbsolute(filename, .{
1149 .lock = .Exclusive,
1150 .lock_nonblocking = true,
1151 });
1138 file1.close();1152 file1.close();
1139 try testing.expectError(error.WouldBlock, file2);1153 try testing.expectError(error.WouldBlock, file2);
11401154