OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 import 'dart:async'; |
| 6 |
| 7 import 'package:stream_channel/stream_channel.dart'; |
| 8 import 'package:test/test.dart'; |
| 9 |
| 10 import 'utils.dart'; |
| 11 |
| 12 void main() { |
| 13 var completer; |
| 14 var streamController; |
| 15 var sinkController; |
| 16 var innerChannel; |
| 17 setUp(() { |
| 18 completer = new StreamChannelCompleter(); |
| 19 streamController = new StreamController(); |
| 20 sinkController = new StreamController(); |
| 21 innerChannel = new StreamChannel( |
| 22 streamController.stream, sinkController.sink); |
| 23 }); |
| 24 |
| 25 group("when a channel is set before accessing", () { |
| 26 test("forwards events through the stream", () { |
| 27 completer.setChannel(innerChannel); |
| 28 expect(completer.channel.stream.toList(), completion(equals([1, 2, 3]))); |
| 29 |
| 30 streamController.add(1); |
| 31 streamController.add(2); |
| 32 streamController.add(3); |
| 33 streamController.close(); |
| 34 }); |
| 35 |
| 36 test("forwards events through the sink", () { |
| 37 completer.setChannel(innerChannel); |
| 38 expect(sinkController.stream.toList(), completion(equals([1, 2, 3]))); |
| 39 |
| 40 completer.channel.sink.add(1); |
| 41 completer.channel.sink.add(2); |
| 42 completer.channel.sink.add(3); |
| 43 completer.channel.sink.close(); |
| 44 }); |
| 45 |
| 46 test("forwards an error through the stream", () { |
| 47 completer.setError("oh no"); |
| 48 expect(completer.channel.stream.first, throwsA("oh no")); |
| 49 }); |
| 50 |
| 51 test("drops sink events", () { |
| 52 completer.setError("oh no"); |
| 53 expect(completer.channel.sink.done, completes); |
| 54 completer.channel.sink.add(1); |
| 55 completer.channel.sink.addError("oh no"); |
| 56 }); |
| 57 }); |
| 58 |
| 59 group("when a channel is set after accessing", () { |
| 60 test("forwards events through the stream", () async { |
| 61 expect(completer.channel.stream.toList(), completion(equals([1, 2, 3]))); |
| 62 await pumpEventQueue(); |
| 63 |
| 64 completer.setChannel(innerChannel); |
| 65 streamController.add(1); |
| 66 streamController.add(2); |
| 67 streamController.add(3); |
| 68 streamController.close(); |
| 69 }); |
| 70 |
| 71 test("forwards events through the sink", () async { |
| 72 completer.channel.sink.add(1); |
| 73 completer.channel.sink.add(2); |
| 74 completer.channel.sink.add(3); |
| 75 completer.channel.sink.close(); |
| 76 await pumpEventQueue(); |
| 77 |
| 78 completer.setChannel(innerChannel); |
| 79 expect(sinkController.stream.toList(), completion(equals([1, 2, 3]))); |
| 80 }); |
| 81 |
| 82 test("forwards an error through the stream", () async { |
| 83 expect(completer.channel.stream.first, throwsA("oh no")); |
| 84 await pumpEventQueue(); |
| 85 |
| 86 completer.setError("oh no"); |
| 87 }); |
| 88 |
| 89 test("drops sink events", () async { |
| 90 expect(completer.channel.sink.done, completes); |
| 91 completer.channel.sink.add(1); |
| 92 completer.channel.sink.addError("oh no"); |
| 93 await pumpEventQueue(); |
| 94 |
| 95 completer.setError("oh no"); |
| 96 }); |
| 97 }); |
| 98 |
| 99 group("forFuture", () { |
| 100 test("forwards a StreamChannel", () { |
| 101 var channel = StreamChannelCompleter.fromFuture( |
| 102 new Future.value(innerChannel)); |
| 103 channel.sink.add(1); |
| 104 channel.sink.close(); |
| 105 streamController.sink.add(2); |
| 106 streamController.sink.close(); |
| 107 |
| 108 expect(sinkController.stream.toList(), completion(equals([1]))); |
| 109 expect(channel.stream.toList(), completion(equals([2]))); |
| 110 }); |
| 111 |
| 112 test("forwards an error", () { |
| 113 var channel = StreamChannelCompleter.fromFuture( |
| 114 new Future.error("oh no")); |
| 115 expect(channel.stream.toList(), throwsA("oh no")); |
| 116 }); |
| 117 }); |
| 118 |
| 119 test("doesn't allow the channel to be set multiple times", () { |
| 120 completer.setChannel(innerChannel); |
| 121 expect(() => completer.setChannel(innerChannel), throwsStateError); |
| 122 expect(() => completer.setChannel(innerChannel), throwsStateError); |
| 123 }); |
| 124 } |
OLD | NEW |