OLD | NEW |
(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 // Test that transformations like `map` and `where` preserve broadcast flag. |
| 6 library stream_join_test; |
| 7 |
| 8 import 'dart:async'; |
| 9 import 'event_helper.dart'; |
| 10 import 'package:unittest/unittest.dart'; |
| 11 import "package:expect/expect.dart"; |
| 12 |
| 13 main() { |
| 14 testStream("singlesub", () => new StreamController(), (c) => c.stream); |
| 15 testStream("broadcast", () => new StreamController.broadcast(), |
| 16 (c) => c.stream); |
| 17 testStream("asBroadcast", () => new StreamController(), |
| 18 (c) => c.stream.asBroadcastStream()); |
| 19 testStream("broadcast.asBroadcast", () => new StreamController.broadcast(), |
| 20 (c) => c.stream.asBroadcastStream()); |
| 21 } |
| 22 |
| 23 void testStream(String name, |
| 24 StreamController create(), |
| 25 Stream getStream(controller)) { |
| 26 test("$name-map", () { |
| 27 var c = create(); |
| 28 var s = getStream(c); |
| 29 Stream newStream = s.map((x) => x + 1); |
| 30 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 31 newStream.single.then(expectAsync((v) { |
| 32 Expect.equals(43, v); |
| 33 })); |
| 34 c.add(42); |
| 35 c.close(); |
| 36 }); |
| 37 test("$name-where", () { |
| 38 var c = create(); |
| 39 var s = getStream(c); |
| 40 Stream newStream = s.where((x) => x.isEven); |
| 41 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 42 newStream.single.then(expectAsync((v) { |
| 43 Expect.equals(42, v); |
| 44 })); |
| 45 c.add(37); |
| 46 c.add(42); |
| 47 c.add(87); |
| 48 c.close(); |
| 49 }); |
| 50 test("$name-handleError", () { |
| 51 var c = create(); |
| 52 var s = getStream(c); |
| 53 Stream newStream = s.handleError((x, s) {}); |
| 54 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 55 newStream.single.then(expectAsync((v) { |
| 56 Expect.equals(42, v); |
| 57 })); |
| 58 c.addError("BAD1"); |
| 59 c.add(42); |
| 60 c.addError("BAD2"); |
| 61 c.close(); |
| 62 }); |
| 63 test("$name-expand", () { |
| 64 var c = create(); |
| 65 var s = getStream(c); |
| 66 Stream newStream = s.expand((x) => x.isEven ? [x] : []); |
| 67 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 68 newStream.single.then(expectAsync((v) { |
| 69 Expect.equals(42, v); |
| 70 })); |
| 71 c.add(37); |
| 72 c.add(42); |
| 73 c.add(87); |
| 74 c.close(); |
| 75 }); |
| 76 test("$name-transform", () { |
| 77 var c = create(); |
| 78 var s = getStream(c); |
| 79 // TODO: find name of default transformer |
| 80 var t = new StreamTransformer.fromHandlers( |
| 81 handleData: (value, EventSink sink) { sink.add(value); } |
| 82 ); |
| 83 Stream newStream = s.transform(t); |
| 84 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 85 newStream.single.then(expectAsync((v) { |
| 86 Expect.equals(42, v); |
| 87 })); |
| 88 c.add(42); |
| 89 c.close(); |
| 90 }); |
| 91 test("$name-take", () { |
| 92 var c = create(); |
| 93 var s = getStream(c); |
| 94 Stream newStream = s.take(1); |
| 95 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 96 newStream.single.then(expectAsync((v) { |
| 97 Expect.equals(42, v); |
| 98 })); |
| 99 c.add(42); |
| 100 c.add(37); |
| 101 c.close(); |
| 102 }); |
| 103 test("$name-takeWhile", () { |
| 104 var c = create(); |
| 105 var s = getStream(c); |
| 106 Stream newStream = s.takeWhile((x) => x.isEven); |
| 107 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 108 newStream.single.then(expectAsync((v) { |
| 109 Expect.equals(42, v); |
| 110 })); |
| 111 c.add(42); |
| 112 c.add(37); |
| 113 c.close(); |
| 114 }); |
| 115 test("$name-skip", () { |
| 116 var c = create(); |
| 117 var s = getStream(c); |
| 118 Stream newStream = s.skip(1); |
| 119 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 120 newStream.single.then(expectAsync((v) { |
| 121 Expect.equals(42, v); |
| 122 })); |
| 123 c.add(37); |
| 124 c.add(42); |
| 125 c.close(); |
| 126 }); |
| 127 test("$name-skipWhile", () { |
| 128 var c = create(); |
| 129 var s = getStream(c); |
| 130 Stream newStream = s.skipWhile((x) => x.isOdd); |
| 131 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 132 newStream.single.then(expectAsync((v) { |
| 133 Expect.equals(42, v); |
| 134 })); |
| 135 c.add(37); |
| 136 c.add(42); |
| 137 c.close(); |
| 138 }); |
| 139 test("$name-distinct", () { |
| 140 var c = create(); |
| 141 var s = getStream(c); |
| 142 Stream newStream = s.distinct(); |
| 143 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 144 newStream.single.then(expectAsync((v) { |
| 145 Expect.equals(42, v); |
| 146 })); |
| 147 c.add(42); |
| 148 c.add(42); |
| 149 c.close(); |
| 150 }); |
| 151 test("$name-timeout", () { |
| 152 var c = create(); |
| 153 var s = getStream(c); |
| 154 Stream newStream = s.timeout(const Duration(seconds: 1)); |
| 155 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 156 newStream.single.then(expectAsync((v) { |
| 157 Expect.equals(42, v); |
| 158 })); |
| 159 c.add(42); |
| 160 c.close(); |
| 161 }); |
| 162 test("$name-asyncMap", () { |
| 163 var c = create(); |
| 164 var s = getStream(c); |
| 165 Stream newStream = s.asyncMap((x) => new Future.value(x + 1)); |
| 166 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 167 newStream.single.then(expectAsync((v) { |
| 168 Expect.equals(43, v); |
| 169 })); |
| 170 c.add(42); |
| 171 c.close(); |
| 172 }); |
| 173 test("$name-asyncExpand", () { |
| 174 var c = create(); |
| 175 var s = getStream(c); |
| 176 Stream newStream = s.asyncExpand((x) => new Stream.fromIterable([x + 1])); |
| 177 Expect.equals(s.isBroadcast, newStream.isBroadcast); |
| 178 newStream.single.then(expectAsync((v) { |
| 179 Expect.equals(43, v); |
| 180 })); |
| 181 c.add(42); |
| 182 c.close(); |
| 183 }); |
| 184 |
| 185 // The following tests are only on broadcast streams, they require listening |
| 186 // more than once. |
| 187 if (name.startsWith("singlesub")) return; |
| 188 |
| 189 test("$name-skip-multilisten", () { |
| 190 if (name.startsWith("singlesub") || |
| 191 name.startsWith("asBroadcast")) return; |
| 192 var c = create(); |
| 193 var s = getStream(c); |
| 194 Stream newStream = s.skip(5); |
| 195 // Listen immediately, to ensure that an asBroadcast stream is started. |
| 196 var sub = newStream.listen((_){}); |
| 197 int i = 0; |
| 198 var expect1 = 11; |
| 199 var expect2 = 21; |
| 200 var handler2 = expectAsync((v) { |
| 201 expect(v, expect2); |
| 202 expect2++; |
| 203 }, count: 5); |
| 204 var handler1 = expectAsync((v) { |
| 205 expect(v, expect1); |
| 206 expect1++; |
| 207 }, count: 15); |
| 208 var loop; |
| 209 loop = expectAsync(() { |
| 210 i++; |
| 211 c.add(i); |
| 212 if (i == 5) { |
| 213 scheduleMicrotask(() { |
| 214 newStream.listen(handler1); |
| 215 }); |
| 216 } |
| 217 if (i == 15) { |
| 218 scheduleMicrotask(() { |
| 219 newStream.listen(handler2); |
| 220 }); |
| 221 } |
| 222 if (i < 25) { |
| 223 scheduleMicrotask(loop); |
| 224 } else { |
| 225 sub.cancel(); |
| 226 c.close(); |
| 227 } |
| 228 }, count: 25); |
| 229 scheduleMicrotask(loop); |
| 230 }); |
| 231 |
| 232 test("$name-take-multilisten", () { |
| 233 var c = create(); |
| 234 var s = getStream(c); |
| 235 Stream newStream = s.take(10); |
| 236 // Listen immediately, to ensure that an asBroadcast stream is started. |
| 237 var sub = newStream.listen((_){}); |
| 238 int i = 0; |
| 239 var expect1 = 6; |
| 240 var expect2 = 11; |
| 241 var handler2 = expectAsync((v) { |
| 242 expect(v, expect2); |
| 243 expect(v <= 20, isTrue); |
| 244 expect2++; |
| 245 }, count: 10); |
| 246 var handler1 = expectAsync((v) { |
| 247 expect(v, expect1); |
| 248 expect(v <= 15, isTrue); |
| 249 expect1++; |
| 250 }, count: 10); |
| 251 var loop; |
| 252 loop = expectAsync(() { |
| 253 i++; |
| 254 c.add(i); |
| 255 if (i == 5) { |
| 256 scheduleMicrotask(() { |
| 257 newStream.listen(handler1); |
| 258 }); |
| 259 } |
| 260 if (i == 10) { |
| 261 scheduleMicrotask(() { |
| 262 newStream.listen(handler2); |
| 263 }); |
| 264 } |
| 265 if (i < 25) { |
| 266 scheduleMicrotask(loop); |
| 267 } else { |
| 268 sub.cancel(); |
| 269 c.close(); |
| 270 } |
| 271 }, count: 25); |
| 272 scheduleMicrotask(loop); |
| 273 }); |
| 274 |
| 275 test("$name-skipWhile-multilisten", () { |
| 276 if (name.startsWith("singlesub") || |
| 277 name.startsWith("asBroadcast")) return; |
| 278 var c = create(); |
| 279 var s = getStream(c); |
| 280 Stream newStream = s.skipWhile((x) => (x % 10) != 1); |
| 281 // Listen immediately, to ensure that an asBroadcast stream is started. |
| 282 var sub = newStream.listen((_){}); |
| 283 int i = 0; |
| 284 var expect1 = 11; |
| 285 var expect2 = 21; |
| 286 var handler2 = expectAsync((v) { |
| 287 expect(v, expect2); |
| 288 expect2++; |
| 289 }, count: 5); |
| 290 var handler1 = expectAsync((v) { |
| 291 expect(v, expect1); |
| 292 expect1++; |
| 293 }, count: 15); |
| 294 var loop; |
| 295 loop = expectAsync(() { |
| 296 i++; |
| 297 c.add(i); |
| 298 if (i == 5) { |
| 299 scheduleMicrotask(() { |
| 300 newStream.listen(handler1); |
| 301 }); |
| 302 } |
| 303 if (i == 15) { |
| 304 scheduleMicrotask(() { |
| 305 newStream.listen(handler2); |
| 306 }); |
| 307 } |
| 308 if (i < 25) { |
| 309 scheduleMicrotask(loop); |
| 310 } else { |
| 311 sub.cancel(); |
| 312 c.close(); |
| 313 } |
| 314 }, count: 25); |
| 315 scheduleMicrotask(loop); |
| 316 }); |
| 317 |
| 318 test("$name-takeWhile-multilisten", () { |
| 319 var c = create(); |
| 320 var s = getStream(c); |
| 321 Stream newStream = s.takeWhile((x) => (x % 10) != 5); |
| 322 // Listen immediately, to ensure that an asBroadcast stream is started. |
| 323 var sub = newStream.listen((_){}); |
| 324 int i = 0; |
| 325 // Non-overlapping ranges means the test must not remember its first |
| 326 // failure. |
| 327 var expect1 = 6; |
| 328 var expect2 = 16; |
| 329 var handler2 = expectAsync((v) { |
| 330 expect(v, expect2); |
| 331 expect(v <= 25, isTrue); |
| 332 expect2++; |
| 333 }, count: 9); |
| 334 var handler1 = expectAsync((v) { |
| 335 expect(v, expect1); |
| 336 expect(v <= 15, isTrue); |
| 337 expect1++; |
| 338 }, count: 9); |
| 339 var loop; |
| 340 loop = expectAsync(() { |
| 341 i++; |
| 342 c.add(i); |
| 343 if (i == 5) { |
| 344 scheduleMicrotask(() { |
| 345 newStream.listen(handler1); |
| 346 }); |
| 347 } |
| 348 if (i == 15) { |
| 349 scheduleMicrotask(() { |
| 350 newStream.listen(handler2); |
| 351 }); |
| 352 } |
| 353 if (i < 25) { |
| 354 scheduleMicrotask(loop); |
| 355 } else { |
| 356 sub.cancel(); |
| 357 c.close(); |
| 358 } |
| 359 }, count: 25); |
| 360 scheduleMicrotask(loop); |
| 361 }); |
| 362 } |
OLD | NEW |