OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Test empty stream. | 5 // Test empty stream. |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import "dart:async"; | 7 import "dart:async"; |
8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
9 | 9 |
10 main() { | 10 main() { |
11 asyncStart(); | 11 asyncStart(); |
12 runTest().whenComplete(asyncEnd); | 12 runTest().whenComplete(asyncEnd); |
13 } | 13 } |
14 | 14 |
15 Future runTest() async { | 15 Future runTest() async { |
16 unreachable([a,b]) { throw "UNREACHABLE"; } | 16 unreachable([a, b]) { |
| 17 throw "UNREACHABLE"; |
| 18 } |
| 19 |
17 int tick = 0; | 20 int tick = 0; |
18 ticker() { tick++; } | 21 ticker() { |
| 22 tick++; |
| 23 } |
19 | 24 |
20 asyncStart(); | 25 asyncStart(); |
21 | 26 |
22 Stream<int> s = const Stream<int>.empty(); // Is const constructor. | 27 Stream<int> s = const Stream<int>.empty(); // Is const constructor. |
23 Expect.isFalse(s is Stream<String>); // Respects type parameter. | 28 Expect.isFalse(s is Stream<String>); // Respects type parameter. |
24 StreamSubscription<int> sub = | 29 StreamSubscription<int> sub = |
25 s.listen(unreachable, onError: unreachable, onDone: ticker); | 30 s.listen(unreachable, onError: unreachable, onDone: ticker); |
26 Expect.isFalse(sub is StreamSubscription<String>); // Type parameter in sub. | 31 Expect.isFalse(sub is StreamSubscription<String>); // Type parameter in sub. |
27 | 32 |
28 // Doesn't do callback in response to listen. | 33 // Doesn't do callback in response to listen. |
29 Expect.equals(tick, 0); | 34 Expect.equals(tick, 0); |
30 await flushMicrotasks(); | 35 await flushMicrotasks(); |
31 // Completes eventually. | 36 // Completes eventually. |
32 Expect.equals(tick, 1); | 37 Expect.equals(tick, 1); |
33 | 38 |
34 // It's a broadcast stream - can listen twice. | 39 // It's a broadcast stream - can listen twice. |
35 Expect.isTrue(s.isBroadcast); | 40 Expect.isTrue(s.isBroadcast); |
36 StreamSubscription<int> sub2 = | 41 StreamSubscription<int> sub2 = |
37 s.listen(unreachable, onError: unreachable, onDone: unreachable); | 42 s.listen(unreachable, onError: unreachable, onDone: unreachable); |
38 // respects pause. | 43 // respects pause. |
39 sub2.pause(); | 44 sub2.pause(); |
40 await flushMicrotasks(); | 45 await flushMicrotasks(); |
41 // respects cancel. | 46 // respects cancel. |
42 sub2.cancel(); | 47 sub2.cancel(); |
43 await flushMicrotasks(); | 48 await flushMicrotasks(); |
44 Expect.equals(tick, 1); | 49 Expect.equals(tick, 1); |
45 // Still not complete. | 50 // Still not complete. |
46 | 51 |
47 StreamSubscription<int> sub3 = | 52 StreamSubscription<int> sub3 = |
48 s.listen(unreachable, onError: unreachable, onDone: ticker); | 53 s.listen(unreachable, onError: unreachable, onDone: ticker); |
49 // respects pause. | 54 // respects pause. |
50 sub3.pause(); | 55 sub3.pause(); |
51 Expect.equals(tick, 1); | 56 Expect.equals(tick, 1); |
52 await flushMicrotasks(); | 57 await flushMicrotasks(); |
53 // Doesn't complete while paused. | 58 // Doesn't complete while paused. |
54 Expect.equals(tick, 1); | 59 Expect.equals(tick, 1); |
55 sub3.resume(); | 60 sub3.resume(); |
56 await flushMicrotasks(); | 61 await flushMicrotasks(); |
57 // Now completed. | 62 // Now completed. |
58 Expect.equals(tick, 2); | 63 Expect.equals(tick, 2); |
59 | 64 |
60 asyncEnd(); | 65 asyncEnd(); |
61 } | 66 } |
62 | 67 |
63 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); | 68 Future flushMicrotasks() => new Future.delayed(Duration.ZERO); |
OLD | NEW |