Chromium Code Reviews| Index: tests/standalone/io/file_blocking_lock_test.dart |
| diff --git a/tests/standalone/io/file_blocking_lock_test.dart b/tests/standalone/io/file_blocking_lock_test.dart |
| index aa44bd15850162cee7965730635ca16fea32cbbe..23c543fdb8e2a9c5d703cfe0380c608fe8f83732 100644 |
| --- a/tests/standalone/io/file_blocking_lock_test.dart |
| +++ b/tests/standalone/io/file_blocking_lock_test.dart |
| @@ -39,64 +39,51 @@ runPeer(String path, int len, FileLock mode) { |
| }); |
| } |
| +const int peerTimeoutMilliseconds = 10000; |
| + |
| +Future<bool> waitForPeer(RandomAccessFile raf, int length) async { |
| + Stopwatch s = new Stopwatch(); |
| + s.start(); |
| + while (true) { |
| + await raf.unlock(0, length); |
| + if (s.elapsedMilliseconds > peerTimeoutMilliseconds) { |
| + s.stop(); |
| + return false; |
| + } |
| + try { |
| + await raf.lock(FileLock.EXCLUSIVE, 0, length); |
| + } catch(e) { |
|
Florian Schneider
2016/11/15 17:54:49
nit: Should e be expected to be a specific excepti
zra
2016/11/15 18:23:58
Done.
|
| + await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length); |
| + break; |
| + } |
| + } |
| + s.stop(); |
| + return true; |
| +} |
| + |
| testLockWholeFile() async { |
| const int length = 25; |
| Directory directory = await Directory.systemTemp.createTemp('dart_file_lock'); |
| File file = new File(join(directory.path, "file")); |
| await file.writeAsBytes(new List.filled(length, 0)); |
| var raf = await file.open(mode: APPEND); |
| - await raf.setPosition(0); |
| await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length); |
| Process peer = await runPeer(file.path, length, FileLock.BLOCKING_EXCLUSIVE); |
| - int nextToWrite = 1; |
| - int at = 0; |
| - List iWrote = new List.filled(length, 0); |
| - bool nonBlockingFailed = false; |
| - while (nextToWrite <= length) { |
| - int p = await raf.position(); |
| - await raf.writeByte(nextToWrite); |
| - await raf.flush(); |
| - // Record which bytes this process wrote so that we can check that the |
| - // other process was able to take the lock and write some bytes. |
| - iWrote[nextToWrite-1] = nextToWrite; |
| - nextToWrite++; |
| - // Let the other process get the lock at least once by spinning until the |
| - // non-blocking lock fails. |
| - while (!nonBlockingFailed) { |
| - await raf.unlock(0, length); |
| - try { |
| - await raf.lock(FileLock.EXCLUSIVE, 0, length); |
| - } catch(e) { |
| - // Check that at some point the non-blocking lock fails. |
| - nonBlockingFailed = true; |
| - await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length); |
| - } |
| - } |
| - while (true) { |
| - p = await raf.position(); |
| - at = await raf.readByte(); |
| - if (at == 0 || at == -1) break; |
| - nextToWrite++; |
| - } |
| - await raf.setPosition(p); |
| - } |
| + // Waits for the peer to take the lock, then takes the lock. |
| + Expect.isTrue(await waitForPeer(raf, length)); |
| + // Check that the peer wrote to the file. |
| + int p = 0; |
| await raf.setPosition(0); |
| - for (int i = 1; i <= length; i++) { |
| - Expect.equals(i, await raf.readByte()); |
| + while (p < length) { |
| + int at = await raf.readByte(); |
| + Expect.equals(1, at); |
| + p++; |
| } |
| await raf.unlock(0, length); |
| - bool wroteAll = true; |
| - for (int i = 0; i < length; i++) { |
| - // If there's a 0 entry, this process didn't write all bytes. |
| - wroteAll = wroteAll && (iWrote[i] == 0); |
| - } |
| - Expect.equals(false, wroteAll); |
| - |
| - Expect.equals(true, nonBlockingFailed); |
| - |
| + // Check that the peer exited successfully. |
| await peer.exitCode.then((v) async { |
|
Florian Schneider
2016/11/15 17:54:49
Can this be shortened?
var v = await peer.exitCod
zra
2016/11/15 18:23:58
Done.
|
| Expect.equals(0, v); |
| await raf.close(); |