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