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 import 'dart:isolate'; |
| 7 |
| 8 import 'package:stream_channel/stream_channel.dart'; |
| 9 import 'package:test/test.dart'; |
| 10 |
| 11 import 'utils.dart'; |
| 12 |
| 13 void main() { |
| 14 var receivePort; |
| 15 var sendPort; |
| 16 var channel; |
| 17 setUp(() { |
| 18 receivePort = new ReceivePort(); |
| 19 var receivePortForSend = new ReceivePort(); |
| 20 sendPort = receivePortForSend.sendPort; |
| 21 channel = new IsolateChannel(receivePortForSend, receivePort.sendPort); |
| 22 }); |
| 23 |
| 24 tearDown(() { |
| 25 receivePort.close(); |
| 26 channel.sink.close(); |
| 27 }); |
| 28 |
| 29 test("the channel can send messages", () { |
| 30 channel.sink.add(1); |
| 31 channel.sink.add(2); |
| 32 channel.sink.add(3); |
| 33 |
| 34 expect(receivePort.take(3).toList(), completion(equals([1, 2, 3]))); |
| 35 }); |
| 36 |
| 37 test("the channel can receive messages", () { |
| 38 sendPort.send(1); |
| 39 sendPort.send(2); |
| 40 sendPort.send(3); |
| 41 |
| 42 expect(channel.stream.take(3).toList(), completion(equals([1, 2, 3]))); |
| 43 }); |
| 44 |
| 45 test("events can't be added to an explicitly-closed sink", () { |
| 46 expect(channel.sink.close(), completes); |
| 47 expect(() => channel.sink.add(1), throwsStateError); |
| 48 expect(() => channel.sink.addError("oh no"), throwsStateError); |
| 49 expect(() => channel.sink.addStream(new Stream.fromIterable([])), |
| 50 throwsStateError); |
| 51 }); |
| 52 |
| 53 test("events can't be added while a stream is being added", () { |
| 54 var controller = new StreamController(); |
| 55 channel.sink.addStream(controller.stream); |
| 56 |
| 57 expect(() => channel.sink.add(1), throwsStateError); |
| 58 expect(() => channel.sink.addError("oh no"), throwsStateError); |
| 59 expect(() => channel.sink.addStream(new Stream.fromIterable([])), |
| 60 throwsStateError); |
| 61 expect(() => channel.sink.close(), throwsStateError); |
| 62 |
| 63 controller.close(); |
| 64 }); |
| 65 |
| 66 group("stream channel rules", () { |
| 67 test("closing the sink causes the stream to close before it emits any more " |
| 68 "events", () { |
| 69 sendPort.send(1); |
| 70 sendPort.send(2); |
| 71 sendPort.send(3); |
| 72 sendPort.send(4); |
| 73 sendPort.send(5); |
| 74 |
| 75 channel.stream.listen(expectAsync((message) { |
| 76 expect(message, equals(1)); |
| 77 channel.sink.close(); |
| 78 }, count: 1)); |
| 79 }); |
| 80 |
| 81 test("cancelling the stream's subscription has no effect on the sink", |
| 82 () async { |
| 83 channel.stream.listen(null).cancel(); |
| 84 await pumpEventQueue(); |
| 85 |
| 86 channel.sink.add(1); |
| 87 channel.sink.add(2); |
| 88 channel.sink.add(3); |
| 89 expect(receivePort.take(3).toList(), completion(equals([1, 2, 3]))); |
| 90 }); |
| 91 |
| 92 test("the sink closes as soon as an error is added", () async { |
| 93 channel.sink.addError("oh no"); |
| 94 channel.sink.add(1); |
| 95 expect(channel.sink.done, throwsA("oh no")); |
| 96 |
| 97 // Since the sink is closed, the stream should also be closed. |
| 98 expect(channel.stream.isEmpty, completion(isTrue)); |
| 99 |
| 100 // The other end shouldn't receive the next event, since the sink was |
| 101 // closed. Pump the event queue to give it a chance to. |
| 102 receivePort.listen(expectAsync((_) {}, count: 0)); |
| 103 await pumpEventQueue(); |
| 104 }); |
| 105 |
| 106 test("the sink closes as soon as an error is added via addStream", |
| 107 () async { |
| 108 var canceled = false; |
| 109 var controller = new StreamController(onCancel: () { |
| 110 canceled = true; |
| 111 }); |
| 112 |
| 113 // This future shouldn't get the error, because it's sent to [Sink.done]. |
| 114 expect(channel.sink.addStream(controller.stream), completes); |
| 115 |
| 116 controller.addError("oh no"); |
| 117 expect(channel.sink.done, throwsA("oh no")); |
| 118 await pumpEventQueue(); |
| 119 expect(canceled, isTrue); |
| 120 |
| 121 // Even though the sink is closed, this shouldn't throw an error because |
| 122 // the user didn't explicitly close it. |
| 123 channel.sink.add(1); |
| 124 }); |
| 125 }); |
| 126 } |
OLD | NEW |