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 import "dart:async"; |
| 6 import "package:expect/expect.dart"; |
| 7 import 'package:async_helper/async_helper.dart'; |
| 8 |
| 9 main() { |
| 10 asyncStart(); |
| 11 |
| 12 testValues(); |
| 13 testErrors(); |
| 14 testMixed(); |
| 15 testOrdering(); |
| 16 testEmpty(); |
| 17 testPrecompleted(); |
| 18 |
| 19 asyncEnd(); |
| 20 } |
| 21 |
| 22 void testValues() { |
| 23 asyncStart(); |
| 24 var cs = new List.generate(3, (_) => new Completer()); |
| 25 var stream = new Stream.fromFutures(cs.map((x) => x.future)); |
| 26 var result = stream.toList(); |
| 27 |
| 28 result.then((list) { |
| 29 Expect.listEquals([1, 2, 3], list); |
| 30 asyncEnd(); |
| 31 }); |
| 32 |
| 33 cs[1].complete(1); |
| 34 cs[2].complete(2); |
| 35 cs[0].complete(3); |
| 36 } |
| 37 |
| 38 void testErrors() { |
| 39 asyncStart(); |
| 40 var cs = new List.generate(3, (_) => new Completer()); |
| 41 var stream = new Stream.fromFutures(cs.map((x) => x.future)); |
| 42 |
| 43 int counter = 0; |
| 44 stream.listen((_) { |
| 45 Expect.fail("unexpected value"); |
| 46 }, onError: (e) { |
| 47 Expect.equals(++counter, e); |
| 48 }, onDone: () { |
| 49 Expect.equals(3, counter); |
| 50 asyncEnd(); |
| 51 }); |
| 52 |
| 53 cs[1].completeError(1); |
| 54 cs[2].completeError(2); |
| 55 cs[0].completeError(3); |
| 56 } |
| 57 |
| 58 void testMixed() { |
| 59 asyncStart(); |
| 60 var cs = new List.generate(3, (_) => new Completer()); |
| 61 var stream = new Stream.fromFutures(cs.map((x) => x.future)); |
| 62 |
| 63 int counter = 0; |
| 64 stream.listen((v) { |
| 65 Expect.isTrue(counter == 0 || counter == 2); |
| 66 Expect.equals(++counter, v); |
| 67 }, onError: (e) { |
| 68 Expect.equals(++counter, 2); |
| 69 Expect.equals(2, e); |
| 70 }, onDone: () { |
| 71 Expect.equals(3, counter); |
| 72 asyncEnd(); |
| 73 }); |
| 74 |
| 75 cs[1].complete(1); |
| 76 cs[2].completeError(2); |
| 77 cs[0].complete(3); |
| 78 } |
| 79 |
| 80 void testOrdering() { |
| 81 // The output is in completion order, not affected by the input future order. |
| 82 test(n1, n2, n3) { |
| 83 asyncStart(); |
| 84 var cs = new List.generate(3, (_) => new Completer()); |
| 85 var stream = new Stream.fromFutures(cs.map((x) => x.future)); |
| 86 var result = stream.toList(); |
| 87 |
| 88 result.then((list) { |
| 89 Expect.listEquals([1, 2, 3], list); |
| 90 asyncEnd(); |
| 91 }); |
| 92 |
| 93 cs[n1].complete(1); |
| 94 cs[n2].complete(2); |
| 95 cs[n3].complete(3); |
| 96 } |
| 97 test(0, 1, 2); |
| 98 test(0, 2, 1); |
| 99 test(1, 0, 2); |
| 100 test(1, 2, 0); |
| 101 test(2, 0, 1); |
| 102 test(2, 1, 0); |
| 103 } |
| 104 |
| 105 void testEmpty() { |
| 106 asyncStart(); |
| 107 var stream = new Stream.fromFutures([]); |
| 108 |
| 109 stream.listen((_) { |
| 110 Expect.fail("unexpected value"); |
| 111 }, onError: (e) { |
| 112 Expect.fail("unexpected error"); |
| 113 }, onDone: () { |
| 114 asyncEnd(); |
| 115 }); |
| 116 } |
| 117 |
| 118 void testPrecompleted() { |
| 119 asyncStart(); |
| 120 var stream = new Stream.fromFutures( |
| 121 new Iterable.generate(3, (v) => new Future.value(v + 1))); |
| 122 var expected = new Set.from([1, 2, 3]); |
| 123 stream.listen((v) { |
| 124 Expect.isTrue(expected.contains(v)); |
| 125 expected.remove(v); |
| 126 }, onDone: () { |
| 127 Expect.isTrue(expected.isEmpty); |
| 128 asyncEnd(); |
| 129 }); |
| 130 } |
OLD | NEW |