| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 stream transformers. |
| 6 import "dart:async"; |
| 7 |
| 8 import "package:async/async.dart"; |
| 9 import "package:test/test.dart"; |
| 10 import "package:clock/clock.dart"; |
| 11 |
| 12 const ms = const Duration(milliseconds: 1); |
| 13 |
| 14 void main() { |
| 15 var currentTime; |
| 16 var createStopwatch; |
| 17 var error = new Result.error("BAD", StackTrace.current); |
| 18 |
| 19 setUp(() { |
| 20 currentTime = Duration.ZERO; |
| 21 createStopwatch = new Clock(elapsed: () => currentTime).getStopwatch; |
| 22 }); |
| 23 |
| 24 /// Stream with events at the specified times. |
| 25 Stream createTimerStream(List<int> eventTimes, |
| 26 {List errors: const[]}) async* { |
| 27 for (var time in eventTimes) { |
| 28 currentTime = ms * time; |
| 29 if (errors.contains(time)) { |
| 30 yield* error.asFuture.asStream(); |
| 31 } else { |
| 32 yield time; |
| 33 } |
| 34 // Wait for the event to propagate before changing timer again. |
| 35 await new Future.delayed(Duration.ZERO); |
| 36 } |
| 37 } |
| 38 |
| 39 test("expected behavior", () async { |
| 40 var stream = |
| 41 createTimerStream([0, 30, 60, 90, 180, 280, 380, 479, 480, 580]); |
| 42 var throttled = stream.transform( |
| 43 new Throttle<int>(ms * 100, createStopwatch: createStopwatch)); |
| 44 expect(throttled.toList(), completion([0, 180, 280, 380, 480, 580])); |
| 45 }); |
| 46 |
| 47 test("empty source", () async { |
| 48 var stream = new Stream<int>.empty(); |
| 49 var throttled = stream.transform( |
| 50 new Throttle<int>(ms * 100, createStopwatch: createStopwatch)); |
| 51 expect(throttled.toList(), completion([])); |
| 52 }); |
| 53 |
| 54 test("omit last event", () async { |
| 55 var stream = |
| 56 createTimerStream([0, 30, 60, 90, 180, 270]); |
| 57 var throttled = stream.transform( |
| 58 new Throttle<int>(ms * 100, createStopwatch: createStopwatch)); |
| 59 expect(throttled.toList(), completion([0, 180])); |
| 60 }); |
| 61 |
| 62 test("stops at first error", () async { |
| 63 var stream = |
| 64 createTimerStream([0, 30, 60, 120, 180, 280], errors: [180]); |
| 65 var throttled = stream.transform( |
| 66 new Throttle<int>(ms * 100, createStopwatch: createStopwatch)); |
| 67 // Stops at error. |
| 68 Result r(v) => new Result.value(v); |
| 69 expect(Result.captureStream(throttled).toList(), |
| 70 completion([r(0), r(120), error])); |
| 71 }); |
| 72 } |
| OLD | NEW |