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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « tests/standalone/io/file_blocking_lock_script.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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;
43
44 Future<bool> waitForPeer(RandomAccessFile raf, int length) async {
45 Stopwatch s = new Stopwatch();
46 s.start();
47 while (true) {
48 await raf.unlock(0, length);
49 if (s.elapsedMilliseconds > peerTimeoutMilliseconds) {
50 s.stop();
51 return false;
52 }
53 try {
54 await raf.lock(FileLock.EXCLUSIVE, 0, length);
55 } on dynamic {
56 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length);
57 break;
58 }
59 }
60 s.stop();
61 return true;
62 }
63
42 testLockWholeFile() async { 64 testLockWholeFile() async {
43 const int length = 25; 65 const int length = 25;
44 Directory directory = await Directory.systemTemp.createTemp('dart_file_lock'); 66 Directory directory = await Directory.systemTemp.createTemp('dart_file_lock');
45 File file = new File(join(directory.path, "file")); 67 File file = new File(join(directory.path, "file"));
46 await file.writeAsBytes(new List.filled(length, 0)); 68 await file.writeAsBytes(new List.filled(length, 0));
47 var raf = await file.open(mode: APPEND); 69 var raf = await file.open(mode: APPEND);
48 await raf.setPosition(0);
49 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length); 70 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length);
50 Process peer = await runPeer(file.path, length, FileLock.BLOCKING_EXCLUSIVE); 71 Process peer = await runPeer(file.path, length, FileLock.BLOCKING_EXCLUSIVE);
51 72
52 int nextToWrite = 1; 73 // Waits for the peer to take the lock, then takes the lock.
53 int at = 0; 74 Expect.isTrue(await waitForPeer(raf, length));
54 List iWrote = new List.filled(length, 0);
55 bool nonBlockingFailed = false;
56 while (nextToWrite <= length) {
57 int p = await raf.position();
58 await raf.writeByte(nextToWrite);
59 await raf.flush();
60 // Record which bytes this process wrote so that we can check that the
61 // other process was able to take the lock and write some bytes.
62 iWrote[nextToWrite-1] = nextToWrite;
63 nextToWrite++;
64 // Let the other process get the lock at least once by spinning until the
65 // non-blocking lock fails.
66 while (!nonBlockingFailed) {
67 await raf.unlock(0, length);
68 try {
69 await raf.lock(FileLock.EXCLUSIVE, 0, length);
70 } catch(e) {
71 // Check that at some point the non-blocking lock fails.
72 nonBlockingFailed = true;
73 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length);
74 }
75 }
76 while (true) {
77 p = await raf.position();
78 at = await raf.readByte();
79 if (at == 0 || at == -1) break;
80 nextToWrite++;
81 }
82 await raf.setPosition(p);
83 }
84 75
76 // Check that the peer wrote to the file.
77 int p = 0;
85 await raf.setPosition(0); 78 await raf.setPosition(0);
86 for (int i = 1; i <= length; i++) { 79 while (p < length) {
87 Expect.equals(i, await raf.readByte()); 80 int at = await raf.readByte();
81 Expect.equals(1, at);
82 p++;
88 } 83 }
89 await raf.unlock(0, length); 84 await raf.unlock(0, length);
90 85
91 bool wroteAll = true; 86 // Check that the peer exited successfully.
92 for (int i = 0; i < length; i++) { 87 int v = await peer.exitCode;
93 // If there's a 0 entry, this process didn't write all bytes. 88 Expect.equals(0, v);
94 wroteAll = wroteAll && (iWrote[i] == 0); 89 await raf.close();
95 } 90 await directory.delete(recursive: true);
96 Expect.equals(false, wroteAll);
97
98 Expect.equals(true, nonBlockingFailed);
99
100 await peer.exitCode.then((v) async {
101 Expect.equals(0, v);
102 await raf.close();
103 await directory.delete(recursive: true);
104 });
105 } 91 }
106 92
107 main() async { 93 main() async {
108 asyncStart(); 94 asyncStart();
109 await testLockWholeFile(); 95 await testLockWholeFile();
110 asyncEnd(); 96 asyncEnd();
111 } 97 }
OLDNEW
« 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