OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 the basic StreamController and StreamController.singleSubscription. | 5 // Test the basic StreamController and StreamController.singleSubscription. |
6 library stream_controller_async_test; | 6 library stream_controller_async_test; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 import '../../../pkg/unittest/lib/unittest.dart'; | 10 import '../../../pkg/unittest/lib/unittest.dart'; |
11 import 'event_helper.dart'; | 11 import 'event_helper.dart'; |
12 | 12 |
13 testController() { | 13 testController() { |
14 // Test reduce | 14 // Test reduce |
15 test("StreamController.reduce", () { | 15 test("StreamController.reduce", () { |
16 StreamController c = new StreamController(); | 16 StreamController c = new StreamController.multiSubscription(); |
17 c.reduce(0, (a,b) => a + b) | 17 c.reduce(0, (a,b) => a + b) |
18 .then(expectAsync1((int v) { | 18 .then(expectAsync1((int v) { |
19 Expect.equals(42, v); | 19 Expect.equals(42, v); |
20 })); | 20 })); |
21 c.add(10); | 21 c.add(10); |
22 c.add(32); | 22 c.add(32); |
23 c.close(); | 23 c.close(); |
24 }); | 24 }); |
25 | 25 |
26 test("StreamController.reduce throws", () { | 26 test("StreamController.reduce throws", () { |
27 StreamController c = new StreamController(); | 27 StreamController c = new StreamController.multiSubscription(); |
28 c.reduce(0, (a,b) { throw "Fnyf!"; }) | 28 c.reduce(0, (a,b) { throw "Fnyf!"; }) |
29 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); | 29 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); |
30 c.add(42); | 30 c.add(42); |
31 }); | 31 }); |
32 | 32 |
33 test("StreamController.pipeInto", () { | 33 test("StreamController.pipeInto", () { |
34 StreamController c = new StreamController(); | 34 StreamController c = new StreamController.multiSubscription(); |
35 var list = <int>[]; | 35 var list = <int>[]; |
36 c.pipeInto(new CollectionSink<int>(list)) | 36 c.pipeInto(new CollectionSink<int>(list)) |
37 .whenComplete(expectAsync0(() { | 37 .whenComplete(expectAsync0(() { |
38 Expect.listEquals(<int>[1,2,9,3,9], list); | 38 Expect.listEquals(<int>[1,2,9,3,9], list); |
39 })); | 39 })); |
40 c.add(1); | 40 c.add(1); |
41 c.add(2); | 41 c.add(2); |
42 c.add(9); | 42 c.add(9); |
43 c.add(3); | 43 c.add(3); |
44 c.add(9); | 44 c.add(9); |
45 c.close(); | 45 c.close(); |
46 }); | 46 }); |
47 } | 47 } |
48 | 48 |
49 testSingleController() { | 49 testSingleController() { |
50 test("Single-subscription StreamController.reduce", () { | 50 test("Single-subscription StreamController.reduce", () { |
51 StreamController c = new StreamController.singleSubscription(); | 51 StreamController c = new StreamController(); |
52 c.reduce(0, (a,b) => a + b) | 52 c.reduce(0, (a,b) => a + b) |
53 .then(expectAsync1((int v) { Expect.equals(42, v); })); | 53 .then(expectAsync1((int v) { Expect.equals(42, v); })); |
54 c.add(10); | 54 c.add(10); |
55 c.add(32); | 55 c.add(32); |
56 c.close(); | 56 c.close(); |
57 }); | 57 }); |
58 | 58 |
59 test("Single-subscription StreamController.reduce throws", () { | 59 test("Single-subscription StreamController.reduce throws", () { |
60 StreamController c = new StreamController.singleSubscription(); | 60 StreamController c = new StreamController(); |
61 c.reduce(0, (a,b) { throw "Fnyf!"; }) | 61 c.reduce(0, (a,b) { throw "Fnyf!"; }) |
62 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); | 62 .catchError(expectAsync1((e) { Expect.equals("Fnyf!", e.error); })); |
63 c.add(42); | 63 c.add(42); |
64 }); | 64 }); |
65 | 65 |
66 test("Single-subscription StreamController.pipeInto", () { | 66 test("Single-subscription StreamController.pipeInto", () { |
67 StreamController c = new StreamController.singleSubscription(); | 67 StreamController c = new StreamController(); |
68 var list = <int>[]; | 68 var list = <int>[]; |
69 c.pipeInto(new CollectionSink<int>(list)) | 69 c.pipeInto(new CollectionSink<int>(list)) |
70 .whenComplete(expectAsync0(() { | 70 .whenComplete(expectAsync0(() { |
71 Expect.listEquals(<int>[1,2,9,3,9], list); | 71 Expect.listEquals(<int>[1,2,9,3,9], list); |
72 })); | 72 })); |
73 c.add(1); | 73 c.add(1); |
74 c.add(2); | 74 c.add(2); |
75 c.add(9); | 75 c.add(9); |
76 c.add(3); | 76 c.add(3); |
77 c.add(9); | 77 c.add(9); |
78 c.close(); | 78 c.close(); |
79 }); | 79 }); |
80 | 80 |
81 test("Single-subscription StreamController subscription changes", () { | 81 test("Single-subscription StreamController subscription changes", () { |
82 StreamController c = new StreamController.singleSubscription(); | 82 StreamController c = new StreamController(); |
83 StreamSink sink = c.sink; | 83 StreamSink sink = c.sink; |
84 Stream stream = c.stream; | 84 Stream stream = c.stream; |
85 int counter = 0; | 85 int counter = 0; |
86 var subscription; | 86 var subscription; |
87 subscription = stream.listen((data) { | 87 subscription = stream.listen((data) { |
88 counter += data; | 88 counter += data; |
89 Expect.throws(() => stream.listen(null), (e) => e is StateError); | 89 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
90 subscription.cancel(); | 90 subscription.cancel(); |
91 stream.listen((data) { | 91 stream.listen((data) { |
92 counter += data * 10; | 92 counter += data * 10; |
93 }, | 93 }, |
94 onDone: expectAsync0(() { | 94 onDone: expectAsync0(() { |
95 Expect.equals(1 + 20, counter); | 95 Expect.equals(1 + 20, counter); |
96 })); | 96 })); |
97 }); | 97 }); |
98 sink.add(1); | 98 sink.add(1); |
99 sink.add(2); | 99 sink.add(2); |
100 sink.close(); | 100 sink.close(); |
101 }); | 101 }); |
102 | 102 |
103 test("Single-subscription StreamController events are buffered when" | 103 test("Single-subscription StreamController events are buffered when" |
104 " there is no subscriber", | 104 " there is no subscriber", |
105 () { | 105 () { |
106 StreamController c = new StreamController.singleSubscription(); | 106 StreamController c = new StreamController(); |
107 StreamSink sink = c.sink; | 107 StreamSink sink = c.sink; |
108 Stream stream = c.stream; | 108 Stream stream = c.stream; |
109 int counter = 0; | 109 int counter = 0; |
110 sink.add(1); | 110 sink.add(1); |
111 sink.add(2); | 111 sink.add(2); |
112 sink.close(); | 112 sink.close(); |
113 stream.listen( | 113 stream.listen( |
114 (data) { | 114 (data) { |
115 counter += data; | 115 counter += data; |
116 }, | 116 }, |
117 onDone: expectAsync0(() { | 117 onDone: expectAsync0(() { |
118 Expect.equals(3, counter); | 118 Expect.equals(3, counter); |
119 })); | 119 })); |
120 }); | 120 }); |
121 | 121 |
122 // Test subscription changes while firing. | 122 // Test subscription changes while firing. |
123 test("Single-subscription StreamController subscription changes while firing", | 123 test("Single-subscription StreamController subscription changes while firing", |
124 () { | 124 () { |
125 StreamController c = new StreamController.singleSubscription(); | 125 StreamController c = new StreamController(); |
126 StreamSink sink = c.sink; | 126 StreamSink sink = c.sink; |
127 Stream stream = c.stream; | 127 Stream stream = c.stream; |
128 int counter = 0; | 128 int counter = 0; |
129 var subscription = stream.listen(null); | 129 var subscription = stream.listen(null); |
130 subscription.onData(expectAsync1((data) { | 130 subscription.onData(expectAsync1((data) { |
131 counter += data; | 131 counter += data; |
132 subscription.cancel(); | 132 subscription.cancel(); |
133 stream.listen((data) { | 133 stream.listen((data) { |
134 counter += 10 * data; | 134 counter += 10 * data; |
135 }, | 135 }, |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 testFuture("reduce", (s, act) => s.reduce(0, (a,b) => act(b))); | 445 testFuture("reduce", (s, act) => s.reduce(0, (a,b) => act(b))); |
446 } | 446 } |
447 | 447 |
448 main() { | 448 main() { |
449 testController(); | 449 testController(); |
450 testSingleController(); | 450 testSingleController(); |
451 testExtraMethods(); | 451 testExtraMethods(); |
452 testPause(); | 452 testPause(); |
453 testRethrow(); | 453 testRethrow(); |
454 } | 454 } |
OLD | NEW |