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