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

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

Issue 2050413002: Adds blocking file locks. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Update docs and CHANGELOG Created 4 years, 6 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // This test works by spawning a new process running
6 // file_blocking_lock_script.dart, trading the file lock back and forth,
7 // writing bytes 1 ... 25 in order to the file. There are checks to ensure
8 // that the bytes are written in order, that one process doesn't write all the
9 // bytes and that a non-blocking lock fails such that a blocking lock must
10 // be taken, which succeeds.
11
12 import 'dart:async';
13 import 'dart:convert';
14 import 'dart:io';
15
16 import "package:async_helper/async_helper.dart";
17 import "package:expect/expect.dart";
18 import "package:path/path.dart";
19
20 // Check whether the file is locked or not.
21 runPeer(String path, int len, FileLock mode) {
22 var script = Platform.script.resolve(
23 'file_blocking_lock_script.dart').toFilePath();
24 var arguments = []
25 ..addAll(Platform.executableArguments)
26 ..add(script)
27 ..add(path)
28 ..add(len.toString());
29 return Process.start(Platform.executable, arguments).then((process) {
30 process.stdout
31 .transform(UTF8.decoder)
32 .listen((data) { print(data); });
33 process.stderr
34 .transform(UTF8.decoder)
35 .listen((data) { print(data); });
36 return process;
37 });
38 }
39
40 testLockWholeFile() async {
41 const int length = 25;
42 asyncStart();
43 Directory directory = await Directory.systemTemp.createTemp('dart_file_lock');
44 File file = new File(join(directory.path, "file"));
45 await file.writeAsBytes(new List.filled(length, 0));
46 var raf = await file.open(mode: APPEND);
47 await raf.setPosition(0);
48 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, length);
49 Process peer = await runPeer(file.path, length, FileLock.BLOCKING_EXCLUSIVE);
50
51 // Wait a bit for the other process to get started. We'll synchronize on
52 // the file lock.
53 await new Future.delayed(const Duration(seconds: 1));
54
55 int nextToWrite = 1;
56 int at = 0;
57 List iWrote = new List.filled(length, 0);
58 bool nonBlockingFailed = false;
59 while (nextToWrite <= length) {
60 int p = await raf.position();
61 await raf.writeByte(nextToWrite);
62 await raf.flush();
63 // Record which bytes this process wrote so that we can check that the
64 // other process was able to take the lock and write some bytes.
65 iWrote[nextToWrite-1] = nextToWrite;
66 nextToWrite++;
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 while (true) {
76 p = await raf.position();
77 at = await raf.readByte();
78 if (at == 0 || at == -1) break;
79 nextToWrite++;
80 }
81 await raf.setPosition(p);
82 }
83
84 await raf.setPosition(0);
85 for (int i = 1; i <= length; i++) {
86 Expect.equals(i, await raf.readByte());
87 }
88 await raf.unlock(0, length);
89
90 bool wroteAll = true;
91 for (int i = 0; i < length; i++) {
92 // If there's a 0 entry, this process didn't write all bytes.
93 wroteAll = wroteAll && (iWrote[i] == 0);
94 }
95 Expect.equals(false, wroteAll);
96
97 Expect.equals(true, nonBlockingFailed);
98
99 peer.exitCode.then((v) {
100 Expect.equals(0, v);
101 raf.closeSync();
102 directory.deleteSync(recursive: true);
103 asyncEnd();
104 });
105 }
106
107 main() {
108 testLockWholeFile();
109 }
OLDNEW
« sdk/lib/io/file.dart ('K') | « 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