Chromium Code Reviews| 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 library barback.test.stream_pool_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:barback/src/stream_pool.dart'; | |
| 10 import 'package:barback/src/utils.dart'; | |
| 11 import 'package:scheduled_test/scheduled_test.dart'; | |
| 12 | |
| 13 import 'utils.dart'; | |
| 14 | |
| 15 main() { | |
| 16 initConfig(); | |
| 17 | |
| 18 group("buffered", () { | |
| 19 test("buffers events from multiple inputs", () { | |
| 20 var pool = new StreamPool<String>(); | |
| 21 | |
| 22 var controller1 = new StreamController<String>(); | |
| 23 pool.addStream(controller1.stream); | |
| 24 controller1.add("first"); | |
| 25 | |
| 26 var controller2 = new StreamController<String>(); | |
| 27 pool.addStream(controller2.stream); | |
| 28 controller2.add("second"); | |
| 29 | |
| 30 // Call [toList] asynchronously to be sure that the events have been | |
| 31 // buffered beforehand and aren't just being received unbuffered. | |
| 32 expect(newFuture(() => pool.stream.toList()), | |
| 33 completion(equals(["first", "second"]))); | |
| 34 | |
| 35 pumpEventQueue().then((_) => pool.close()); | |
| 36 }); | |
| 37 | |
| 38 test("buffers errors from multiple inputs", () { | |
| 39 var pool = new StreamPool<String>(); | |
| 40 | |
| 41 var controller1 = new StreamController<String>(); | |
| 42 pool.addStream(controller1.stream); | |
| 43 controller1.add("first"); | |
| 44 | |
| 45 var controller2 = new StreamController<String>(); | |
| 46 pool.addStream(controller2.stream); | |
| 47 controller2.add("second"); | |
| 48 controller1.addError("third"); | |
| 49 controller2.addError("fourth"); | |
| 50 controller1.add("fifth"); | |
| 51 | |
| 52 expect(newFuture(() { | |
| 53 return pool.stream.transform(new StreamTransformer( | |
| 54 handleData: (data, sink) => sink.add(["data", data]), | |
| 55 handleError: (error, sink) => sink.add(["error", error]))).toList(); | |
| 56 }), completion(equals([ | |
| 57 ["data", "first"], | |
| 58 ["data", "second"], | |
| 59 ["error", "third"], | |
| 60 ["error", "fourth"], | |
| 61 ["data", "fifth"] | |
| 62 ]))); | |
| 63 | |
| 64 pumpEventQueue().then((_) => pool.close()); | |
| 65 }); | |
| 66 | |
| 67 test("buffers inputs from a broadcast stream", () { | |
| 68 var pool = new StreamPool<String>(); | |
| 69 var controller = new StreamController<String>.broadcast(); | |
| 70 pool.addStream(controller.stream); | |
| 71 controller.add("first"); | |
| 72 controller.add("second"); | |
| 73 | |
| 74 expect(newFuture(() => pool.stream.toList()), | |
|
Bob Nystrom
2013/08/01 18:10:33
Copy the comment from the similar test above here.
nweiz
2013/08/01 20:30:52
Done.
| |
| 75 completion(equals(["first", "second"]))); | |
| 76 | |
| 77 pumpEventQueue().then((_) => pool.close()); | |
| 78 }); | |
| 79 }); | |
| 80 | |
| 81 group("broadcast", () { | |
| 82 test("doesn't buffer inputs", () { | |
| 83 var pool = new StreamPool<String>.broadcast(); | |
| 84 | |
| 85 var controller1 = new StreamController<String>.broadcast(); | |
| 86 pool.addStream(controller1.stream); | |
| 87 controller1.add("first"); | |
| 88 | |
| 89 var controller2 = new StreamController<String>.broadcast(); | |
| 90 pool.addStream(controller2.stream); | |
| 91 controller2.add("second"); | |
| 92 | |
| 93 // Call [toList] asynchronously to be sure that the events have been | |
| 94 // buffered beforehand and aren't just being received unbuffered. | |
| 95 expect(newFuture(() => pool.stream.toList()), completion(isEmpty)); | |
| 96 | |
| 97 pumpEventQueue().then((_) => pool.close()); | |
| 98 }); | |
| 99 | |
| 100 test("doesn't buffer errors", () { | |
| 101 var pool = new StreamPool<String>.broadcast(); | |
| 102 | |
| 103 var controller1 = new StreamController<String>.broadcast(); | |
| 104 pool.addStream(controller1.stream); | |
| 105 controller1.addError("first"); | |
| 106 | |
| 107 var controller2 = new StreamController<String>.broadcast(); | |
| 108 pool.addStream(controller2.stream); | |
| 109 controller2.addError("second"); | |
| 110 | |
| 111 expect(newFuture(() { | |
| 112 return pool.stream.transform(new StreamTransformer( | |
| 113 handleData: (data, sink) => sink.add(["data", data]), | |
| 114 handleError: (error, sink) => sink.add(["error", error]))).toList(); | |
|
Bob Nystrom
2013/08/01 18:10:33
Since you're just checking for emptiness, there's
nweiz
2013/08/01 20:30:52
Done.
| |
| 115 }), completion(isEmpty)); | |
| 116 | |
| 117 pumpEventQueue().then((_) => pool.close()); | |
| 118 }); | |
| 119 | |
| 120 test("doesn't buffer inputs from a buffered stream", () { | |
| 121 var pool = new StreamPool<String>.broadcast(); | |
| 122 var controller = new StreamController<String>(); | |
| 123 pool.addStream(controller.stream); | |
| 124 controller.add("first"); | |
| 125 controller.add("second"); | |
| 126 | |
| 127 expect(pumpEventQueue().then((_) => pool.stream.toList()), | |
| 128 completion(isEmpty)); | |
| 129 | |
| 130 pumpEventQueue().then((_) => pool.close()); | |
| 131 }); | |
| 132 }); | |
| 133 | |
| 134 for (var type in ["buffered", "broadcast"]) { | |
| 135 group(type, () { | |
| 136 var pool; | |
| 137 var bufferedController; | |
| 138 var bufferedStream; | |
| 139 var bufferedSyncController; | |
| 140 var broadcastController; | |
| 141 var broadcastStream; | |
| 142 var broadcastSyncController; | |
| 143 | |
| 144 setUp(() { | |
| 145 if (type == "buffered") { | |
| 146 pool = new StreamPool<String>(); | |
| 147 } else { | |
| 148 pool = new StreamPool<String>.broadcast(); | |
| 149 } | |
| 150 | |
| 151 bufferedController = new StreamController<String>(); | |
| 152 pool.addStream(bufferedController.stream); | |
| 153 | |
| 154 bufferedSyncController = new StreamController<String>(sync: true); | |
| 155 pool.addStream(bufferedSyncController.stream); | |
| 156 | |
| 157 broadcastController = new StreamController<String>.broadcast(); | |
| 158 pool.addStream(broadcastController.stream); | |
| 159 | |
| 160 broadcastSyncController = | |
| 161 new StreamController<String>.broadcast(sync: true); | |
| 162 pool.addStream(broadcastSyncController.stream); | |
| 163 }); | |
| 164 | |
| 165 test("emits events to a listener", () { | |
| 166 expect(pool.stream.toList(), completion(equals(["first", "second"]))); | |
| 167 | |
| 168 bufferedController.add("first"); | |
| 169 broadcastController.add("second"); | |
| 170 pumpEventQueue().then((_) => pool.close()); | |
| 171 }); | |
| 172 | |
| 173 test("emits sync events synchronously", () { | |
| 174 var events = []; | |
| 175 pool.stream.listen(events.add); | |
| 176 | |
| 177 bufferedSyncController.add("first"); | |
| 178 expect(events, equals(["first"])); | |
| 179 | |
| 180 broadcastSyncController.add("second"); | |
| 181 expect(events, equals(["first", "second"])); | |
| 182 }); | |
| 183 | |
| 184 test("emits async events asynchronously", () { | |
| 185 var events = []; | |
| 186 pool.stream.listen(events.add); | |
| 187 | |
| 188 bufferedController.add("first"); | |
| 189 broadcastController.add("second"); | |
| 190 expect(events, isEmpty); | |
| 191 | |
| 192 expect(pumpEventQueue().then((_) => events), | |
| 193 completion(equals(["first", "second"]))); | |
| 194 }); | |
| 195 | |
| 196 test("doesn't emit events from removed streams", () { | |
| 197 expect(pool.stream.toList(), completion(equals(["first", "third"]))); | |
| 198 | |
| 199 bufferedController.add("first"); | |
| 200 expect(pumpEventQueue().then((_) { | |
| 201 pool.removeStream(bufferedController.stream); | |
| 202 bufferedController.add("second"); | |
| 203 }).then((_) { | |
| 204 broadcastController.add("third"); | |
| 205 return pumpEventQueue(); | |
| 206 }).then((_) { | |
| 207 pool.removeStream(broadcastController.stream); | |
| 208 broadcastController.add("fourth"); | |
| 209 pool.close(); | |
| 210 }), completes); | |
| 211 }); | |
| 212 }); | |
| 213 } | |
| 214 } | |
| OLD | NEW |