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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import "package:async_helper/async_helper.dart"; |
6 import "dart:async"; | 7 import "dart:async"; |
7 import "dart:io"; | 8 import "dart:io"; |
8 import "dart:isolate"; | |
9 | 9 |
10 class TestConsumer implements StreamConsumer { | 10 class TestConsumer implements StreamConsumer { |
11 final List expected; | 11 final List expected; |
12 final List received = []; | 12 final List received = []; |
13 | 13 |
14 var closePort; | |
15 | |
16 int addStreamCount = 0; | 14 int addStreamCount = 0; |
17 int expcetedAddStreamCount; | 15 int expcetedAddStreamCount; |
| 16 bool expectClose; |
18 | 17 |
19 TestConsumer(this.expected, | 18 TestConsumer(this.expected, |
20 {close: true, | 19 {this.expectClose: true, |
21 this.expcetedAddStreamCount: -1}) { | 20 this.expcetedAddStreamCount: -1}) { |
22 if (close) closePort = new ReceivePort(); | 21 if (expectClose) asyncStart(); |
23 } | 22 } |
24 | 23 |
25 Future addStream(Stream stream) { | 24 Future addStream(Stream stream) { |
26 addStreamCount++; | 25 addStreamCount++; |
27 return stream.fold( | 26 var sub = stream |
28 received, | 27 .listen((v) { |
29 (list, value) { | 28 received.addAll(v); |
30 list.addAll(value); | 29 }); |
31 return list; | 30 sub.pause(); |
32 }) | 31 scheduleMicrotask(sub.resume); |
33 .then((_) {}); | 32 return sub.asFuture(); |
| 33 } |
| 34 |
| 35 void matches(List list) { |
| 36 Expect.listEquals(list, received); |
34 } | 37 } |
35 | 38 |
36 Future close() { | 39 Future close() { |
37 return new Future.value() | 40 return new Future.value() |
38 .then((_) { | 41 .then((_) { |
39 if (closePort != null) closePort.close(); | 42 if (expectClose) asyncEnd(); |
40 Expect.listEquals(expected, received); | 43 Expect.listEquals(expected, received); |
41 if (expcetedAddStreamCount >= 0) { | 44 if (expcetedAddStreamCount >= 0) { |
42 Expect.equals(expcetedAddStreamCount, addStreamCount); | 45 Expect.equals(expcetedAddStreamCount, addStreamCount); |
43 } | 46 } |
44 }); | 47 }); |
45 } | 48 } |
46 } | 49 } |
47 | 50 |
48 void testClose() { | 51 void testClose() { |
49 var sink = new IOSink(new TestConsumer([], expcetedAddStreamCount: 0)); | 52 var sink = new IOSink(new TestConsumer([], expcetedAddStreamCount: 0)); |
50 sink.close(); | 53 sink.close(); |
51 } | 54 } |
52 | 55 |
53 void testAddClose() { | 56 void testAddClose() { |
54 var sink = new IOSink(new TestConsumer([0])); | 57 var sink = new IOSink(new TestConsumer([0])); |
55 sink.add([0]); | 58 sink.add([0]); |
56 sink.close(); | 59 sink.close(); |
57 | 60 |
58 sink = new IOSink(new TestConsumer([0, 1, 2])); | 61 sink = new IOSink(new TestConsumer([0, 1, 2])); |
59 sink.add([0]); | 62 sink.add([0]); |
60 sink.add([1]); | 63 sink.add([1]); |
61 sink.add([2]); | 64 sink.add([2]); |
62 sink.close(); | 65 sink.close(); |
63 } | 66 } |
64 | 67 |
| 68 |
| 69 void testAddFlush() { |
| 70 var consumer = new TestConsumer([0, 1, 2]); |
| 71 var sink = new IOSink(consumer); |
| 72 sink.add([0]); |
| 73 sink.flush().then((s) { |
| 74 consumer.matches([0]); |
| 75 s.add([1]); |
| 76 s.add([2]); |
| 77 s.flush().then((s) { |
| 78 consumer.matches([0, 1, 2]); |
| 79 s.close(); |
| 80 }); |
| 81 }); |
| 82 } |
| 83 |
| 84 |
65 void testAddStreamClose() { | 85 void testAddStreamClose() { |
66 { | 86 { |
67 var sink = new IOSink(new TestConsumer([0])); | 87 var sink = new IOSink(new TestConsumer([0])); |
68 var controller = new StreamController(sync: true); | 88 var controller = new StreamController(sync: true); |
69 sink.addStream(controller.stream) | 89 sink.addStream(controller.stream) |
70 .then((_) { | 90 .then((_) { |
71 sink.close(); | 91 sink.close(); |
72 }); | 92 }); |
73 controller.add([0]); | 93 controller.add([0]); |
74 controller.close(); | 94 controller.close(); |
(...skipping 22 matching lines...) Expand all Loading... |
97 sink.close(); | 117 sink.close(); |
98 }); | 118 }); |
99 controller.add([0]); | 119 controller.add([0]); |
100 controller.close(); | 120 controller.close(); |
101 } | 121 } |
102 } | 122 } |
103 | 123 |
104 void main() { | 124 void main() { |
105 testClose(); | 125 testClose(); |
106 testAddClose(); | 126 testAddClose(); |
| 127 testAddFlush(); |
107 testAddStreamClose(); | 128 testAddStreamClose(); |
108 testAddStreamAddClose(); | 129 testAddStreamAddClose(); |
109 } | 130 } |
OLD | NEW |