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 import "package:expect/expect.dart"; |
| 6 import "dart:async"; |
| 7 import "dart:io"; |
| 8 import "dart:isolate"; |
| 9 |
| 10 class TestConsumer implements StreamConsumer { |
| 11 final List expected; |
| 12 final List received = []; |
| 13 |
| 14 var closePort; |
| 15 |
| 16 int addStreamCount = 0; |
| 17 int expcetedAddStreamCount; |
| 18 |
| 19 TestConsumer(this.expected, |
| 20 {close: true, |
| 21 this.expcetedAddStreamCount: -1}) { |
| 22 if (close) closePort = new ReceivePort(); |
| 23 } |
| 24 |
| 25 Future addStream(Stream stream) { |
| 26 addStreamCount++; |
| 27 return stream.fold( |
| 28 received, |
| 29 (list, value) { |
| 30 list.addAll(value); |
| 31 return list; |
| 32 }) |
| 33 .then((_) {}); |
| 34 } |
| 35 |
| 36 Future close() { |
| 37 return new Future.immediate(null) |
| 38 .then((_) { |
| 39 if (closePort != null) closePort.close(); |
| 40 Expect.listEquals(expected, received); |
| 41 if (expcetedAddStreamCount >= 0) { |
| 42 Expect.equals(expcetedAddStreamCount, addStreamCount); |
| 43 } |
| 44 }); |
| 45 } |
| 46 } |
| 47 |
| 48 void testClose() { |
| 49 var sink = new IOSink(new TestConsumer([], expcetedAddStreamCount: 0)); |
| 50 sink.close(); |
| 51 } |
| 52 |
| 53 void testAddClose() { |
| 54 var sink = new IOSink(new TestConsumer([0])); |
| 55 sink.add([0]); |
| 56 sink.close(); |
| 57 |
| 58 sink = new IOSink(new TestConsumer([0, 1, 2])); |
| 59 sink.add([0]); |
| 60 sink.add([1]); |
| 61 sink.add([2]); |
| 62 sink.close(); |
| 63 } |
| 64 |
| 65 void testAddStreamClose() { |
| 66 { |
| 67 var sink = new IOSink(new TestConsumer([0])); |
| 68 var controller = new StreamController(); |
| 69 sink.addStream(controller.stream) |
| 70 .then((_) { |
| 71 sink.close(); |
| 72 }); |
| 73 controller.add([0]); |
| 74 controller.close(); |
| 75 } |
| 76 { |
| 77 var sink = new IOSink(new TestConsumer([0, 1, 2])); |
| 78 var controller = new StreamController(); |
| 79 sink.addStream(controller.stream) |
| 80 .then((_) { |
| 81 sink.close(); |
| 82 }); |
| 83 controller.add([0]); |
| 84 controller.add([1]); |
| 85 controller.add([2]); |
| 86 controller.close(); |
| 87 } |
| 88 } |
| 89 |
| 90 void testAddStreamAddClose() { |
| 91 { |
| 92 var sink = new IOSink(new TestConsumer([0, 1])); |
| 93 var controller = new StreamController(); |
| 94 sink.addStream(controller.stream) |
| 95 .then((_) { |
| 96 sink.add([1]); |
| 97 sink.close(); |
| 98 }); |
| 99 controller.add([0]); |
| 100 controller.close(); |
| 101 } |
| 102 } |
| 103 |
| 104 void main() { |
| 105 testClose(); |
| 106 testAddClose(); |
| 107 testAddStreamClose(); |
| 108 testAddStreamAddClose(); |
| 109 } |
OLD | NEW |