OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "dart:async"; | 5 import "dart:async"; |
6 import "package:unittest/unittest.dart"; | 6 import "package:unittest/unittest.dart"; |
7 | 7 |
8 main() { | 8 main() { |
9 test("stream iterator basic", () { | 9 test("stream iterator basic", () async { |
10 StreamController c = new StreamController(); | 10 var stream = createStream(); |
11 Stream s = c.stream; | 11 StreamIterator iterator = new StreamIterator(stream); |
12 StreamIterator i = new StreamIterator(s); | 12 expect(iterator.current, isNull); |
13 i.moveNext().then(expectAsync((bool b) { | 13 expect(await iterator.moveNext(), isTrue); |
14 expect(b, isTrue); | 14 expect(iterator.current, 42); |
15 expect(42, i.current); | 15 expect(await iterator.moveNext(), isTrue); |
16 return i.moveNext(); | 16 expect(iterator.current, 37); |
17 })).then(expectAsync((bool b) { | 17 expect(await iterator.moveNext(), isFalse); |
18 expect(b, isTrue); | 18 expect(iterator.current, isNull); |
19 expect(37, i.current); | 19 expect(await iterator.moveNext(), isFalse); |
20 return i.moveNext(); | |
21 })).then(expectAsync((bool b) { | |
22 expect(b, isFalse); | |
23 })); | |
24 c.add(42); | |
25 c.add(37); | |
26 c.close(); | |
27 }); | 20 }); |
28 | 21 |
29 test("stream iterator prefilled", () { | 22 test("stream iterator prefilled", () async { |
30 StreamController c = new StreamController(); | 23 Stream stream = createStream(); |
31 c.add(42); | 24 StreamIterator iterator = new StreamIterator(stream); |
32 c.add(37); | 25 await new Future.delayed(Duration.ZERO); |
33 c.close(); | 26 expect(iterator.current, isNull); |
34 Stream s = c.stream; | 27 expect(await iterator.moveNext(), isTrue); |
35 StreamIterator i = new StreamIterator(s); | 28 expect(iterator.current, 42); |
36 i.moveNext().then(expectAsync((bool b) { | 29 expect(await iterator.moveNext(), isTrue); |
37 expect(b, isTrue); | 30 expect(iterator.current, 37); |
38 expect(42, i.current); | 31 expect(await iterator.moveNext(), isFalse); |
39 return i.moveNext(); | 32 expect(iterator.current, isNull); |
40 })).then(expectAsync((bool b) { | 33 expect(await iterator.moveNext(), isFalse); |
41 expect(b, isTrue); | |
42 expect(37, i.current); | |
43 return i.moveNext(); | |
44 })).then(expectAsync((bool b) { | |
45 expect(b, isFalse); | |
46 })); | |
47 }); | 34 }); |
48 | 35 |
49 test("stream iterator error", () { | 36 test("stream iterator error", () async { |
50 StreamController c = new StreamController(); | 37 Stream stream = createErrorStream(); |
51 Stream s = c.stream; | 38 StreamIterator iterator = new StreamIterator(stream); |
52 StreamIterator i = new StreamIterator(s); | 39 expect(await iterator.moveNext(), isTrue); |
53 i.moveNext().then(expectAsync((bool b) { | 40 expect(iterator.current, 42); |
54 expect(b, isTrue); | 41 var hasNext = iterator.moveNext(); |
55 expect(42, i.current); | 42 expect(hasNext, throwsA("BAD")); // This is an async expectation, |
56 return i.moveNext(); | 43 await hasNext.catchError((_){}); // so we have to wait for the future too. |
57 })).then((bool b) { | 44 expect(iterator.current, isNull); |
58 fail("Result not expected"); | 45 expect(await iterator.moveNext(), isFalse); |
59 }, onError: expectAsync((e) { | 46 expect(iterator.current, isNull); |
60 expect("BAD", e); | |
61 return i.moveNext(); | |
62 })).then(expectAsync((bool b) { | |
63 expect(b, isFalse); | |
64 })); | |
65 c.add(42); | |
66 c.addError("BAD"); | |
67 c.add(37); | |
68 c.close(); | |
69 }); | 47 }); |
70 | 48 |
71 test("stream iterator current/moveNext during move", () { | 49 test("stream iterator current/moveNext during move", () async { |
72 StreamController c = new StreamController(); | 50 Stream stream = createStream(); |
73 Stream s = c.stream; | 51 StreamIterator iterator = new StreamIterator(stream); |
74 StreamIterator i = new StreamIterator(s); | 52 var hasNext = iterator.moveNext(); |
75 i.moveNext().then(expectAsync((bool b) { | 53 expect(iterator.moveNext, throwsA(isStateError)); |
76 expect(b, isTrue); | 54 expect(await hasNext, isTrue); |
77 expect(42, i.current); | 55 expect(iterator.current, 42); |
78 new Timer(const Duration(milliseconds:100), expectAsync(() { | 56 iterator.cancel(); |
79 expect(i.current, null); | |
80 expect(() { i.moveNext(); }, throws); | |
81 c.add(37); | |
82 c.close(); | |
83 })); | |
84 return i.moveNext(); | |
85 })).then(expectAsync((bool b) { | |
86 expect(b, isTrue); | |
87 expect(37, i.current); | |
88 return i.moveNext(); | |
89 })).then(expectAsync((bool b) { | |
90 expect(b, isFalse); | |
91 })); | |
92 c.add(42); | |
93 }); | 57 }); |
| 58 |
| 59 test("stream iterator error during cancel", () async { |
| 60 Stream stream = createCancelErrorStream(); |
| 61 StreamIterator iterator = new StreamIterator(stream); |
| 62 for (int i = 0; i < 10; i++) { |
| 63 expect(await iterator.moveNext(), isTrue); |
| 64 expect(iterator.current, i); |
| 65 } |
| 66 var hasNext = iterator.moveNext(); // active moveNext will be completed. |
| 67 var cancel = iterator.cancel(); |
| 68 expect(cancel, throwsA("BAD")); |
| 69 expect(await hasNext, isFalse); |
| 70 expect(await iterator.moveNext(), isFalse); |
| 71 }); |
| 72 |
94 } | 73 } |
| 74 |
| 75 Stream createStream() async* { |
| 76 yield 42; |
| 77 yield 37; |
| 78 } |
| 79 |
| 80 Stream createErrorStream() async* { |
| 81 yield 42; |
| 82 // Emit an error without stopping the generator. |
| 83 yield* (new Future.error("BAD").asStream()); |
| 84 yield 37; |
| 85 } |
| 86 |
| 87 /// Create a stream that throws when cancelled. |
| 88 Stream createCancelErrorStream() async* { |
| 89 int i = 0; |
| 90 try { |
| 91 while (true) yield i++; |
| 92 } finally { |
| 93 throw "BAD"; |
| 94 } |
| 95 } |
OLD | NEW |