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

Side by Side Diff: tests/standalone/io/file_lock_test.dart

Issue 833623004: Add support for file locking (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Comment/doc fixes Created 5 years, 11 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
Lasse Reichstein Nielsen 2015/01/08 11:59:22 2015
Søren Gjesse 2015/01/09 13:06:20 Done.
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 that the file is locked or not.
Lasse Reichstein Nielsen 2015/01/08 11:59:22 that -> whether
Søren Gjesse 2015/01/09 13:06:21 Done.
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('$start')
21 ..add('$end')
22 ..add(mode == FileLock.EXCLUSIVE ? 'EXCLUSIVE' : 'SHARED');
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 checkLocked(file.path).then((_) {
53 checkLocked(file.path, 0, 2).then((_) {
54 raf.unlockSync();
55 checkNotLocked(file.path).then((_) {
56 raf.closeSync();
57 directory.deleteSync(recursive: true);
kustermann 2015/01/08 12:54:28 Could you do the directory.deleteSync() in a whenC
Søren Gjesse 2015/01/09 13:06:21 Done.
58 });
59 });
60 });
61 }
62
63 void testLockWholeFileAsync() {
64 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
65 File file = new File(join(directory.path, "file"));
66 file.writeAsBytesSync(new List.filled(10, 0));
67 var raf = file.openSync(mode: WRITE);
68 asyncStart();
69 Future.forEach([
70 () => raf.lock(),
71 () => checkLocked(file.path, 0, 2),
72 () => checkLocked(file.path),
73 () => raf.unlock(),
74 () => checkNotLocked(file.path),
75 ],
76 (f) => f()).then((_) {
77 raf.closeSync();
78 directory.deleteSync(recursive: true);
79 asyncEnd();
80 });
81 }
82
83 void testLockRange() {
84 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
85 File file = new File(join(directory.path, "file"));
86 file.writeAsBytesSync(new List.filled(10, 0));
87 var raf1 = file.openSync(mode: WRITE);
88 var raf2 = file.openSync(mode: WRITE);
89 asyncStart();
90 var tests = [
91 () => raf1.lockSync(2, 3),
92 () => raf2.lockSync(5, 7),
93 () => checkNotLocked(file.path, 0, 2),
94 () => checkLocked(file.path, 0, 3),
95 () => checkNotLocked(file.path, 4, 5),
96 () => checkLocked(file.path, 4, 6),
97 () => checkLocked(file.path, 6),
98 () => checkNotLocked(file.path, 7),
99 () => raf1.unlockSync(2, 3),
100 () => checkNotLocked(file.path, 0, 5),
101 () => checkLocked(file.path, 4, 6),
102 () => checkLocked(file.path, 6),
103 () => checkNotLocked(file.path, 7),
104 ];
105 // On Windows regions unlocked must match regions locked.
106 if (!Platform.isWindows) {
107 tests.addAll([
108 () => raf1.unlockSync(5, 6),
109 () => checkNotLocked(file.path, 0, 6),
110 () => checkLocked(file.path, 6),
111 () => checkNotLocked(file.path, 7),
112 () => raf2.unlockSync(6, 7),
113 () => checkNotLocked(file.path)
114 ]);
115 } else {
116 tests.addAll([
117 () => raf2.unlockSync(5, 7),
118 () => checkNotLocked(file.path)
119 ]);
120 }
121 Future.forEach(tests, (f) => f()).then((_) {
122 raf1.closeSync();
123 raf2.closeSync();
124 directory.deleteSync(recursive: true);
125 asyncEnd();
126 });
127 }
128
129 void testLockRangeAsync() {
130 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
131 File file = new File(join(directory.path, "file"));
132 file.writeAsBytesSync(new List.filled(10, 0));
133 var raf1 = file.openSync(mode: WRITE);
134 var raf2 = file.openSync(mode: WRITE);
135 asyncStart();
136 var tests = [
137 () => raf1.lock(2, 3),
138 () => raf2.lock(5, 7),
139 () => checkNotLocked(file.path, 0, 2),
140 () => checkLocked(file.path, 0, 3),
141 () => checkNotLocked(file.path, 4, 5),
142 () => checkLocked(file.path, 4, 6),
143 () => checkLocked(file.path, 6),
144 () => checkNotLocked(file.path, 7),
145 () => raf1.unlock(2, 3),
146 () => checkNotLocked(file.path, 0, 5),
147 () => checkLocked(file.path, 4, 6),
148 () => checkLocked(file.path, 6),
149 () => checkNotLocked(file.path, 7),
150 ];
151 // On Windows regions unlocked must match regions locked.
152 if (!Platform.isWindows) {
153 tests.addAll([
154 () => raf1.unlock(5, 6),
155 () => checkNotLocked(file.path, 0, 6),
156 () => checkLocked(file.path, 6),
157 () => checkNotLocked(file.path, 7),
158 () => raf2.unlock(6, 7),
159 () => checkNotLocked(file.path)
160 ]);
161 } else {
162 tests.addAll([
163 () => raf2.unlock(5, 7),
164 () => checkNotLocked(file.path)
165 ]);
166 }
167 Future.forEach(tests,
168 (f) => f()).then((_) {
169 raf1.closeSync();
170 raf2.closeSync();
171 directory.deleteSync(recursive: true);
172 asyncEnd();
173 });
174 }
175
176 void testLockEnd() {
177 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
178 File file = new File(join(directory.path, "file"));
179 file.writeAsBytesSync(new List.filled(10, 0));
180 var raf = file.openSync(mode: APPEND);
181 asyncStart();
182 Future.forEach([
183 () => raf.lockSync(2),
184 () => checkNotLocked(file.path, 0, 2),
185 () => checkLocked(file.path, 0, 3),
186 () => checkLocked(file.path, 9),
187 () => raf.writeFromSync(new List.filled(10, 0)),
188 () => checkLocked(file.path, 10),
189 () => checkLocked(file.path, 19),
190 () => raf.unlockSync(2),
191 () => checkNotLocked(file.path)
192 ],
193 (f) => f()).then((_) {
194 raf.closeSync();
195 directory.deleteSync(recursive: true);
196 asyncEnd();
197 });
198 }
199
200 void testLockEndAsync() {
201 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
202 File file = new File(join(directory.path, "file"));
203 file.writeAsBytesSync(new List.filled(10, 0));
204 var raf = file.openSync(mode: APPEND);
205 asyncStart();
206 Future.forEach([
207 () => raf.lock(2),
208 () => checkNotLocked(file.path, 0, 2),
209 () => checkLocked(file.path, 0, 3),
210 () => checkLocked(file.path, 9),
211 () => raf.writeFromSync(new List.filled(10, 0)),
212 () => checkLocked(file.path, 10),
213 () => checkLocked(file.path, 19),
214 () => raf.unlock(2),
215 () => checkNotLocked(file.path)
216 ],
217 (f) => f()).then((_) {
218 raf.closeSync();
219 directory.deleteSync(recursive: true);
220 asyncEnd();
221 });
222 }
223
224 void testLockShared() {
225 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
226 File file = new File(join(directory.path, "file"));
227 file.writeAsBytesSync(new List.filled(10, 0));
228 var raf = file.openSync();
229 asyncStart();
230 Future.forEach([
231 () => raf.lock(null, null, FileLock.SHARED),
232 () => checkLocked(file.path),
233 () => checkLocked(file.path, 0, 2),
234 () => checkNotLocked(file.path, 0, 2, FileLock.SHARED)
235 ],
236 (f) => f()).then((_) {
237 raf.closeSync();
238 directory.deleteSync(recursive: true);
239 asyncEnd();
240 });
241 }
242
243 void testLockSharedAsync() {
244 Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
245 File file = new File(join(directory.path, "file"));
246 file.writeAsBytesSync(new List.filled(10, 0));
247 var raf = file.openSync();
248 asyncStart();
249 Future.forEach([
250 () => raf.lock(null, null, FileLock.SHARED),
251 () => checkLocked(file.path),
252 () => checkLocked(file.path, 0, 2),
253 () => checkNotLocked(file.path, 0, 2, FileLock.SHARED)
254 ],
255 (f) => f()).then((_) {
256 raf.closeSync();
257 directory.deleteSync(recursive: true);
258 asyncEnd();
259 });
260 }
261
262 void main() {
263 testLockWholeFile();
264 testLockWholeFileAsync();
265 testLockRange();
266 testLockRangeAsync();
267 testLockEnd();
268 testLockEndAsync();
269 testLockShared();
270 testLockSharedAsync();
Lasse Reichstein Nielsen 2015/01/08 11:59:22 What happens if the lock range is larger than the
Søren Gjesse 2015/01/09 13:06:20 Done.
271 }
OLDNEW
« tests/standalone/io/file_lock_script.dart ('K') | « tests/standalone/io/file_lock_script.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698