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

Unified Diff: pkg/analyzer/test/src/dart/analysis/mutex_test.dart

Issue 3006563003: Implement Mutex for Dart. (Closed)
Patch Set: Add Mutex.guard(). Created 3 years, 4 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
« no previous file with comments | « pkg/analyzer/lib/src/dart/analysis/mutex.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/test/src/dart/analysis/mutex_test.dart
diff --git a/pkg/analyzer/test/src/dart/analysis/mutex_test.dart b/pkg/analyzer/test/src/dart/analysis/mutex_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..34a8d864d649cd9e53a3abdebb5f4afad36f25ba
--- /dev/null
+++ b/pkg/analyzer/test/src/dart/analysis/mutex_test.dart
@@ -0,0 +1,84 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// 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 'package:analyzer/src/dart/analysis/mutex.dart';
+import 'package:test/test.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(MutexTest);
+ });
+}
+
+@reflectiveTest
+class MutexTest {
+ test_acquire() async {
+ var values = <int>[];
+ var mutex = new Mutex();
+ await Future.wait([
+ new Future(() async {
+ await mutex.acquire();
+ try {
+ await new Future.delayed(new Duration(milliseconds: 10));
+ values.add(1);
+ } finally {
+ mutex.release();
+ }
+ }),
+ new Future(() async {
+ await mutex.acquire();
+ try {
+ values.add(2);
+ } finally {
+ mutex.release();
+ }
+ }),
+ ]);
+ // The first Future is schedule first, and it acquires the mutex first.
+ // But then it sleeps before adding (1), so if Mutex locking does not work,
+ // the second Future might add (2) first.
+ expect(values, [1, 2]);
+ }
+
+ test_guard() async {
+ var values = <int>[];
+ var mutex = new Mutex();
+ await Future.wait([
+ new Future(() async {
+ await mutex.guard(() async {
+ await new Future.delayed(new Duration(milliseconds: 10));
+ values.add(1);
+ });
+ }),
+ new Future(() async {
+ await mutex.guard(() async {
+ values.add(2);
+ });
+ }),
+ ]);
+ // The first Future is schedule first, and it acquires the mutex first.
+ // But then it sleeps before adding (1), so if Mutex locking does not work,
+ // the second Future might add (2) first.
+ expect(values, [1, 2]);
+ }
+
+ test_release_noLock() {
+ var mutex = new Mutex();
+ expect(() {
+ mutex.release();
+ }, throwsStateError);
+ }
+
+ test_release_noLock_alreadyReleased() async {
+ var mutex = new Mutex();
+ await mutex.acquire();
+ mutex.release();
+ expect(() {
+ mutex.release();
+ }, throwsStateError);
+ }
+}
« no previous file with comments | « pkg/analyzer/lib/src/dart/analysis/mutex.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698