| 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.add(controller1.stream); |
| 24 controller1.add("first"); |
| 25 |
| 26 var controller2 = new StreamController<String>(); |
| 27 pool.add(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.add(controller1.stream); |
| 43 controller1.add("first"); |
| 44 |
| 45 var controller2 = new StreamController<String>(); |
| 46 pool.add(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.add(controller.stream); |
| 71 controller.add("first"); |
| 72 controller.add("second"); |
| 73 |
| 74 // Call [toList] asynchronously to be sure that the events have been |
| 75 // buffered beforehand and aren't just being received unbuffered. |
| 76 expect(newFuture(() => pool.stream.toList()), |
| 77 completion(equals(["first", "second"]))); |
| 78 |
| 79 pumpEventQueue().then((_) => pool.close()); |
| 80 }); |
| 81 }); |
| 82 |
| 83 group("broadcast", () { |
| 84 test("doesn't buffer inputs", () { |
| 85 var pool = new StreamPool<String>.broadcast(); |
| 86 |
| 87 var controller1 = new StreamController<String>.broadcast(); |
| 88 pool.add(controller1.stream); |
| 89 controller1.add("first"); |
| 90 |
| 91 var controller2 = new StreamController<String>.broadcast(); |
| 92 pool.add(controller2.stream); |
| 93 controller2.add("second"); |
| 94 |
| 95 // Call [toList] asynchronously to be sure that the events have been |
| 96 // buffered beforehand and aren't just being received unbuffered. |
| 97 expect(newFuture(() => pool.stream.toList()), completion(isEmpty)); |
| 98 |
| 99 pumpEventQueue().then((_) => pool.close()); |
| 100 }); |
| 101 |
| 102 test("doesn't buffer errors", () { |
| 103 var pool = new StreamPool<String>.broadcast(); |
| 104 |
| 105 var controller1 = new StreamController<String>.broadcast(); |
| 106 pool.add(controller1.stream); |
| 107 controller1.addError("first"); |
| 108 |
| 109 var controller2 = new StreamController<String>.broadcast(); |
| 110 pool.add(controller2.stream); |
| 111 controller2.addError("second"); |
| 112 |
| 113 expect(newFuture(() { |
| 114 return pool.stream.transform(new StreamTransformer( |
| 115 handleData: (data, sink) => sink.add(data), |
| 116 handleError: (error, sink) => sink.add(error))).toList(); |
| 117 }), completion(isEmpty)); |
| 118 |
| 119 pumpEventQueue().then((_) => pool.close()); |
| 120 }); |
| 121 |
| 122 test("doesn't buffer inputs from a buffered stream", () { |
| 123 var pool = new StreamPool<String>.broadcast(); |
| 124 var controller = new StreamController<String>(); |
| 125 pool.add(controller.stream); |
| 126 controller.add("first"); |
| 127 controller.add("second"); |
| 128 |
| 129 expect(pumpEventQueue().then((_) => pool.stream.toList()), |
| 130 completion(isEmpty)); |
| 131 |
| 132 pumpEventQueue().then((_) => pool.close()); |
| 133 }); |
| 134 }); |
| 135 |
| 136 for (var type in ["buffered", "broadcast"]) { |
| 137 group(type, () { |
| 138 var pool; |
| 139 var bufferedController; |
| 140 var bufferedStream; |
| 141 var bufferedSyncController; |
| 142 var broadcastController; |
| 143 var broadcastStream; |
| 144 var broadcastSyncController; |
| 145 |
| 146 setUp(() { |
| 147 if (type == "buffered") { |
| 148 pool = new StreamPool<String>(); |
| 149 } else { |
| 150 pool = new StreamPool<String>.broadcast(); |
| 151 } |
| 152 |
| 153 bufferedController = new StreamController<String>(); |
| 154 pool.add(bufferedController.stream); |
| 155 |
| 156 bufferedSyncController = new StreamController<String>(sync: true); |
| 157 pool.add(bufferedSyncController.stream); |
| 158 |
| 159 broadcastController = new StreamController<String>.broadcast(); |
| 160 pool.add(broadcastController.stream); |
| 161 |
| 162 broadcastSyncController = |
| 163 new StreamController<String>.broadcast(sync: true); |
| 164 pool.add(broadcastSyncController.stream); |
| 165 }); |
| 166 |
| 167 test("emits events to a listener", () { |
| 168 expect(pool.stream.toList(), completion(equals(["first", "second"]))); |
| 169 |
| 170 bufferedController.add("first"); |
| 171 broadcastController.add("second"); |
| 172 pumpEventQueue().then((_) => pool.close()); |
| 173 }); |
| 174 |
| 175 test("emits sync events synchronously", () { |
| 176 var events = []; |
| 177 pool.stream.listen(events.add); |
| 178 |
| 179 bufferedSyncController.add("first"); |
| 180 expect(events, equals(["first"])); |
| 181 |
| 182 broadcastSyncController.add("second"); |
| 183 expect(events, equals(["first", "second"])); |
| 184 }); |
| 185 |
| 186 test("emits async events asynchronously", () { |
| 187 var events = []; |
| 188 pool.stream.listen(events.add); |
| 189 |
| 190 bufferedController.add("first"); |
| 191 broadcastController.add("second"); |
| 192 expect(events, isEmpty); |
| 193 |
| 194 expect(pumpEventQueue().then((_) => events), |
| 195 completion(equals(["first", "second"]))); |
| 196 }); |
| 197 |
| 198 test("doesn't emit events from removed streams", () { |
| 199 expect(pool.stream.toList(), completion(equals(["first", "third"]))); |
| 200 |
| 201 bufferedController.add("first"); |
| 202 expect(pumpEventQueue().then((_) { |
| 203 pool.remove(bufferedController.stream); |
| 204 bufferedController.add("second"); |
| 205 }).then((_) { |
| 206 broadcastController.add("third"); |
| 207 return pumpEventQueue(); |
| 208 }).then((_) { |
| 209 pool.remove(broadcastController.stream); |
| 210 broadcastController.add("fourth"); |
| 211 pool.close(); |
| 212 }), completes); |
| 213 }); |
| 214 }); |
| 215 } |
| 216 } |
| OLD | NEW |