OLD | NEW |
| (Empty) |
1 // Copyright (c) 2016, 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 import "package:expect/expect.dart"; | |
7 import "package:async_helper/async_helper.dart"; | |
8 | |
9 var events = []; | |
10 | |
11 ticker() async* { | |
12 var sc; | |
13 var sentTickCount = 0; | |
14 sc = new StreamController(onListen: () { | |
15 events.add("listen"); | |
16 }, onCancel: () { | |
17 events.add("cancel"); | |
18 }); | |
19 | |
20 try { | |
21 var counter = 0; | |
22 await for (var tick in sc.stream) { | |
23 counter++; | |
24 } | |
25 } finally { | |
26 events.add("finally"); | |
27 } | |
28 } | |
29 | |
30 void main() { | |
31 asyncStart(); | |
32 events.add("main"); | |
33 final subscription = ticker().listen((val) {}); | |
34 | |
35 bool cancelFinished = false; | |
36 // Cancel the subscription. | |
37 // The async* function is blocked on an `await` (the inner stream) and won't | |
38 // be able to complete. | |
39 Timer.run(() { | |
40 events.add("invoke cancel"); | |
41 subscription.cancel().then((_) => cancelFinished = true); | |
42 }); | |
43 | |
44 new Timer(const Duration(milliseconds: 100), () { | |
45 Expect.isFalse(cancelFinished); | |
46 Expect.listEquals(["main", "listen", "invoke cancel"], events); | |
47 asyncEnd(); | |
48 }); | |
49 } | |
OLD | NEW |