| 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 // Test the event/callback protocol of the stream implementations. | 5 // Test the event/callback protocol of the stream implementations. |
| 6 library stream_state_test; | 6 library stream_state_test; |
| 7 | 7 |
| 8 import "../../../pkg/unittest/lib/unittest.dart"; | 8 import "../../../pkg/unittest/lib/unittest.dart"; |
| 9 import "stream_state_helper.dart"; | 9 import "stream_state_helper.dart"; |
| 10 | 10 |
| 11 const ms5 = const Duration(milliseconds: 5); | 11 const ms5 = const Duration(milliseconds: 5); |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 mainTest(sync: true, asBroadcast: false); | 14 mainTest(sync: true, asBroadcast: false); |
| 15 mainTest(sync: true, asBroadcast: true); | 15 mainTest(sync: true, asBroadcast: true); |
| 16 mainTest(sync: false, asBroadcast: false); | 16 mainTest(sync: false, asBroadcast: false); |
| 17 mainTest(sync: false, asBroadcast: true); | 17 mainTest(sync: false, asBroadcast: true); |
| 18 } | 18 } |
| 19 | 19 |
| 20 void terminateWithDone(t, asBroadcast) { |
| 21 if (asBroadcast) { |
| 22 t..expectCancel() |
| 23 ..expectDone() |
| 24 ..expectBroadcastCancel((_) => t.terminate()); |
| 25 } else { |
| 26 t..expectCancel() |
| 27 ..expectDone(t.terminate); |
| 28 } |
| 29 } |
| 30 |
| 20 mainTest({bool sync, bool asBroadcast}) { | 31 mainTest({bool sync, bool asBroadcast}) { |
| 21 var p = (sync ? "S" : "AS") + (asBroadcast ? "BC" : "SC"); | 32 var p = (sync ? "S" : "AS") + (asBroadcast ? "BC" : "SC"); |
| 22 test("$p-sub-data-done", () { | 33 test("$p-sub-data-done", () { |
| 23 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) | 34 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 24 : new StreamProtocolTest(sync: sync); | 35 : new StreamProtocolTest(sync: sync); |
| 25 t..expectListen() | 36 t..expectListen() |
| 26 ..expectBroadcastListenOpt() | 37 ..expectBroadcastListenOpt() |
| 27 ..expectData(42) | 38 ..expectData(42); |
| 28 ..expectDone() | 39 terminateWithDone(t, asBroadcast); |
| 29 ..expectBroadcastCancelOpt() | |
| 30 ..expectCancel(t.terminate); | |
| 31 t..listen()..add(42)..close(); | 40 t..listen()..add(42)..close(); |
| 32 }); | 41 }); |
| 33 | 42 |
| 34 test("$p-data-done-sub-sync", () { | 43 test("$p-data-done-sub-sync", () { |
| 35 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) | 44 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 36 : new StreamProtocolTest(sync: sync); | 45 : new StreamProtocolTest(sync: sync); |
| 37 t..expectListen() | 46 t..expectListen() |
| 38 ..expectBroadcastListenOpt() | 47 ..expectBroadcastListenOpt() |
| 39 ..expectData(42) | 48 ..expectData(42); |
| 40 ..expectDone() | 49 terminateWithDone(t, asBroadcast); |
| 41 ..expectBroadcastCancelOpt() | |
| 42 ..expectCancel(t.terminate); | |
| 43 t..add(42)..close()..listen(); | 50 t..add(42)..close()..listen(); |
| 44 }); | 51 }); |
| 45 | 52 |
| 46 test("$p-data-done-sub-async", () { | 53 test("$p-data-done-sub-async", () { |
| 47 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) | 54 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 48 : new StreamProtocolTest(sync: sync); | 55 : new StreamProtocolTest(sync: sync); |
| 49 t..expectListen() | 56 t..expectListen() |
| 50 ..expectBroadcastListenOpt() | 57 ..expectBroadcastListenOpt() |
| 51 ..expectData(42) | 58 ..expectData(42); |
| 52 ..expectDone() | 59 terminateWithDone(t, asBroadcast); |
| 53 ..expectBroadcastCancelOpt() | |
| 54 ..expectCancel(t.terminate); | |
| 55 t..add(42)..close()..listen(); | 60 t..add(42)..close()..listen(); |
| 56 }); | 61 }); |
| 57 | 62 |
| 58 test("$p-sub-data/pause+resume-done", () { | 63 test("$p-sub-data/pause+resume-done", () { |
| 59 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) | 64 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 60 : new StreamProtocolTest(sync: sync); | 65 : new StreamProtocolTest(sync: sync); |
| 61 t..expectListen() | 66 t..expectListen() |
| 62 ..expectBroadcastListenOpt() | 67 ..expectBroadcastListenOpt() |
| 63 ..expectData(42, () { | 68 ..expectData(42, () { |
| 64 t.pause(); | 69 t.pause(); |
| 65 t.resume(); | 70 t.resume(); |
| 66 t.close(); | 71 t.close(); |
| 67 }) | 72 }); |
| 68 ..expectDone() | 73 terminateWithDone(t, asBroadcast); |
| 69 ..expectBroadcastCancelOpt() | |
| 70 ..expectCancel(t.terminate); | |
| 71 t..listen()..add(42); | 74 t..listen()..add(42); |
| 72 }); | 75 }); |
| 73 | 76 |
| 74 test("$p-sub-data-unsubonerror", () { | 77 test("$p-sub-data-unsubonerror", () { |
| 75 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) | 78 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 76 : new StreamProtocolTest(sync: sync); | 79 : new StreamProtocolTest(sync: sync); |
| 77 t..expectListen() | 80 if (asBroadcast) { |
| 78 ..expectBroadcastListenOpt() | 81 t..expectListen() |
| 79 ..expectData(42) | 82 ..expectBroadcastListen() |
| 80 ..expectError("bad") | 83 ..expectData(42) |
| 81 ..expectBroadcastCancelOpt() | 84 ..expectError("bad") |
| 82 ..expectCancel(t.terminate); | 85 ..expectBroadcastCancel() |
| 86 ..expectCancel(t.terminate); |
| 87 } else { |
| 88 t..expectListen() |
| 89 ..expectData(42) |
| 90 ..expectCancel() |
| 91 ..expectError("bad", t.terminate); |
| 92 } |
| 83 t..listen(cancelOnError: true) | 93 t..listen(cancelOnError: true) |
| 84 ..add(42) | 94 ..add(42) |
| 85 ..error("bad") | 95 ..error("bad") |
| 86 ..add(43) | 96 ..add(43) |
| 87 ..close(); | 97 ..close(); |
| 88 }); | 98 }); |
| 89 | 99 |
| 90 test("$p-sub-data-no-unsubonerror", () { | 100 test("$p-sub-data-no-unsubonerror", () { |
| 91 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) | 101 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 92 : new StreamProtocolTest(sync: sync); | 102 : new StreamProtocolTest(sync: sync); |
| 93 t..expectListen() | 103 t..expectListen() |
| 94 ..expectBroadcastListenOpt() | 104 ..expectBroadcastListenOpt() |
| 95 ..expectData(42) | 105 ..expectData(42) |
| 96 ..expectError("bad") | 106 ..expectError("bad") |
| 97 ..expectData(43) | 107 ..expectData(43); |
| 98 ..expectDone() | 108 terminateWithDone(t, asBroadcast); |
| 99 ..expectBroadcastCancelOpt() | |
| 100 ..expectCancel(t.terminate); | |
| 101 t..listen(cancelOnError: false) | 109 t..listen(cancelOnError: false) |
| 102 ..add(42) | 110 ..add(42) |
| 103 ..error("bad") | 111 ..error("bad") |
| 104 ..add(43) | 112 ..add(43) |
| 105 ..close(); | 113 ..close(); |
| 106 }); | 114 }); |
| 107 | 115 |
| 108 test("$p-pause-resume-during-event", () { | 116 test("$p-pause-resume-during-event", () { |
| 109 var t = asBroadcast ? new StreamProtocolTest.broadcast(sync: sync) | 117 var t = asBroadcast ? new StreamProtocolTest.broadcast(sync: sync) |
| 110 : new StreamProtocolTest(sync: sync); | 118 : new StreamProtocolTest(sync: sync); |
| 111 t..expectListen() | 119 t..expectListen() |
| 112 ..expectBroadcastListenOpt() | 120 ..expectBroadcastListenOpt() |
| 113 ..expectData(42, () { | 121 ..expectData(42, () { |
| 114 t.pause(); | 122 t.pause(); |
| 115 t.resume(); | 123 t.resume(); |
| 116 }); | 124 }); |
| 117 if (!asBroadcast && !sync) { | 125 if (!asBroadcast && !sync) { |
| 118 t..expectPause(); | 126 t..expectPause(); |
| 119 } | 127 } |
| 120 t..expectDone() | 128 if (asBroadcast && sync) { |
| 121 ..expectBroadcastCancelOpt() | 129 t..expectDone() |
| 122 ..expectCancel(t.terminate); | 130 ..expectCancel(t.terminate); |
| 131 } else { |
| 132 t..expectCancel() |
| 133 ..expectDone(t.terminate); |
| 134 } |
| 123 t..listen() | 135 t..listen() |
| 124 ..add(42) | 136 ..add(42) |
| 125 ..close(); | 137 ..close(); |
| 126 }); | 138 }); |
| 139 |
| 140 test("$p-cancel-on-data", () { |
| 141 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 142 : new StreamProtocolTest(sync: sync); |
| 143 t..expectListen() |
| 144 ..expectBroadcastListenOpt() |
| 145 ..expectData(42, t.cancel) |
| 146 ..expectBroadcastCancelOpt() |
| 147 ..expectCancel(t.terminate); |
| 148 t..listen(cancelOnError: false) |
| 149 ..add(42) |
| 150 ..close(); |
| 151 }); |
| 152 |
| 153 test("$p-cancel-on-error", () { |
| 154 var t = asBroadcast ? new StreamProtocolTest.asBroadcast(sync: sync) |
| 155 : new StreamProtocolTest(sync: sync); |
| 156 t..expectListen() |
| 157 ..expectBroadcastListenOpt() |
| 158 ..expectError(42, t.cancel) |
| 159 ..expectBroadcastCancelOpt() |
| 160 ..expectCancel(t.terminate); |
| 161 t..listen(cancelOnError: false) |
| 162 ..error(42) |
| 163 ..close(); |
| 164 }); |
| 127 } | 165 } |
| OLD | NEW |