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 library error_group_test; | 5 library error_group_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 | 10 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 StreamController controller; | 203 StreamController controller; |
204 Stream stream; | 204 Stream stream; |
205 | 205 |
206 setUp(() { | 206 setUp(() { |
207 errorGroup = new ErrorGroup(); | 207 errorGroup = new ErrorGroup(); |
208 controller = new StreamController(); | 208 controller = new StreamController(); |
209 stream = errorGroup.registerStream(controller.stream.asBroadcastStream()); | 209 stream = errorGroup.registerStream(controller.stream.asBroadcastStream()); |
210 }); | 210 }); |
211 | 211 |
212 test('should pass through values from the stream', () { | 212 test('should pass through values from the stream', () { |
213 StreamIterator iter = new StreamIterator(stream); | 213 expect(stream.elementAt(0), completion(equals(1))); |
214 iter.moveNext().then((hasNext) { | 214 expect(stream.elementAt(1), completion(equals(2))); |
215 expect(hasNext, isTrue); | |
216 expect(iter.current, equals(1)); | |
217 iter.moveNext().then((hasNext) { | |
218 expect(hasNext, isTrue); | |
219 expect(iter.current, equals(2)); | |
220 expect(iter.moveNext(), completion(isFalse)); | |
221 }); | |
222 }); | |
223 expect(errorGroup.done, completes); | 215 expect(errorGroup.done, completes); |
224 | 216 |
225 controller..add(1)..add(2)..close(); | 217 controller..add(1)..add(2)..close(); |
226 }); | 218 }); |
227 | 219 |
228 test('should pass through an error from the stream if it has a ' | 220 test('should pass through an error from the stream if it has a ' |
229 'listener', () { | 221 'listener', () { |
230 expect(stream.first, throwsFormatException); | 222 expect(stream.first, throwsFormatException); |
231 // errorGroup shouldn't top-level the exception | 223 // errorGroup shouldn't top-level the exception |
232 controller.addError(new FormatException()); | 224 controller.addError(new FormatException()); |
233 }); | 225 }); |
234 | 226 |
235 test('should notify the error group of an exception from the stream even ' | 227 test('should notify the error group of an exception from the stream even ' |
236 'if it has a listener', () { | 228 'if it has a listener', () { |
237 expect(stream.first, throwsFormatException); | 229 expect(stream.first, throwsFormatException); |
238 expect(errorGroup.done, throwsFormatException); | 230 expect(errorGroup.done, throwsFormatException); |
239 controller.addError(new FormatException()); | 231 controller.addError(new FormatException()); |
240 }); | 232 }); |
241 | 233 |
242 test('should pass a signaled exception to the stream if it has a listener ' | 234 test('should pass a signaled exception to the stream if it has a listener ' |
243 'and should unsubscribe that stream', () { | 235 'and should unsubscribe that stream', () { |
| 236 expect(stream.first, throwsFormatException); |
244 // errorGroup shouldn't top-level the exception | 237 // errorGroup shouldn't top-level the exception |
245 expect(stream.first, throwsFormatException); | |
246 errorGroup.signalError(new FormatException()); | 238 errorGroup.signalError(new FormatException()); |
247 | 239 |
248 expect(new Future(() { | 240 expect(stream.first.catchError((_) { |
249 controller.add('value'); | 241 controller.add('value'); |
250 }), completes); | 242 return stream.isEmpty; |
| 243 }), completion(isTrue)); |
251 }); | 244 }); |
252 | 245 |
253 test('should notify the error group of a signaled exception even if the ' | 246 test('should notify the error group of a signaled exception even if the ' |
254 'stream has a listener', () { | 247 'stream has a listener', () { |
255 expect(stream.first, throwsFormatException); | 248 expect(stream.first, throwsFormatException); |
256 expect(errorGroup.done, throwsFormatException); | 249 expect(errorGroup.done, throwsFormatException); |
257 errorGroup.signalError(new FormatException()); | 250 errorGroup.signalError(new FormatException()); |
258 }); | 251 }); |
259 | 252 |
260 test("should see one value and complete .done when the stream is done even " | 253 test("should see one value and complete .done when the stream is done even " |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 expect(stream1.toList(), completion(equals(['value1.1', 'value1.2']))); | 338 expect(stream1.toList(), completion(equals(['value1.1', 'value1.2']))); |
346 expect(stream2.toList(), completion(equals(['value2.1', 'value2.2']))); | 339 expect(stream2.toList(), completion(equals(['value2.1', 'value2.2']))); |
347 expect(errorGroup.done, completes); | 340 expect(errorGroup.done, completes); |
348 | 341 |
349 controller1..add('value1.1')..add('value1.2')..close(); | 342 controller1..add('value1.1')..add('value1.2')..close(); |
350 controller2..add('value2.1')..add('value2.2')..close(); | 343 controller2..add('value2.1')..add('value2.2')..close(); |
351 }); | 344 }); |
352 | 345 |
353 test("shouldn't throw a top-level exception if a stream receives an error " | 346 test("shouldn't throw a top-level exception if a stream receives an error " |
354 "after the other listened stream completes", () { | 347 "after the other listened stream completes", () { |
355 var signal = new Completer(); | 348 expect(stream1.toList(), completion(equals(['value1', 'value2']))); |
356 expect(stream1.toList().whenComplete(signal.complete), | |
357 completion(equals(['value1', 'value2']))); | |
358 controller1..add('value1')..add('value2')..close(); | 349 controller1..add('value1')..add('value2')..close(); |
359 | 350 |
360 expect(signal.future.then((_) { | 351 expect(stream1.toList().then((_) { |
361 // shouldn't cause a top-level exception | 352 // shouldn't cause a top-level exception |
362 controller2.addError(new FormatException()); | 353 controller2.addError(new FormatException()); |
363 }), completes); | 354 }), completes); |
364 }); | 355 }); |
365 | 356 |
366 test("shouldn't throw a top-level exception if an error is signaled after " | 357 test("shouldn't throw a top-level exception if an error is signaled after " |
367 "one listened stream completes", () { | 358 "one listened stream completes", () { |
368 var signal = new Completer(); | 359 expect(stream1.toList(), completion(equals(['value1', 'value2']))); |
369 expect(stream1.toList().whenComplete(signal.complete), | |
370 completion(equals(['value1', 'value2']))); | |
371 controller1..add('value1')..add('value2')..close(); | 360 controller1..add('value1')..add('value2')..close(); |
372 | 361 |
373 expect(signal.future.then((_) { | 362 expect(stream1.toList().then((_) { |
374 // shouldn't cause a top-level exception | 363 // shouldn't cause a top-level exception |
375 errorGroup.signalError(new FormatException()); | 364 errorGroup.signalError(new FormatException()); |
376 }), completes); | 365 }), completes); |
377 }); | 366 }); |
378 }); | 367 }); |
379 | 368 |
380 group('with a stream and a future', () { | 369 group('with a stream and a future', () { |
381 StreamController controller; | 370 StreamController controller; |
382 Stream stream; | 371 Stream stream; |
383 Completer completer; | 372 Completer completer; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 completer.complete('value'); | 412 completer.complete('value'); |
424 | 413 |
425 expect(future.then((_) { | 414 expect(future.then((_) { |
426 // shouldn't cause a top-level exception | 415 // shouldn't cause a top-level exception |
427 controller.addError(new FormatException()); | 416 controller.addError(new FormatException()); |
428 }), completes); | 417 }), completes); |
429 }); | 418 }); |
430 | 419 |
431 test("shouldn't throw a top-level exception if the future receives an " | 420 test("shouldn't throw a top-level exception if the future receives an " |
432 "error after the listened stream completes", () { | 421 "error after the listened stream completes", () { |
433 var signal = new Completer(); | 422 expect(stream.toList(), completion(equals(['value1', 'value2']))); |
434 expect(stream.toList().whenComplete(signal.complete), | |
435 completion(equals(['value1', 'value2']))); | |
436 controller..add('value1')..add('value2')..close(); | 423 controller..add('value1')..add('value2')..close(); |
437 | 424 |
438 expect(signal.future.then((_) { | 425 expect(stream.toList().then((_) { |
439 // shouldn't cause a top-level exception | 426 // shouldn't cause a top-level exception |
440 completer.completeError(new FormatException()); | 427 completer.completeError(new FormatException()); |
441 }), completes); | 428 }), completes); |
442 }); | 429 }); |
443 }); | 430 }); |
444 } | 431 } |
OLD | NEW |