OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import "dart:async"; | 5 import "dart:async"; |
6 import "package:test/test.dart"; | 6 import "package:test/test.dart"; |
7 | 7 |
8 // Test that stream listener callbacks all happen in the zone where the | 8 // Test that stream listener callbacks all happen in the zone where the |
9 // listen occurred. | 9 // listen occurred. |
10 | 10 |
11 main() { | 11 main() { |
12 StreamController controller; | 12 StreamController controller; |
13 controller = new StreamController(); | 13 controller = new StreamController(); |
14 testStream("singlesub-async", controller, controller.stream); | 14 testStream("singlesub-async", controller, controller.stream); |
15 controller = new StreamController.broadcast(); | 15 controller = new StreamController.broadcast(); |
16 testStream("broadcast-async", controller, controller.stream); | 16 testStream("broadcast-async", controller, controller.stream); |
17 controller = new StreamController(); | 17 controller = new StreamController(); |
18 testStream("asbroadcast-async", controller, | 18 testStream( |
19 controller.stream.asBroadcastStream()); | 19 "asbroadcast-async", controller, controller.stream.asBroadcastStream()); |
20 | 20 |
21 controller = new StreamController(sync: true); | 21 controller = new StreamController(sync: true); |
22 testStream("singlesub-sync", controller, controller.stream); | 22 testStream("singlesub-sync", controller, controller.stream); |
23 controller = new StreamController.broadcast(sync: true); | 23 controller = new StreamController.broadcast(sync: true); |
24 testStream("broadcast-sync", controller, controller.stream); | 24 testStream("broadcast-sync", controller, controller.stream); |
25 controller = new StreamController(sync: true); | 25 controller = new StreamController(sync: true); |
26 testStream("asbroadcast-sync", controller, | 26 testStream( |
27 controller.stream.asBroadcastStream()); | 27 "asbroadcast-sync", controller, controller.stream.asBroadcastStream()); |
28 } | 28 } |
29 | 29 |
30 void testStream(String name, StreamController controller, Stream stream) { | 30 void testStream(String name, StreamController controller, Stream stream) { |
31 test(name, () { | 31 test(name, () { |
32 Zone outer = Zone.current; | 32 Zone outer = Zone.current; |
33 runZoned(() { | 33 runZoned(() { |
34 Zone newZone1 = Zone.current; | 34 Zone newZone1 = Zone.current; |
35 StreamSubscription sub; | 35 StreamSubscription sub; |
36 sub = stream.listen(expectAsync((v) { | 36 sub = stream.listen(expectAsync1((v) { |
37 expect(v, 42); | 37 expect(v, 42); |
38 expect(Zone.current, newZone1); | 38 expect(Zone.current, newZone1); |
39 outer.run(() { | 39 outer.run(() { |
40 sub.onData(expectAsync((v) { | 40 sub.onData(expectAsync1((v) { |
41 expect(v, 37); | 41 expect(v, 37); |
42 expect(Zone.current, newZone1); | 42 expect(Zone.current, newZone1); |
43 runZoned(() { | 43 runZoned(() { |
44 sub.onData(expectAsync((v) { | 44 sub.onData(expectAsync1((v) { |
45 expect(v, 87); | 45 expect(v, 87); |
46 expect(Zone.current, newZone1); | 46 expect(Zone.current, newZone1); |
47 })); | 47 })); |
48 }); | 48 }); |
49 if (controller is SynchronousStreamController) { | 49 if (controller is SynchronousStreamController) { |
50 scheduleMicrotask(() => controller.add(87)); | 50 scheduleMicrotask(() => controller.add(87)); |
51 } else { | 51 } else { |
52 controller.add(87); | 52 controller.add(87); |
53 } | 53 } |
54 })); | 54 })); |
55 }); | 55 }); |
56 if (controller is SynchronousStreamController) { | 56 if (controller is SynchronousStreamController) { |
57 scheduleMicrotask(() => controller.add(37)); | 57 scheduleMicrotask(() => controller.add(37)); |
58 } else { | 58 } else { |
59 controller.add(37); | 59 controller.add(37); |
60 } | 60 } |
61 })); | 61 })); |
62 }); | 62 }); |
63 controller.add(42); | 63 controller.add(42); |
64 }); | 64 }); |
65 } | 65 } |
OLD | NEW |