Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(401)

Unified Diff: tests/standalone/io/file_blocking_lock_test.dart

Issue 2501803002: Simplifies the blocking file lock test (Closed)
Patch Set: Address comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/standalone/io/file_blocking_lock_script.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..9f665b67565619d56f8427fdedcd9d26a9b50cb7 100644
--- a/tests/standalone/io/file_blocking_lock_test.dart
+++ b/tests/standalone/io/file_blocking_lock_test.dart
@@ -39,69 +39,55 @@ 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);
+ } on dynamic {
+ 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);
-
- await peer.exitCode.then((v) async {
- Expect.equals(0, v);
- await raf.close();
- await directory.delete(recursive: true);
- });
+ // Check that the peer exited successfully.
+ int v = await peer.exitCode;
+ Expect.equals(0, v);
+ await raf.close();
+ await directory.delete(recursive: true);
}
main() async {
« no previous file with comments | « tests/standalone/io/file_blocking_lock_script.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698