OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 import 'dart:io'; |
| 7 |
| 8 import "package:async_helper/async_helper.dart"; |
| 9 import "package:expect/expect.dart"; |
| 10 import "package:path/path.dart"; |
| 11 |
| 12 // Check whether the file is locked or not. |
| 13 check(String path, int start, int end, FileLock mode, {bool locked}) { |
| 14 // Client process returns either 'LOCK FAILED' or 'LOCK SUCCEEDED'. |
| 15 var expected = locked ? 'LOCK FAILED' : 'LOCK SUCCEEDED'; |
| 16 var arguments = [] |
| 17 ..addAll(Platform.executableArguments) |
| 18 ..add(Platform.script.resolve('file_lock_script.dart').toFilePath()) |
| 19 ..add(path) |
| 20 ..add(mode == FileLock.EXCLUSIVE ? 'EXCLUSIVE' : 'SHARED') |
| 21 ..add('$start') |
| 22 ..add('$end'); |
| 23 return Process.run(Platform.executable, arguments) |
| 24 .then((ProcessResult result) { |
| 25 if (result.exitCode != 0 || !result.stdout.contains(expected)) { |
| 26 print("Client failed, exit code ${result.exitCode}"); |
| 27 print(" stdout:"); |
| 28 print(result.stdout); |
| 29 print(" stderr:"); |
| 30 print(result.stderr); |
| 31 print(" arguments:"); |
| 32 print(arguments); |
| 33 Expect.fail('Client subprocess exit code: ${result.exitCode}'); |
| 34 } |
| 35 }); |
| 36 } |
| 37 |
| 38 checkLocked(String path, |
| 39 [int start, int end, FileLock mode = FileLock.EXCLUSIVE]) => |
| 40 check(path, start, end, mode, locked: true); |
| 41 |
| 42 checkNotLocked(String path, |
| 43 [int start, int end, FileLock mode = FileLock.EXCLUSIVE]) => |
| 44 check(path, start, end, mode, locked: false); |
| 45 |
| 46 void testLockWholeFile() { |
| 47 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 48 File file = new File(join(directory.path, "file")); |
| 49 file.writeAsBytesSync(new List.filled(10, 0)); |
| 50 var raf = file.openSync(mode: WRITE); |
| 51 raf.lockSync(); |
| 52 asyncStart(); |
| 53 checkLocked(file.path).then((_) { |
| 54 return checkLocked(file.path, 0, 2).then((_) { |
| 55 raf.unlockSync(); |
| 56 return checkNotLocked(file.path).then((_) { |
| 57 }); |
| 58 }); |
| 59 }).whenComplete(() { |
| 60 raf.closeSync(); |
| 61 directory.deleteSync(recursive: true); |
| 62 asyncEnd(); |
| 63 }); |
| 64 } |
| 65 |
| 66 void testLockWholeFileAsync() { |
| 67 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 68 File file = new File(join(directory.path, "file")); |
| 69 file.writeAsBytesSync(new List.filled(10, 0)); |
| 70 var raf = file.openSync(mode: WRITE); |
| 71 asyncStart(); |
| 72 Future.forEach([ |
| 73 () => raf.lock(), |
| 74 () => checkLocked(file.path, 0, 2), |
| 75 () => checkLocked(file.path), |
| 76 () => raf.unlock(), |
| 77 () => checkNotLocked(file.path), |
| 78 ], |
| 79 (f) => f()).whenComplete(() { |
| 80 raf.closeSync(); |
| 81 directory.deleteSync(recursive: true); |
| 82 asyncEnd(); |
| 83 }); |
| 84 } |
| 85 |
| 86 void testLockRange() { |
| 87 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 88 File file = new File(join(directory.path, "file")); |
| 89 file.writeAsBytesSync(new List.filled(10, 0)); |
| 90 var raf1 = file.openSync(mode: WRITE); |
| 91 var raf2 = file.openSync(mode: WRITE); |
| 92 asyncStart(); |
| 93 var tests = [ |
| 94 () => raf1.lockSync(FileLock.EXCLUSIVE, 2, 3), |
| 95 () => raf2.lockSync(FileLock.EXCLUSIVE, 5, 7), |
| 96 () => checkNotLocked(file.path, 0, 2), |
| 97 () => checkLocked(file.path, 0, 3), |
| 98 () => checkNotLocked(file.path, 4, 5), |
| 99 () => checkLocked(file.path, 4, 6), |
| 100 () => checkLocked(file.path, 6), |
| 101 () => checkNotLocked(file.path, 7), |
| 102 () => raf1.unlockSync(2, 3), |
| 103 () => checkNotLocked(file.path, 0, 5), |
| 104 () => checkLocked(file.path, 4, 6), |
| 105 () => checkLocked(file.path, 6), |
| 106 () => checkNotLocked(file.path, 7), |
| 107 ]; |
| 108 // On Windows regions unlocked must match regions locked. |
| 109 if (!Platform.isWindows) { |
| 110 tests.addAll([ |
| 111 () => raf1.unlockSync(5, 6), |
| 112 () => checkNotLocked(file.path, 0, 6), |
| 113 () => checkLocked(file.path, 6), |
| 114 () => checkNotLocked(file.path, 7), |
| 115 () => raf2.unlockSync(6, 7), |
| 116 () => checkNotLocked(file.path) |
| 117 ]); |
| 118 } else { |
| 119 tests.addAll([ |
| 120 () => raf2.unlockSync(5, 7), |
| 121 () => checkNotLocked(file.path) |
| 122 ]); |
| 123 } |
| 124 Future.forEach(tests, (f) => f()).whenComplete(() { |
| 125 raf1.closeSync(); |
| 126 raf2.closeSync(); |
| 127 directory.deleteSync(recursive: true); |
| 128 asyncEnd(); |
| 129 }); |
| 130 } |
| 131 |
| 132 void testLockRangeAsync() { |
| 133 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 134 File file = new File(join(directory.path, "file")); |
| 135 file.writeAsBytesSync(new List.filled(10, 0)); |
| 136 var raf1 = file.openSync(mode: WRITE); |
| 137 var raf2 = file.openSync(mode: WRITE); |
| 138 asyncStart(); |
| 139 var tests = [ |
| 140 () => raf1.lock(FileLock.EXCLUSIVE, 2, 3), |
| 141 () => raf2.lock(FileLock.EXCLUSIVE, 5, 7), |
| 142 () => checkNotLocked(file.path, 0, 2), |
| 143 () => checkLocked(file.path, 0, 3), |
| 144 () => checkNotLocked(file.path, 4, 5), |
| 145 () => checkLocked(file.path, 4, 6), |
| 146 () => checkLocked(file.path, 6), |
| 147 () => checkNotLocked(file.path, 7), |
| 148 () => raf1.unlock(2, 3), |
| 149 () => checkNotLocked(file.path, 0, 5), |
| 150 () => checkLocked(file.path, 4, 6), |
| 151 () => checkLocked(file.path, 6), |
| 152 () => checkNotLocked(file.path, 7), |
| 153 ]; |
| 154 // On Windows regions unlocked must match regions locked. |
| 155 if (!Platform.isWindows) { |
| 156 tests.addAll([ |
| 157 () => raf1.unlock(5, 6), |
| 158 () => checkNotLocked(file.path, 0, 6), |
| 159 () => checkLocked(file.path, 6), |
| 160 () => checkNotLocked(file.path, 7), |
| 161 () => raf2.unlock(6, 7), |
| 162 () => checkNotLocked(file.path) |
| 163 ]); |
| 164 } else { |
| 165 tests.addAll([ |
| 166 () => raf2.unlock(5, 7), |
| 167 () => checkNotLocked(file.path) |
| 168 ]); |
| 169 } |
| 170 Future.forEach(tests, (f) => f()).whenComplete(() { |
| 171 raf1.closeSync(); |
| 172 raf2.closeSync(); |
| 173 directory.deleteSync(recursive: true); |
| 174 asyncEnd(); |
| 175 }); |
| 176 } |
| 177 |
| 178 void testLockEnd() { |
| 179 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 180 File file = new File(join(directory.path, "file")); |
| 181 file.writeAsBytesSync(new List.filled(10, 0)); |
| 182 var raf = file.openSync(mode: APPEND); |
| 183 asyncStart(); |
| 184 Future.forEach([ |
| 185 () => raf.lockSync(FileLock.EXCLUSIVE, 2), |
| 186 () => checkNotLocked(file.path, 0, 2), |
| 187 () => checkLocked(file.path, 0, 3), |
| 188 () => checkLocked(file.path, 9), |
| 189 () => raf.writeFromSync(new List.filled(10, 0)), |
| 190 () => checkLocked(file.path, 10), |
| 191 () => checkLocked(file.path, 19), |
| 192 () => raf.unlockSync(2), |
| 193 () => checkNotLocked(file.path) |
| 194 ], |
| 195 (f) => f()).whenComplete(() { |
| 196 raf.closeSync(); |
| 197 directory.deleteSync(recursive: true); |
| 198 asyncEnd(); |
| 199 }); |
| 200 } |
| 201 |
| 202 void testLockEndAsync() { |
| 203 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 204 File file = new File(join(directory.path, "file")); |
| 205 file.writeAsBytesSync(new List.filled(10, 0)); |
| 206 var raf = file.openSync(mode: APPEND); |
| 207 asyncStart(); |
| 208 Future.forEach([ |
| 209 () => raf.lock(FileLock.EXCLUSIVE, 2), |
| 210 () => checkNotLocked(file.path, 0, 2), |
| 211 () => checkLocked(file.path, 0, 3), |
| 212 () => checkLocked(file.path, 9), |
| 213 () => raf.writeFromSync(new List.filled(10, 0)), |
| 214 () => checkLocked(file.path, 10), |
| 215 () => checkLocked(file.path, 19), |
| 216 () => raf.unlock(2), |
| 217 () => checkNotLocked(file.path) |
| 218 ], |
| 219 (f) => f()).whenComplete(() { |
| 220 raf.closeSync(); |
| 221 directory.deleteSync(recursive: true); |
| 222 asyncEnd(); |
| 223 }); |
| 224 } |
| 225 |
| 226 void testLockShared() { |
| 227 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 228 File file = new File(join(directory.path, "file")); |
| 229 file.writeAsBytesSync(new List.filled(10, 0)); |
| 230 var raf = file.openSync(); |
| 231 asyncStart(); |
| 232 Future.forEach([ |
| 233 () => raf.lock(FileLock.SHARED), |
| 234 () => checkLocked(file.path), |
| 235 () => checkLocked(file.path, 0, 2), |
| 236 () => checkNotLocked(file.path, 0, 2, FileLock.SHARED) |
| 237 ], |
| 238 (f) => f()).then((_) { |
| 239 raf.closeSync(); |
| 240 directory.deleteSync(recursive: true); |
| 241 asyncEnd(); |
| 242 }); |
| 243 } |
| 244 |
| 245 void testLockSharedAsync() { |
| 246 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 247 File file = new File(join(directory.path, "file")); |
| 248 file.writeAsBytesSync(new List.filled(10, 0)); |
| 249 var raf = file.openSync(); |
| 250 asyncStart(); |
| 251 Future.forEach([ |
| 252 () => raf.lock(FileLock.SHARED), |
| 253 () => checkLocked(file.path), |
| 254 () => checkLocked(file.path, 0, 2), |
| 255 () => checkNotLocked(file.path, 0, 2, FileLock.SHARED) |
| 256 ], |
| 257 (f) => f()).whenComplete(() { |
| 258 raf.closeSync(); |
| 259 directory.deleteSync(recursive: true); |
| 260 asyncEnd(); |
| 261 }); |
| 262 } |
| 263 |
| 264 void testLockAfterLength() { |
| 265 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 266 File file = new File(join(directory.path, "file")); |
| 267 file.writeAsBytesSync(new List.filled(10, 0)); |
| 268 var raf = file.openSync(mode: APPEND); |
| 269 asyncStart(); |
| 270 Future.forEach([ |
| 271 () => raf.lockSync(FileLock.EXCLUSIVE, 2, 15), |
| 272 () => checkNotLocked(file.path, 0, 2), |
| 273 () => checkLocked(file.path, 0, 3), |
| 274 () => checkLocked(file.path, 9), |
| 275 () => checkLocked(file.path, 14), |
| 276 () => raf.writeFromSync(new List.filled(10, 0)), |
| 277 () => checkLocked(file.path, 10), |
| 278 () => checkNotLocked(file.path, 15), |
| 279 () => raf.unlockSync(2, 15), |
| 280 () => checkNotLocked(file.path) |
| 281 ], |
| 282 (f) => f()).whenComplete(() { |
| 283 raf.closeSync(); |
| 284 directory.deleteSync(recursive: true); |
| 285 asyncEnd(); |
| 286 }); |
| 287 } |
| 288 |
| 289 void testLockAfterLengthAsync() { |
| 290 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock'); |
| 291 File file = new File(join(directory.path, "file")); |
| 292 file.writeAsBytesSync(new List.filled(10, 0)); |
| 293 var raf = file.openSync(mode: APPEND); |
| 294 asyncStart(); |
| 295 Future.forEach([ |
| 296 () => raf.lock(FileLock.EXCLUSIVE, 2, 15), |
| 297 () => checkNotLocked(file.path, 0, 2), |
| 298 () => checkLocked(file.path, 0, 3), |
| 299 () => checkLocked(file.path, 9), |
| 300 () => checkLocked(file.path, 14), |
| 301 () => raf.writeFromSync(new List.filled(10, 0)), |
| 302 () => checkLocked(file.path, 10), |
| 303 () => checkNotLocked(file.path, 15), |
| 304 () => raf.unlock(2, 15), |
| 305 () => checkNotLocked(file.path) |
| 306 ], |
| 307 (f) => f()).whenComplete(() { |
| 308 raf.closeSync(); |
| 309 directory.deleteSync(recursive: true); |
| 310 asyncEnd(); |
| 311 }); |
| 312 } |
| 313 |
| 314 void main() { |
| 315 testLockWholeFile(); |
| 316 testLockWholeFileAsync(); |
| 317 testLockRange(); |
| 318 testLockRangeAsync(); |
| 319 testLockEnd(); |
| 320 testLockEndAsync(); |
| 321 testLockShared(); |
| 322 testLockSharedAsync(); |
| 323 testLockAfterLength(); |
| 324 testLockAfterLengthAsync(); |
| 325 } |
OLD | NEW |