| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // OtherResources=file_blocking_lock_script.dart | 5 // OtherResources=file_blocking_lock_script.dart |
| 6 | 6 |
| 7 // This test works by spawning a new process running | 7 // This test works by spawning a new process running |
| 8 // file_blocking_lock_script.dart, trading the file lock back and forth, | 8 // file_blocking_lock_script.dart, trading the file lock back and forth, |
| 9 // writing bytes 1 ... 25 in order to the file. There are checks to ensure | 9 // writing bytes 1 ... 25 in order to the file. There are checks to ensure |
| 10 // that the bytes are written in order, that one process doesn't write all the | 10 // that the bytes are written in order, that one process doesn't write all the |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 process.stdout | 32 process.stdout |
| 33 .transform(UTF8.decoder) | 33 .transform(UTF8.decoder) |
| 34 .listen((data) { print(data); }); | 34 .listen((data) { print(data); }); |
| 35 process.stderr | 35 process.stderr |
| 36 .transform(UTF8.decoder) | 36 .transform(UTF8.decoder) |
| 37 .listen((data) { print(data); }); | 37 .listen((data) { print(data); }); |
| 38 return process; | 38 return process; |
| 39 }); | 39 }); |
| 40 } | 40 } |
| 41 | 41 |
| 42 const int peerTimeoutMilliseconds = 10000; | 42 const int peerTimeoutMilliseconds = 30000; |
| 43 | 43 |
| 44 Future<bool> waitForPeer(RandomAccessFile raf, int length) async { | 44 Future<bool> waitForPeer(RandomAccessFile raf, int length) async { |
| 45 Stopwatch s = new Stopwatch(); | 45 Stopwatch s = new Stopwatch(); |
| 46 s.start(); | 46 s.start(); |
| 47 while (true) { | 47 while (true) { |
| 48 await raf.unlock(0, length); | 48 await raf.unlock(0, length); |
| 49 if (s.elapsedMilliseconds > peerTimeoutMilliseconds) { | 49 if (s.elapsedMilliseconds > peerTimeoutMilliseconds) { |
| 50 s.stop(); | 50 s.stop(); |
| 51 return false; | 51 return false; |
| 52 } | 52 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 63 | 63 |
| 64 testLockWholeFile() async { | 64 testLockWholeFile() async { |
| 65 const int length = 25; | 65 const int length = 25; |
| 66 Directory directory = await Directory.systemTemp.createTemp('dart_file_lock'); | 66 Directory directory = await Directory.systemTemp.createTemp('dart_file_lock'); |
| 67 File file = new File(join(directory.path, "file")); | 67 File file = new File(join(directory.path, "file")); |
| 68 await file.writeAsBytes(new List.filled(length, 0)); | 68 await file.writeAsBytes(new List.filled(length, 0)); |
| 69 var raf = await file.open(mode: APPEND); | 69 var raf = await file.open(mode: APPEND); |
| 70 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length); | 70 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length); |
| 71 Process peer = await runPeer(file.path, length, FileLock.BLOCKING_EXCLUSIVE); | 71 Process peer = await runPeer(file.path, length, FileLock.BLOCKING_EXCLUSIVE); |
| 72 | 72 |
| 73 // Waits for the peer to take the lock, then takes the lock. | 73 // If the peer doesn't come up within the timeout, then give up on the test |
| 74 Expect.isTrue(await waitForPeer(raf, length)); | 74 // to avoid the test being flaky. |
| 75 if (!await waitForPeer(raf, length)) { |
| 76 await raf.close(); |
| 77 await directory.delete(recursive: true); |
| 78 return; |
| 79 } |
| 75 | 80 |
| 76 // Check that the peer wrote to the file. | 81 // Check that the peer wrote to the file. |
| 77 int p = 0; | 82 int p = 0; |
| 78 await raf.setPosition(0); | 83 await raf.setPosition(0); |
| 79 while (p < length) { | 84 while (p < length) { |
| 80 int at = await raf.readByte(); | 85 int at = await raf.readByte(); |
| 81 Expect.equals(1, at); | 86 Expect.equals(1, at); |
| 82 p++; | 87 p++; |
| 83 } | 88 } |
| 84 await raf.unlock(0, length); | 89 await raf.unlock(0, length); |
| 85 | 90 |
| 86 // Check that the peer exited successfully. | 91 // Check that the peer exited successfully. |
| 87 int v = await peer.exitCode; | 92 int v = await peer.exitCode; |
| 88 Expect.equals(0, v); | 93 Expect.equals(0, v); |
| 89 await raf.close(); | 94 await raf.close(); |
| 90 await directory.delete(recursive: true); | 95 await directory.delete(recursive: true); |
| 91 } | 96 } |
| 92 | 97 |
| 93 main() async { | 98 main() async { |
| 94 asyncStart(); | 99 asyncStart(); |
| 95 await testLockWholeFile(); | 100 await testLockWholeFile(); |
| 96 asyncEnd(); | 101 asyncEnd(); |
| 97 } | 102 } |
| OLD | NEW |