| 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 library stream_state_helper; |
| 6 | 6 |
| 7 import "../../../pkg/unittest/lib/unittest.dart"; | 7 import "../../../pkg/unittest/lib/unittest.dart"; |
| 8 import "dart:async"; |
| 8 import "dart:collection"; | 9 import "dart:collection"; |
| 9 import "dart:async"; | |
| 10 | |
| 11 const ms5 = const Duration(milliseconds: 5); | |
| 12 | |
| 13 main() { | |
| 14 mainTest(false); | |
| 15 mainTest(true); | |
| 16 } | |
| 17 | |
| 18 mainTest(bool broadcast) { | |
| 19 var p = broadcast ? "BC" : "SC"; | |
| 20 test("$p-sub-data-done", () { | |
| 21 var t = new StreamProtocolTest(broadcast); | |
| 22 t..expectSubscription(true, false) | |
| 23 ..expectData(42) | |
| 24 ..expectDone() | |
| 25 ..expectSubscription(false, false); | |
| 26 t..subscribe()..add(42)..close(); | |
| 27 }); | |
| 28 | |
| 29 test("$p-data-done-sub", () { | |
| 30 var t = new StreamProtocolTest(broadcast); | |
| 31 if (broadcast) { | |
| 32 t..expectDone(); | |
| 33 } else { | |
| 34 t..expectSubscription(true, false) | |
| 35 ..expectData(42) | |
| 36 ..expectDone() | |
| 37 ..expectSubscription(false, false); | |
| 38 } | |
| 39 t..add(42)..close()..subscribe(); | |
| 40 }); | |
| 41 | |
| 42 test("$p-sub-data/pause-done", () { | |
| 43 var t = new StreamProtocolTest(broadcast); | |
| 44 t..expectSubscription(true, false) | |
| 45 ..expectData(42, () { | |
| 46 t.pause(new Future.delayed(ms5, () => null)); | |
| 47 }) | |
| 48 ..expectPause(true) | |
| 49 ..expectDone() | |
| 50 ..expectSubscription(false, false); | |
| 51 // We are calling "close" while the controller is actually paused, | |
| 52 // and it will stay paused until the pending events are sent. | |
| 53 t..subscribe()..add(42)..close(); | |
| 54 }); | |
| 55 | |
| 56 test("$p-sub-data/pause-resume/done", () { | |
| 57 var t = new StreamProtocolTest(broadcast); | |
| 58 t..expectSubscription(true, false) | |
| 59 ..expectData(42, () { | |
| 60 t.pause(new Future.delayed(ms5, () => null)); | |
| 61 }) | |
| 62 ..expectPause(true) | |
| 63 ..expectPause(false, () { t.close(); }) | |
| 64 ..expectDone() | |
| 65 ..expectSubscription(false, false); | |
| 66 t..subscribe()..add(42); | |
| 67 }); | |
| 68 | |
| 69 test("$p-sub-data/pause/resume/pause/resume-done", () { | |
| 70 var t = new StreamProtocolTest(broadcast); | |
| 71 t..expectSubscription(true, false) | |
| 72 ..expectData(42, () { | |
| 73 t.pause(); | |
| 74 }) | |
| 75 ..expectPause(true, () { t.resume(); }) | |
| 76 ..expectPause(false, () { t.pause(); }) | |
| 77 ..expectPause(true, () { t.resume(); }) | |
| 78 ..expectPause(false, () { t.close(); }) | |
| 79 ..expectDone() | |
| 80 ..expectSubscription(false, false); | |
| 81 t..subscribe()..add(42); | |
| 82 }); | |
| 83 | |
| 84 test("$p-sub-data/pause+resume-done", () { | |
| 85 var t = new StreamProtocolTest(broadcast); | |
| 86 t..expectSubscription(true, false) | |
| 87 ..expectData(42, () { | |
| 88 t.pause(); | |
| 89 t.resume(); | |
| 90 t.close(); | |
| 91 }) | |
| 92 ..expectDone() | |
| 93 ..expectSubscription(false, false); | |
| 94 t..subscribe()..add(42); | |
| 95 }); | |
| 96 | |
| 97 test("$p-sub-data/data+pause-data-resume-done", () { | |
| 98 var t = new StreamProtocolTest(broadcast); | |
| 99 t..expectSubscription(true, false) | |
| 100 ..expectData(42, () { | |
| 101 t.add(43); | |
| 102 t.pause(new Future.delayed(ms5, () => null)); | |
| 103 // Should now be paused until the future finishes. | |
| 104 // After that, the controller stays paused until the pending queue | |
| 105 // is empty. | |
| 106 }) | |
| 107 ..expectPause(true) | |
| 108 ..expectData(43) | |
| 109 ..expectPause(false, () { t.close(); }) | |
| 110 ..expectDone() | |
| 111 ..expectSubscription(false, false); | |
| 112 t..subscribe()..add(42); | |
| 113 }); | |
| 114 | |
| 115 test("$p-sub-data-unsubonerror", () { | |
| 116 var t = new StreamProtocolTest(broadcast); | |
| 117 t..expectSubscription(true, false) | |
| 118 ..expectData(42) | |
| 119 ..expectError("bad") | |
| 120 ..expectSubscription(false, !broadcast); | |
| 121 t..subscribe(unsubscribeOnError: true) | |
| 122 ..add(42) | |
| 123 ..error("bad") | |
| 124 ..add(43) | |
| 125 ..close(); | |
| 126 }); | |
| 127 | |
| 128 test("$p-sub-data-no-unsubonerror", () { | |
| 129 var t = new StreamProtocolTest(broadcast); | |
| 130 t..expectSubscription(true, false) | |
| 131 ..expectData(42) | |
| 132 ..expectError("bad") | |
| 133 ..expectData(43) | |
| 134 ..expectDone() | |
| 135 ..expectSubscription(false, false); | |
| 136 t..subscribe(unsubscribeOnError: false) | |
| 137 ..add(42) | |
| 138 ..error("bad") | |
| 139 ..add(43) | |
| 140 ..close(); | |
| 141 }); | |
| 142 | |
| 143 test("$p-pause-during-callback", () { | |
| 144 var t = new StreamProtocolTest(broadcast); | |
| 145 t..expectSubscription(true, false) | |
| 146 ..expectData(42, () { | |
| 147 t.pause(); | |
| 148 }) | |
| 149 ..expectPause(true, () { | |
| 150 t.resume(); | |
| 151 }) | |
| 152 ..expectPause(false, () { | |
| 153 t.pause(); | |
| 154 t.resume(); | |
| 155 t.close(); | |
| 156 }) | |
| 157 ..expectDone() | |
| 158 ..expectSubscription(false, false); | |
| 159 t..subscribe() | |
| 160 ..add(42); | |
| 161 }); | |
| 162 | |
| 163 test("$p-pause-resume-during-event", () { | |
| 164 var t = new StreamProtocolTest(broadcast); | |
| 165 t..expectSubscription(true, false) | |
| 166 ..expectData(42, () { | |
| 167 t.pause(); | |
| 168 t.resume(); | |
| 169 }) | |
| 170 ..expectDone() | |
| 171 ..expectSubscription(false, false); | |
| 172 t..subscribe() | |
| 173 ..add(42) | |
| 174 ..close(); | |
| 175 }); | |
| 176 | |
| 177 test("$p-cancel-sub-during-event", () { | |
| 178 var t = new StreamProtocolTest(broadcast); | |
| 179 t..expectSubscription(true, false) | |
| 180 ..expectData(42, () { | |
| 181 t.cancel(); | |
| 182 t.subscribe(); | |
| 183 }) | |
| 184 ..expectData(43) | |
| 185 ..expectDone() | |
| 186 ..expectSubscription(false, false); | |
| 187 t..subscribe() | |
| 188 ..add(42) | |
| 189 ..add(43) | |
| 190 ..close(); | |
| 191 }); | |
| 192 | |
| 193 test("$p-cancel-sub-during-callback", () { | |
| 194 var t = new StreamProtocolTest(broadcast); | |
| 195 t..expectSubscription(true, false) | |
| 196 ..expectData(42, () { | |
| 197 t.pause(); | |
| 198 }) | |
| 199 ..expectPause(true, () { | |
| 200 t.cancel(); // Cancels pause | |
| 201 t.subscribe(); | |
| 202 }) | |
| 203 ..expectPause(false) | |
| 204 ..expectData(43) | |
| 205 ..expectDone() | |
| 206 ..expectSubscription(false, false); | |
| 207 t..subscribe() | |
| 208 ..add(42) | |
| 209 ..add(43) | |
| 210 ..close(); | |
| 211 }); | |
| 212 | |
| 213 test("$p-sub-after-done-is-done", () { | |
| 214 var t = new StreamProtocolTest(broadcast); | |
| 215 t..expectSubscription(true, false) | |
| 216 ..expectDone() | |
| 217 ..expectSubscription(false, false) | |
| 218 ..expectDone(); | |
| 219 t..subscribe() | |
| 220 ..close() | |
| 221 ..subscribe(); // Subscribe after done does not cause callbacks at all. | |
| 222 }); | |
| 223 } | |
| 224 | |
| 225 // -------------------------------------------------------------------- | |
| 226 // Utility classes. | |
| 227 | 10 |
| 228 class StreamProtocolTest { | 11 class StreamProtocolTest { |
| 229 StreamController _controller; | 12 StreamController _controller; |
| 230 StreamSubscription _subscription; | 13 StreamSubscription _subscription; |
| 231 List<Event> _expectations = new List<Event>(); | 14 List<Event> _expectations = new List<Event>(); |
| 232 int _nextExpectationIndex = 0; | 15 int _nextExpectationIndex = 0; |
| 233 Function _onComplete; | 16 Function _onComplete; |
| 234 | 17 |
| 235 StreamProtocolTest([bool broadcast = false]) { | 18 StreamProtocolTest([bool broadcast = false]) { |
| 236 if (broadcast) { | 19 if (broadcast) { |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 _actual = "*[Paused:${c.isPaused}]"; | 282 _actual = "*[Paused:${c.isPaused}]"; |
| 500 return true; | 283 return true; |
| 501 } | 284 } |
| 502 bool _testSubcribe(StreamController c) { | 285 bool _testSubcribe(StreamController c) { |
| 503 _actual = "*[Subscribers:${c.hasSubscribers}, Paused:${c.isPaused}]"; | 286 _actual = "*[Subscribers:${c.hasSubscribers}, Paused:${c.isPaused}]"; |
| 504 return true; | 287 return true; |
| 505 } | 288 } |
| 506 | 289 |
| 507 String toString() => _actual; | 290 String toString() => _actual; |
| 508 } | 291 } |
| OLD | NEW |