| ... | ... | @@ -1,7 +1,9 @@ |
| 1 | 1 | const std = @import("../std.zig"); |
| 2 | 2 | const builtin = std.builtin; |
| 3 | 3 | const fs = std.fs; |
| 4 | const Dir = std.fs.Dir; |
| 4 | 5 | const File = std.fs.File; |
| 6 | const tmpDir = std.testing.tmpDir; |
| 5 | 7 | |
| 6 | 8 | test "openSelfExe" { |
| 7 | 9 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| ... | ... | @@ -15,16 +17,18 @@ const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.millisecond; |
| 15 | 17 | test "open file with exclusive nonblocking lock twice" { |
| 16 | 18 | if (builtin.os.tag == .wasi) return error.SkipZigTest; |
| 17 | 19 | |
| 18 | | const dir = fs.cwd(); |
| 20 | var tmp = tmpDir(.{}); |
| 21 | defer tmp.cleanup(); |
| 22 | |
| 19 | 23 | const filename = "file_nonblocking_lock_test.txt"; |
| 20 | 24 | |
| 21 | | const file1 = try dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); |
| 25 | const file1 = try tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); |
| 22 | 26 | defer file1.close(); |
| 23 | 27 | |
| 24 | | const file2 = dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); |
| 28 | const file2 = tmp.dir.createFile(filename, .{ .lock = .Exclusive, .lock_nonblocking = true }); |
| 25 | 29 | std.debug.assert(std.meta.eql(file2, error.WouldBlock)); |
| 26 | 30 | |
| 27 | | dir.deleteFile(filename) catch |err| switch (err) { |
| 31 | tmp.dir.deleteFile(filename) catch |err| switch (err) { |
| 28 | 32 | error.FileNotFound => {}, |
| 29 | 33 | else => return err, |
| 30 | 34 | }; |
| ... | ... | @@ -40,9 +44,12 @@ test "open file with lock twice, make sure it wasn't open at the same time" { |
| 40 | 44 | |
| 41 | 45 | const filename = "file_lock_test.txt"; |
| 42 | 46 | |
| 47 | var tmp = tmpDir(.{}); |
| 48 | defer tmp.cleanup(); |
| 49 | |
| 43 | 50 | var contexts = [_]FileLockTestContext{ |
| 44 | | .{ .filename = filename, .create = true, .lock = .Exclusive }, |
| 45 | | .{ .filename = filename, .create = true, .lock = .Exclusive }, |
| 51 | .{ .dir = tmp.dir, .filename = filename, .create = true, .lock = .Exclusive }, |
| 52 | .{ .dir = tmp.dir, .filename = filename, .create = true, .lock = .Exclusive }, |
| 46 | 53 | }; |
| 47 | 54 | try run_lock_file_test(&contexts); |
| 48 | 55 | |
| ... | ... | @@ -58,7 +65,7 @@ test "open file with lock twice, make sure it wasn't open at the same time" { |
| 58 | 65 | |
| 59 | 66 | std.debug.assert(!contexts[0].overlaps(&contexts[1])); |
| 60 | 67 | |
| 61 | | fs.cwd().deleteFile(filename) catch |err| switch (err) { |
| 68 | tmp.dir.deleteFile(filename) catch |err| switch (err) { |
| 62 | 69 | error.FileNotFound => {}, |
| 63 | 70 | else => return err, |
| 64 | 71 | }; |
| ... | ... | @@ -80,12 +87,15 @@ test "create file, lock and read from multiple process at once" { |
| 80 | 87 | const filename = "file_read_lock_test.txt"; |
| 81 | 88 | const filedata = "Hello, world!\n"; |
| 82 | 89 | |
| 83 | | try fs.cwd().writeFile(filename, filedata); |
| 90 | var tmp = tmpDir(.{}); |
| 91 | defer tmp.cleanup(); |
| 92 | |
| 93 | try tmp.dir.writeFile(filename, filedata); |
| 84 | 94 | |
| 85 | 95 | var contexts = [_]FileLockTestContext{ |
| 86 | | .{ .filename = filename, .create = false, .lock = .Shared }, |
| 87 | | .{ .filename = filename, .create = false, .lock = .Shared }, |
| 88 | | .{ .filename = filename, .create = false, .lock = .Exclusive }, |
| 96 | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Shared }, |
| 97 | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Shared }, |
| 98 | .{ .dir = tmp.dir, .filename = filename, .create = false, .lock = .Exclusive }, |
| 89 | 99 | }; |
| 90 | 100 | |
| 91 | 101 | try run_lock_file_test(&contexts); |
| ... | ... | @@ -108,7 +118,7 @@ test "create file, lock and read from multiple process at once" { |
| 108 | 118 | std.debug.assert(contexts[0].bytes_read.? == filedata.len); |
| 109 | 119 | std.debug.assert(contexts[1].bytes_read.? == filedata.len); |
| 110 | 120 | |
| 111 | | fs.cwd().deleteFile(filename) catch |err| switch (err) { |
| 121 | tmp.dir.deleteFile(filename) catch |err| switch (err) { |
| 112 | 122 | error.FileNotFound => {}, |
| 113 | 123 | else => return err, |
| 114 | 124 | }; |
| ... | ... | @@ -133,6 +143,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" { |
| 133 | 143 | } |
| 134 | 144 | |
| 135 | 145 | const FileLockTestContext = struct { |
| 146 | dir: Dir, |
| 136 | 147 | filename: []const u8, |
| 137 | 148 | pid: if (builtin.os.tag == .windows) ?void else ?std.os.pid_t = null, |
| 138 | 149 | |
| ... | ... | @@ -154,12 +165,12 @@ const FileLockTestContext = struct { |
| 154 | 165 | fn run(ctx: *@This()) void { |
| 155 | 166 | var file: File = undefined; |
| 156 | 167 | if (ctx.create) { |
| 157 | | file = fs.cwd().createFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { |
| 168 | file = ctx.dir.createFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { |
| 158 | 169 | ctx.err = err; |
| 159 | 170 | return; |
| 160 | 171 | }; |
| 161 | 172 | } else { |
| 162 | | file = fs.cwd().openFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { |
| 173 | file = ctx.dir.openFile(ctx.filename, .{ .lock = ctx.lock }) catch |err| { |
| 163 | 174 | ctx.err = err; |
| 164 | 175 | return; |
| 165 | 176 | }; |