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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: tests/standalone/io/file_lock_test.dart
diff --git a/tests/standalone/io/file_lock_test.dart b/tests/standalone/io/file_lock_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..711178fe33cacf82cfbf33b327908919d4aa4221
--- /dev/null
+++ b/tests/standalone/io/file_lock_test.dart
@@ -0,0 +1,271 @@
+// 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.
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:async';
+import 'dart:io';
+
+import "package:async_helper/async_helper.dart";
+import "package:expect/expect.dart";
+import "package:path/path.dart";
+
+// 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.
+check(String path, int start, int end, FileLock mode, {bool locked}) {
+ // Client process returns either 'LOCK FAILED' or 'LOCK SUCCEEDED'.
+ var expected = locked ? 'LOCK FAILED' : 'LOCK SUCCEEDED';
+ var arguments = []
+ ..addAll(Platform.executableArguments)
+ ..add(Platform.script.resolve('file_lock_script.dart').toFilePath())
+ ..add(path)
+ ..add('$start')
+ ..add('$end')
+ ..add(mode == FileLock.EXCLUSIVE ? 'EXCLUSIVE' : 'SHARED');
+ return Process.run(Platform.executable, arguments)
+ .then((ProcessResult result) {
+ if (result.exitCode != 0 || !result.stdout.contains(expected)) {
+ print("Client failed, exit code ${result.exitCode}");
+ print(" stdout:");
+ print(result.stdout);
+ print(" stderr:");
+ print(result.stderr);
+ print(" arguments:");
+ print(arguments);
+ Expect.fail('Client subprocess exit code: ${result.exitCode}');
+ }
+ });
+}
+
+checkLocked(String path,
+ [int start, int end, FileLock mode = FileLock.EXCLUSIVE]) =>
+ check(path, start, end, mode, locked: true);
+
+checkNotLocked(String path,
+ [int start, int end, FileLock mode = FileLock.EXCLUSIVE]) =>
+ check(path, start, end, mode, locked: false);
+
+void testLockWholeFile() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf = file.openSync(mode: WRITE);
+ raf.lockSync();
+ checkLocked(file.path).then((_) {
+ checkLocked(file.path, 0, 2).then((_) {
+ raf.unlockSync();
+ checkNotLocked(file.path).then((_) {
+ raf.closeSync();
+ 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.
+ });
+ });
+ });
+}
+
+void testLockWholeFileAsync() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf = file.openSync(mode: WRITE);
+ asyncStart();
+ Future.forEach([
+ () => raf.lock(),
+ () => checkLocked(file.path, 0, 2),
+ () => checkLocked(file.path),
+ () => raf.unlock(),
+ () => checkNotLocked(file.path),
+ ],
+ (f) => f()).then((_) {
+ raf.closeSync();
+ directory.deleteSync(recursive: true);
+ asyncEnd();
+ });
+}
+
+void testLockRange() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf1 = file.openSync(mode: WRITE);
+ var raf2 = file.openSync(mode: WRITE);
+ asyncStart();
+ var tests = [
+ () => raf1.lockSync(2, 3),
+ () => raf2.lockSync(5, 7),
+ () => checkNotLocked(file.path, 0, 2),
+ () => checkLocked(file.path, 0, 3),
+ () => checkNotLocked(file.path, 4, 5),
+ () => checkLocked(file.path, 4, 6),
+ () => checkLocked(file.path, 6),
+ () => checkNotLocked(file.path, 7),
+ () => raf1.unlockSync(2, 3),
+ () => checkNotLocked(file.path, 0, 5),
+ () => checkLocked(file.path, 4, 6),
+ () => checkLocked(file.path, 6),
+ () => checkNotLocked(file.path, 7),
+ ];
+ // On Windows regions unlocked must match regions locked.
+ if (!Platform.isWindows) {
+ tests.addAll([
+ () => raf1.unlockSync(5, 6),
+ () => checkNotLocked(file.path, 0, 6),
+ () => checkLocked(file.path, 6),
+ () => checkNotLocked(file.path, 7),
+ () => raf2.unlockSync(6, 7),
+ () => checkNotLocked(file.path)
+ ]);
+ } else {
+ tests.addAll([
+ () => raf2.unlockSync(5, 7),
+ () => checkNotLocked(file.path)
+ ]);
+ }
+ Future.forEach(tests, (f) => f()).then((_) {
+ raf1.closeSync();
+ raf2.closeSync();
+ directory.deleteSync(recursive: true);
+ asyncEnd();
+ });
+}
+
+void testLockRangeAsync() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf1 = file.openSync(mode: WRITE);
+ var raf2 = file.openSync(mode: WRITE);
+ asyncStart();
+ var tests = [
+ () => raf1.lock(2, 3),
+ () => raf2.lock(5, 7),
+ () => checkNotLocked(file.path, 0, 2),
+ () => checkLocked(file.path, 0, 3),
+ () => checkNotLocked(file.path, 4, 5),
+ () => checkLocked(file.path, 4, 6),
+ () => checkLocked(file.path, 6),
+ () => checkNotLocked(file.path, 7),
+ () => raf1.unlock(2, 3),
+ () => checkNotLocked(file.path, 0, 5),
+ () => checkLocked(file.path, 4, 6),
+ () => checkLocked(file.path, 6),
+ () => checkNotLocked(file.path, 7),
+ ];
+ // On Windows regions unlocked must match regions locked.
+ if (!Platform.isWindows) {
+ tests.addAll([
+ () => raf1.unlock(5, 6),
+ () => checkNotLocked(file.path, 0, 6),
+ () => checkLocked(file.path, 6),
+ () => checkNotLocked(file.path, 7),
+ () => raf2.unlock(6, 7),
+ () => checkNotLocked(file.path)
+ ]);
+ } else {
+ tests.addAll([
+ () => raf2.unlock(5, 7),
+ () => checkNotLocked(file.path)
+ ]);
+ }
+ Future.forEach(tests,
+ (f) => f()).then((_) {
+ raf1.closeSync();
+ raf2.closeSync();
+ directory.deleteSync(recursive: true);
+ asyncEnd();
+ });
+}
+
+void testLockEnd() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf = file.openSync(mode: APPEND);
+ asyncStart();
+ Future.forEach([
+ () => raf.lockSync(2),
+ () => checkNotLocked(file.path, 0, 2),
+ () => checkLocked(file.path, 0, 3),
+ () => checkLocked(file.path, 9),
+ () => raf.writeFromSync(new List.filled(10, 0)),
+ () => checkLocked(file.path, 10),
+ () => checkLocked(file.path, 19),
+ () => raf.unlockSync(2),
+ () => checkNotLocked(file.path)
+ ],
+ (f) => f()).then((_) {
+ raf.closeSync();
+ directory.deleteSync(recursive: true);
+ asyncEnd();
+ });
+}
+
+void testLockEndAsync() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf = file.openSync(mode: APPEND);
+ asyncStart();
+ Future.forEach([
+ () => raf.lock(2),
+ () => checkNotLocked(file.path, 0, 2),
+ () => checkLocked(file.path, 0, 3),
+ () => checkLocked(file.path, 9),
+ () => raf.writeFromSync(new List.filled(10, 0)),
+ () => checkLocked(file.path, 10),
+ () => checkLocked(file.path, 19),
+ () => raf.unlock(2),
+ () => checkNotLocked(file.path)
+ ],
+ (f) => f()).then((_) {
+ raf.closeSync();
+ directory.deleteSync(recursive: true);
+ asyncEnd();
+ });
+}
+
+void testLockShared() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf = file.openSync();
+ asyncStart();
+ Future.forEach([
+ () => raf.lock(null, null, FileLock.SHARED),
+ () => checkLocked(file.path),
+ () => checkLocked(file.path, 0, 2),
+ () => checkNotLocked(file.path, 0, 2, FileLock.SHARED)
+ ],
+ (f) => f()).then((_) {
+ raf.closeSync();
+ directory.deleteSync(recursive: true);
+ asyncEnd();
+ });
+}
+
+void testLockSharedAsync() {
+ Directory directory = Directory.systemTemp.createTempSync('dart_file_lock');
+ File file = new File(join(directory.path, "file"));
+ file.writeAsBytesSync(new List.filled(10, 0));
+ var raf = file.openSync();
+ asyncStart();
+ Future.forEach([
+ () => raf.lock(null, null, FileLock.SHARED),
+ () => checkLocked(file.path),
+ () => checkLocked(file.path, 0, 2),
+ () => checkNotLocked(file.path, 0, 2, FileLock.SHARED)
+ ],
+ (f) => f()).then((_) {
+ raf.closeSync();
+ directory.deleteSync(recursive: true);
+ asyncEnd();
+ });
+}
+
+void main() {
+ testLockWholeFile();
+ testLockWholeFileAsync();
+ testLockRange();
+ testLockRangeAsync();
+ testLockEnd();
+ testLockEndAsync();
+ testLockShared();
+ 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.
+}
« 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