OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 Google Inc. All Rights Reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 library quiver.streams.collect_test; |
| 16 |
| 17 import 'dart:async'; |
| 18 import 'dart:math'; |
| 19 |
| 20 import 'package:test/test.dart'; |
| 21 import 'package:quiver/streams.dart'; |
| 22 |
| 23 main() { |
| 24 group('collect', () { |
| 25 test('should produce no events for no futures', |
| 26 () => collect([]).toList().then((events) => expect(events, isEmpty))); |
| 27 |
| 28 test('should produce events for future completions in input order', () { |
| 29 var futures = new Iterable.generate( |
| 30 5, (int i) => i.isEven ? new Future.value(i) : new Future.error(i)); |
| 31 var events = []; |
| 32 var done = new Completer(); |
| 33 |
| 34 collect(futures).listen(events.add, onError: (i) { |
| 35 events.add('e$i'); |
| 36 }, onDone: done.complete); |
| 37 return Future.wait(futures).catchError((_) => done.future).then((_) { |
| 38 expect(events, [0, 'e1', 2, 'e3', 4]); |
| 39 }); |
| 40 }); |
| 41 |
| 42 test('should only advance iterator once ' |
| 43 'event for previous future is sent', () { |
| 44 var eventCount = 0; |
| 45 var maxParallel = 0; |
| 46 var currentParallel = 0; |
| 47 var done = new Completer(); |
| 48 var futures = new Iterable.generate(3, (_) { |
| 49 maxParallel = max(++currentParallel, maxParallel); |
| 50 return new Future.value(); |
| 51 }); |
| 52 |
| 53 var collected = collect(futures); |
| 54 |
| 55 decrementParallel(_) { |
| 56 eventCount++; |
| 57 currentParallel--; |
| 58 } |
| 59 |
| 60 collected.listen(decrementParallel, |
| 61 onError: decrementParallel, onDone: done.complete); |
| 62 return done.future.then((_) { |
| 63 expect(maxParallel, 1); |
| 64 expect(eventCount, 3); |
| 65 }); |
| 66 }); |
| 67 }); |
| 68 } |
OLD | NEW |