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

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

Issue 3006563003: Implement Mutex for Dart. (Closed)
Patch Set: 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
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..219d72784999a6a7f5559a921843f8462e5edd56
--- /dev/null
+++ b/pkg/analyzer/test/src/dart/analysis/mutex_test.dart
@@ -0,0 +1,62 @@
+// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
Brian Wilkerson 2017/08/25 16:56:44 2017
+// 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_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);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698