| OLD | NEW |
| (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 } |
| OLD | NEW |