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

Side by Side 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, 3 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
« no previous file with comments | « pkg/analyzer/lib/src/dart/analysis/mutex.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2017, 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 import 'dart:async';
6
7 import 'package:analyzer/src/dart/analysis/mutex.dart';
8 import 'package:test/test.dart';
9 import 'package:test_reflective_loader/test_reflective_loader.dart';
10
11 main() {
12 defineReflectiveSuite(() {
13 defineReflectiveTests(MutexTest);
14 });
15 }
16
17 @reflectiveTest
18 class MutexTest {
19 test_acquire() async {
20 var values = <int>[];
21 var mutex = new Mutex();
22 await Future.wait([
23 new Future(() async {
24 await mutex.acquire();
25 try {
26 await new Future.delayed(new Duration(milliseconds: 10));
27 values.add(1);
28 } finally {
29 mutex.release();
30 }
31 }),
32 new Future(() async {
33 await mutex.acquire();
34 try {
35 values.add(2);
36 } finally {
37 mutex.release();
38 }
39 }),
40 ]);
41 // The first Future is schedule first, and it acquires the mutex first.
42 // But then it sleeps before adding (1), so if Mutex locking does not work,
43 // the second Future might add (2) first.
44 expect(values, [1, 2]);
45 }
46
47 test_guard() async {
48 var values = <int>[];
49 var mutex = new Mutex();
50 await Future.wait([
51 new Future(() async {
52 await mutex.guard(() async {
53 await new Future.delayed(new Duration(milliseconds: 10));
54 values.add(1);
55 });
56 }),
57 new Future(() async {
58 await mutex.guard(() async {
59 values.add(2);
60 });
61 }),
62 ]);
63 // The first Future is schedule first, and it acquires the mutex first.
64 // But then it sleeps before adding (1), so if Mutex locking does not work,
65 // the second Future might add (2) first.
66 expect(values, [1, 2]);
67 }
68
69 test_release_noLock() {
70 var mutex = new Mutex();
71 expect(() {
72 mutex.release();
73 }, throwsStateError);
74 }
75
76 test_release_noLock_alreadyReleased() async {
77 var mutex = new Mutex();
78 await mutex.acquire();
79 mutex.release();
80 expect(() {
81 mutex.release();
82 }, throwsStateError);
83 }
84 }
OLDNEW
« 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