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