OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 // This is a regression test for issue 22853. | |
6 | |
7 import "dart:async"; | |
8 import "package:expect/expect.dart"; | |
9 import "package:async_helper/async_helper.dart"; | |
10 | |
11 main() { | |
12 var list = []; | |
13 var sync = new Sync(); | |
14 f() async* { | |
15 list.add("*1"); | |
16 yield 1; | |
17 await sync.wait(); | |
18 sync.release(); | |
19 list.add("*2"); | |
20 yield 2; | |
21 list.add("*3"); | |
22 } | |
23 | |
24 ; | |
25 var stream = f(); | |
26 var sub = stream.listen(list.add); | |
27 | |
28 asyncStart(); | |
29 return sync.wait().whenComplete(() { | |
30 Expect.listEquals(list, ["*1", 1]); | |
31 sub.pause(); | |
32 return sync.wait(); | |
33 }).whenComplete(() { | |
34 Expect.listEquals(list, ["*1", 1, "*2"]); | |
35 sub.cancel(); | |
36 new Future.delayed(new Duration(milliseconds: 200), () { | |
37 // Should not have yielded 2 or added *3 while paused. | |
38 Expect.listEquals(list, ["*1", 1, "*2"]); | |
39 asyncEnd(); | |
40 }); | |
41 }); | |
42 } | |
43 | |
44 /** | |
45 * Allows two asynchronous executions to synchronize. | |
46 * | |
47 * Calling [wait] and waiting for the returned future to complete will | |
48 * wait for the other executions to call [wait] again. At that point, | |
49 * the waiting execution is allowed to continue (the returned future completes), | |
50 * and the more recent call to [wait] is now the waiting execution. | |
51 */ | |
52 class Sync { | |
53 Completer _completer = null; | |
54 // Release whoever is currently waiting and start waiting yourself. | |
55 Future wait([v]) { | |
56 if (_completer != null) _completer.complete(v); | |
57 _completer = new Completer(); | |
58 return _completer.future; | |
59 } | |
60 | |
61 // Release whoever is currently waiting. | |
62 void release([v]) { | |
63 if (_completer != null) { | |
64 _completer.complete(v); | |
65 _completer = null; | |
66 } | |
67 } | |
68 } | |
OLD | NEW |