Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: pkg/sequence_zip/test/stream_test.dart

Issue 18015002: Add package with iterable and stream "zipping" functionality. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. More tests. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/sequence_zip/test/iterable_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import "dart:async";
6 import "package:sequence_zip/stream_zip.dart";
7 import "package:unittest/unittest.dart";
8
9 /// Create an error with the same values as [base], except that it throwsA
10 /// when seeing the value [errorValue].
11 Stream streamError(Stream base, int errorValue) {
12 return base.map((x) => (x == errorValue) ? throw "BAD" : x);
13 }
14
15 /// Make a [Stream] from an [Iterable] by adding events to a stream controller
16 /// at periodic intervals.
17 Stream mks(Iterable iterable) {
18 Iterator iterator = iterable.iterator;
19 StreamController controller = new StreamController();
20 // Some varying time between 3 and 10 ms.
21 int ms = ((++ctr) * 5) % 7 + 3;
22 new Timer.periodic(new Duration(milliseconds: ms), (Timer timer) {
23 if (iterator.moveNext()) {
24 controller.add(iterator.current);
25 } else {
26 controller.close();
27 timer.cancel();
28 }
29 });
30 return controller.stream;
31 }
32
33 /// Counter used to give varying delays for streams.
34 int ctr = 0;
35
36 main() {
37 // Test that zipping [streams] gives the results iterated by [expectedData].
38 testZip(Iterable streams, Iterable expectedData) {
39 List data = [];
40 Stream zip = new StreamZip(streams);
41 zip.listen(data.add, onDone: expectAsync0(() {
42 expect(data, equals(expectedData));
43 }));
44 }
45
46 test("Basic", () {
47 testZip([mks([1, 2, 3]), mks([4, 5, 6]), mks([7, 8, 9])],
48 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
49 });
50
51 test("Uneven length 1", () {
52 testZip([mks([1, 2, 3, 99, 100]), mks([4, 5, 6]), mks([7, 8, 9])],
53 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
54 });
55
56 test("Uneven length 2", () {
57 testZip([mks([1, 2, 3]), mks([4, 5, 6, 99, 100]), mks([7, 8, 9])],
58 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
59 });
60
61 test("Uneven length 3", () {
62 testZip([mks([1, 2, 3]), mks([4, 5, 6]), mks([7, 8, 9, 99, 100])],
63 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
64 });
65
66 test("Uneven length 4", () {
67 testZip([mks([1, 2, 3, 98]), mks([4, 5, 6]), mks([7, 8, 9, 99, 100])],
68 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
69 });
70
71 test("Empty 1", () {
72 testZip([mks([]), mks([4, 5, 6]), mks([7, 8, 9])], []);
73 });
74
75 test("Empty 2", () {
76 testZip([mks([1, 2, 3]), mks([]), mks([7, 8, 9])], []);
77 });
78
79 test("Empty 3", () {
80 testZip([mks([1, 2, 3]), mks([4, 5, 6]), mks([])], []);
81 });
82
83 test("Empty source", () {
84 testZip([], []);
85 });
86
87 test("Single Source", () {
88 testZip([mks([1, 2, 3])], [[1], [2], [3]]);
89 });
90
91 test("Other-streams", () {
92 Stream st1 = mks([1, 2, 3, 4, 5, 6]).where((x) => x < 4);
93 Stream st2 = new Stream.periodic(const Duration(milliseconds: 5),
94 (x) => x + 4).take(3);
95 StreamController c = new StreamController.broadcast();
96 Stream st3 = c.stream;
97 testZip([st1, st2, st3],
98 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
99 c..add(7)..add(8)..add(9)..close();
100 });
101
102 test("Error 1", () {
103 expect(new StreamZip([streamError(mks([1, 2, 3]), 2),
104 mks([4, 5, 6]),
105 mks([7, 8, 9])]).toList(),
106 throwsA(equals("BAD")));
107 });
108
109 test("Error 2", () {
110 expect(new StreamZip([mks([1, 2, 3]),
111 streamError(mks([4, 5, 6]), 5),
112 mks([7, 8, 9])]).toList(),
113 throwsA(equals("BAD")));
114 });
115
116 test("Error 3", () {
117 expect(new StreamZip([mks([1, 2, 3]),
118 mks([4, 5, 6]),
119 streamError(mks([7, 8, 9]), 8)]).toList(),
120 throwsA(equals("BAD")));
121 });
122
123 test("Error at end", () {
124 expect(new StreamZip([mks([1, 2, 3]),
125 streamError(mks([4, 5, 6]), 6),
126 mks([7, 8, 9])]).toList(),
127 throwsA(equals("BAD")));
128 });
129
130 test("Error before first end", () {
131 // StreamControllers' streams with no "close" called will never be done,
132 // so the fourth event of the first stream is guaranteed to come first.
133 expect(new StreamZip(
134 [streamError(mks([1, 2, 3, 4]), 4),
135 (new StreamController()..add(4)..add(5)..add(6)).stream,
136 (new StreamController()..add(7)..add(8)..add(9)).stream]
137 ).toList(),
138 throwsA(equals("BAD")));
139 });
140
141 test("Error after first end", () {
142 StreamController controller = new StreamController();
143 controller..add(7)..add(8)..add(9);
144 testZip([mks([1, 2, 3]),
145 mks([4, 5, 6]),
146 controller.stream],
147 [[1, 4, 7], [2, 5, 8], [3, 6, 9]]);
148 // This comes after the first three events in all cases, since they
149 // use durations no greater than 10 ms.
150 new Timer(const Duration(milliseconds: 100), () {
151 controller.addError("BAD");
152 });
153 });
154
155 test("Pause/Resume", () {
156 var done = expectAsync0((){}); // Call to complete test.
157
158 int sc1p = 0;
159 StreamController c1 = new StreamController(
160 onPause: () {
161 sc1p++;
162 },
163 onResume: () {
164 sc1p--;
165 });
166
167 int sc2p = 0;
168 StreamController c2 = new StreamController(
169 onPause: () {
170 sc2p++;
171 },
172 onResume: () {
173 sc2p--;
174 });
175 Stream zip = new StreamZip([c1.stream, c2.stream]);
176
177 const ms25 = const Duration(milliseconds: 25);
178
179 // StreamIterator uses pause and resume to control flow.
180 StreamIterator it = new StreamIterator(zip);
181
182 it.moveNext().then((hasMore) {
183 expect(hasMore, isTrue);
184 expect(it.current, equals([1, 2]));
185 return it.moveNext();
186 }).then((hasMore) {
187 expect(hasMore, isTrue);
188 expect(it.current, equals([3, 4]));
189 c2.add(6);
190 return it.moveNext();
191 }).then((hasMore) {
192 expect(hasMore, isTrue);
193 expect(it.current, equals([5, 6]));
194 new Future.delayed(ms25).then((_) { c2.add(8); });
195 return it.moveNext();
196 }).then((hasMore) {
197 expect(hasMore, isTrue);
198 expect(it.current, equals([7, 8]));
199 c2.add(9);
200 return it.moveNext();
201 }).then((hasMore) {
202 expect(hasMore, isFalse);
203 done();
204 });
205
206 c1..add(1)..add(3)..add(5)..add(7)..close();
207 c2..add(2)..add(4);
208 });
209
210 test("pause-resume2", () {
211 var s1 = new Stream.fromIterable([0, 2, 4, 6, 8]);
212 var s2 = new Stream.fromIterable([1, 3, 5, 7]);
213 var sz = new StreamZip([s1, s2]);
214 int ctr = 0;
215 var sub;
216 sub = sz.listen(expectAsync1((v) {
217 expect(v, equals([ctr * 2, ctr * 2 + 1]));
218 if (ctr == 1) {
219 sub.pause(new Future.delayed(const Duration(milliseconds: 25)));
220 } else if (ctr == 2) {
221 sub.pause();
222 new Future.delayed(const Duration(milliseconds: 25)).then((_) {
223 sub.resume();
224 });
225 }
226 ctr++;
227 }, count: 4));
228 });
229 }
OLDNEW
« no previous file with comments | « pkg/sequence_zip/test/iterable_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698