| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library test.cancelable_future; | 5 library analyzer.test.cancelable_future_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/cancelable_future.dart'; | 9 import 'package:analyzer/src/cancelable_future.dart'; |
| 10 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 10 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 11 import 'package:watcher/src/utils.dart'; | 12 import 'package:watcher/src/utils.dart'; |
| 12 | 13 |
| 13 import 'reflective_tests.dart'; | |
| 14 import 'utils.dart'; | 14 import 'utils.dart'; |
| 15 | 15 |
| 16 void main() { | 16 void main() { |
| 17 initializeTestEnvironment(); | 17 initializeTestEnvironment(); |
| 18 runReflectiveTests(CancelableCompleterTests); | 18 defineReflectiveTests(CancelableCompleterTests); |
| 19 runReflectiveTests(CancelableFutureTests); | 19 defineReflectiveTests(CancelableFutureTests); |
| 20 } | 20 } |
| 21 | 21 |
| 22 @reflectiveTest | 22 @reflectiveTest |
| 23 class CancelableCompleterTests { | 23 class CancelableCompleterTests { |
| 24 CancelableCompleter<Object> completer; | 24 CancelableCompleter<Object> completer; |
| 25 int cancelCount = 0; | 25 int cancelCount = 0; |
| 26 | 26 |
| 27 void setUp() { | 27 void setUp() { |
| 28 completer = new CancelableCompleter<Object>(() { | 28 completer = new CancelableCompleter<Object>(() { |
| 29 cancelCount++; | 29 cancelCount++; |
| 30 }); | 30 }); |
| 31 } | 31 } |
| 32 | 32 |
| 33 Future test_cancel_after_cancel() { | 33 Future test_cancel_after_cancel() { |
| 34 // It is permissible to cancel multiple times, but only the first | 34 // It is permissible to cancel multiple times, but only the first |
| 35 // cancellation has any effect. | 35 // cancellation has any effect. |
| 36 expect(cancelCount, 0); | 36 expect(cancelCount, 0); |
| 37 completer.future.cancel(); | 37 completer.future.cancel(); |
| 38 expect(cancelCount, 1); | 38 expect(cancelCount, 1); |
| 39 completer.future.cancel(); | 39 completer.future.cancel(); |
| 40 expect(cancelCount, 1); | 40 expect(cancelCount, 1); |
| 41 // Make sure the future still completes with error. | 41 // Make sure the future still completes with error. |
| 42 return completer.future.then((_) { | 42 return completer.future |
| 43 fail('Expected error completion'); | 43 .then((_) { |
| 44 }, onError: (error) { | 44 fail('Expected error completion'); |
| 45 expect(error, new isInstanceOf<FutureCanceledError>()); | 45 }, onError: (error) { |
| 46 // And make sure nothing else happens. | 46 expect(error, new isInstanceOf<FutureCanceledError>()); |
| 47 }).then((_) => pumpEventQueue()).then((_) { | 47 // And make sure nothing else happens. |
| 48 expect(completer.isCompleted, isFalse); | 48 }) |
| 49 expect(cancelCount, 1); | 49 .then((_) => pumpEventQueue()) |
| 50 }); | 50 .then((_) { |
| 51 expect(completer.isCompleted, isFalse); |
| 52 expect(cancelCount, 1); |
| 53 }); |
| 51 } | 54 } |
| 52 | 55 |
| 53 Future test_cancel_after_chaining() { | 56 Future test_cancel_after_chaining() { |
| 54 bool callbackInvoked = false; | 57 bool callbackInvoked = false; |
| 55 completer.future.then((_) { | 58 completer.future.then((_) { |
| 56 fail('Expected error completion'); | 59 fail('Expected error completion'); |
| 57 }, onError: (error) { | 60 }, onError: (error) { |
| 58 expect(callbackInvoked, isFalse); | 61 expect(callbackInvoked, isFalse); |
| 59 expect(error, new isInstanceOf<FutureCanceledError>()); | 62 expect(error, new isInstanceOf<FutureCanceledError>()); |
| 60 callbackInvoked = true; | 63 callbackInvoked = true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 75 } | 78 } |
| 76 | 79 |
| 77 Future test_cancel_after_complete() { | 80 Future test_cancel_after_complete() { |
| 78 Object obj = new Object(); | 81 Object obj = new Object(); |
| 79 completer.complete(obj); | 82 completer.complete(obj); |
| 80 completer.future.cancel(); | 83 completer.future.cancel(); |
| 81 // The cancel callback should not have been invoked, because it was too | 84 // The cancel callback should not have been invoked, because it was too |
| 82 // late to cancel. | 85 // late to cancel. |
| 83 expect(cancelCount, 0); | 86 expect(cancelCount, 0); |
| 84 // Make sure the future still completes with the object. | 87 // Make sure the future still completes with the object. |
| 85 return completer.future.then((result) { | 88 return completer.future |
| 86 expect(result, same(obj)); | 89 .then((result) { |
| 87 // And make sure nothing else happens. | 90 expect(result, same(obj)); |
| 88 }).then((_) => pumpEventQueue()).then((_) { | 91 // And make sure nothing else happens. |
| 89 expect(completer.isCompleted, isTrue); | 92 }) |
| 90 expect(cancelCount, 0); | 93 .then((_) => pumpEventQueue()) |
| 91 }); | 94 .then((_) { |
| 95 expect(completer.isCompleted, isTrue); |
| 96 expect(cancelCount, 0); |
| 97 }); |
| 92 } | 98 } |
| 93 | 99 |
| 94 Future test_cancel_before_chaining() { | 100 Future test_cancel_before_chaining() { |
| 95 completer.future.cancel(); | 101 completer.future.cancel(); |
| 96 // The cancel callback should have been invoked immediately. | 102 // The cancel callback should have been invoked immediately. |
| 97 expect(cancelCount, 1); | 103 expect(cancelCount, 1); |
| 98 // But the completer should remain in the "not completed" state. | 104 // But the completer should remain in the "not completed" state. |
| 99 expect(completer.isCompleted, isFalse); | 105 expect(completer.isCompleted, isFalse); |
| 100 bool callbackInvoked = false; | 106 bool callbackInvoked = false; |
| 101 completer.future.then((_) { | 107 completer.future.then((_) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 119 completer.future.cancel(); | 125 completer.future.cancel(); |
| 120 // The cancel callback should have been invoked immediately. | 126 // The cancel callback should have been invoked immediately. |
| 121 expect(cancelCount, 1); | 127 expect(cancelCount, 1); |
| 122 // Completing should have no effect other than to set the isCompleted | 128 // Completing should have no effect other than to set the isCompleted |
| 123 // flag. | 129 // flag. |
| 124 expect(completer.isCompleted, isFalse); | 130 expect(completer.isCompleted, isFalse); |
| 125 Object obj = new Object(); | 131 Object obj = new Object(); |
| 126 completer.complete(obj); | 132 completer.complete(obj); |
| 127 expect(completer.isCompleted, isTrue); | 133 expect(completer.isCompleted, isTrue); |
| 128 // Make sure the future still completer with error. | 134 // Make sure the future still completer with error. |
| 129 return completer.future.then((_) { | 135 return completer.future |
| 130 fail('Expected error completion'); | 136 .then((_) { |
| 131 }, onError: (error) { | 137 fail('Expected error completion'); |
| 132 expect(error, new isInstanceOf<FutureCanceledError>()); | 138 }, onError: (error) { |
| 133 // And make sure nothing else happens. | 139 expect(error, new isInstanceOf<FutureCanceledError>()); |
| 134 }).then((_) => pumpEventQueue()).then((_) { | 140 // And make sure nothing else happens. |
| 135 expect(completer.isCompleted, isTrue); | 141 }) |
| 136 expect(cancelCount, 1); | 142 .then((_) => pumpEventQueue()) |
| 137 }); | 143 .then((_) { |
| 144 expect(completer.isCompleted, isTrue); |
| 145 expect(cancelCount, 1); |
| 146 }); |
| 138 } | 147 } |
| 139 | 148 |
| 140 Future test_complete_after_chaining() { | 149 Future test_complete_after_chaining() { |
| 141 Object obj = new Object(); | 150 Object obj = new Object(); |
| 142 bool callbackInvoked = false; | 151 bool callbackInvoked = false; |
| 143 completer.future.then((result) { | 152 completer.future.then((result) { |
| 144 expect(callbackInvoked, isFalse); | 153 expect(callbackInvoked, isFalse); |
| 145 expect(result, same(obj)); | 154 expect(result, same(obj)); |
| 146 callbackInvoked = true; | 155 callbackInvoked = true; |
| 147 }, onError: (error) { | 156 }, onError: (error) { |
| 148 fail('Expected successful completion'); | 157 fail('Expected successful completion'); |
| 149 }); | 158 }); |
| 150 expect(completer.isCompleted, isFalse); | 159 expect(completer.isCompleted, isFalse); |
| 151 // Running the event loop should have no effect since the completer hasn't | 160 // Running the event loop should have no effect since the completer hasn't |
| 152 // been completed yet. | 161 // been completed yet. |
| 153 return pumpEventQueue().then((_) { | 162 return pumpEventQueue() |
| 154 completer.complete(obj); | 163 .then((_) { |
| 155 expect(completer.isCompleted, isTrue); | 164 completer.complete(obj); |
| 156 // The callback should be deferred to a microtask. | 165 expect(completer.isCompleted, isTrue); |
| 157 expect(callbackInvoked, isFalse); | 166 // The callback should be deferred to a microtask. |
| 158 }).then((_) => pumpEventQueue()).then((_) { | 167 expect(callbackInvoked, isFalse); |
| 159 expect(callbackInvoked, isTrue); | 168 }) |
| 160 expect(completer.isCompleted, isTrue); | 169 .then((_) => pumpEventQueue()) |
| 161 expect(cancelCount, 0); | 170 .then((_) { |
| 162 }); | 171 expect(callbackInvoked, isTrue); |
| 172 expect(completer.isCompleted, isTrue); |
| 173 expect(cancelCount, 0); |
| 174 }); |
| 163 } | 175 } |
| 164 | 176 |
| 165 void test_complete_after_complete() { | 177 void test_complete_after_complete() { |
| 166 // As with an ordinary Completer, calling complete() (or completeError) | 178 // As with an ordinary Completer, calling complete() (or completeError) |
| 167 // after calling complete() should throw an exception. | 179 // after calling complete() should throw an exception. |
| 168 completer.complete(); | 180 completer.complete(); |
| 169 expect(() { | 181 expect(() { |
| 170 completer.complete(); | 182 completer.complete(); |
| 171 }, throws); | 183 }, throws); |
| 172 expect(() { | 184 expect(() { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 completer.future.cancel(); | 226 completer.future.cancel(); |
| 215 // The cancel callback should have been invoked immediately. | 227 // The cancel callback should have been invoked immediately. |
| 216 expect(cancelCount, 1); | 228 expect(cancelCount, 1); |
| 217 // Completing should have no effect other than to set the isCompleted | 229 // Completing should have no effect other than to set the isCompleted |
| 218 // flag. | 230 // flag. |
| 219 expect(completer.isCompleted, isFalse); | 231 expect(completer.isCompleted, isFalse); |
| 220 Object obj = new Object(); | 232 Object obj = new Object(); |
| 221 completer.completeError(obj); | 233 completer.completeError(obj); |
| 222 expect(completer.isCompleted, isTrue); | 234 expect(completer.isCompleted, isTrue); |
| 223 // Make sure the future still completes with error. | 235 // Make sure the future still completes with error. |
| 224 return completer.future.then((_) { | 236 return completer.future |
| 225 fail('Expected error completion'); | 237 .then((_) { |
| 226 }, onError: (error) { | 238 fail('Expected error completion'); |
| 227 expect(error, new isInstanceOf<FutureCanceledError>()); | 239 }, onError: (error) { |
| 228 // And make sure nothing else happens. | 240 expect(error, new isInstanceOf<FutureCanceledError>()); |
| 229 }).then((_) => pumpEventQueue()).then((_) { | 241 // And make sure nothing else happens. |
| 230 expect(completer.isCompleted, isTrue); | 242 }) |
| 231 expect(cancelCount, 1); | 243 .then((_) => pumpEventQueue()) |
| 232 }); | 244 .then((_) { |
| 245 expect(completer.isCompleted, isTrue); |
| 246 expect(cancelCount, 1); |
| 247 }); |
| 233 } | 248 } |
| 234 | 249 |
| 235 Future test_completeError_after_chaining() { | 250 Future test_completeError_after_chaining() { |
| 236 Object obj = new Object(); | 251 Object obj = new Object(); |
| 237 bool callbackInvoked = false; | 252 bool callbackInvoked = false; |
| 238 completer.future.then((_) { | 253 completer.future.then((_) { |
| 239 fail('Expected error completion'); | 254 fail('Expected error completion'); |
| 240 }, onError: (error) { | 255 }, onError: (error) { |
| 241 expect(callbackInvoked, isFalse); | 256 expect(callbackInvoked, isFalse); |
| 242 expect(error, same(obj)); | 257 expect(error, same(obj)); |
| 243 callbackInvoked = true; | 258 callbackInvoked = true; |
| 244 }); | 259 }); |
| 245 expect(completer.isCompleted, isFalse); | 260 expect(completer.isCompleted, isFalse); |
| 246 // Running the event loop should have no effect since the completer hasn't | 261 // Running the event loop should have no effect since the completer hasn't |
| 247 // been completed yet. | 262 // been completed yet. |
| 248 return pumpEventQueue().then((_) { | 263 return pumpEventQueue() |
| 249 completer.completeError(obj); | 264 .then((_) { |
| 250 expect(completer.isCompleted, isTrue); | 265 completer.completeError(obj); |
| 251 // The callback should be deferred to a microtask. | 266 expect(completer.isCompleted, isTrue); |
| 252 expect(callbackInvoked, isFalse); | 267 // The callback should be deferred to a microtask. |
| 253 }).then((_) => pumpEventQueue()).then((_) { | 268 expect(callbackInvoked, isFalse); |
| 254 expect(callbackInvoked, isTrue); | 269 }) |
| 255 expect(completer.isCompleted, isTrue); | 270 .then((_) => pumpEventQueue()) |
| 256 expect(cancelCount, 0); | 271 .then((_) { |
| 257 }); | 272 expect(callbackInvoked, isTrue); |
| 273 expect(completer.isCompleted, isTrue); |
| 274 expect(cancelCount, 0); |
| 275 }); |
| 258 } | 276 } |
| 259 | 277 |
| 260 Future test_completeError_before_chaining() { | 278 Future test_completeError_before_chaining() { |
| 261 Object obj = new Object(); | 279 Object obj = new Object(); |
| 262 completer.completeError(obj); | 280 completer.completeError(obj); |
| 263 expect(completer.isCompleted, isTrue); | 281 expect(completer.isCompleted, isTrue); |
| 264 bool callbackInvoked = false; | 282 bool callbackInvoked = false; |
| 265 completer.future.then((_) { | 283 completer.future.then((_) { |
| 266 fail('Expected error completion'); | 284 fail('Expected error completion'); |
| 267 }, onError: (error) { | 285 }, onError: (error) { |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 | 420 |
| 403 Future test_value() { | 421 Future test_value() { |
| 404 Object obj = new Object(); | 422 Object obj = new Object(); |
| 405 return new CancelableFuture.value(obj).then((result) { | 423 return new CancelableFuture.value(obj).then((result) { |
| 406 expect(result, same(obj)); | 424 expect(result, same(obj)); |
| 407 }, onError: (error) { | 425 }, onError: (error) { |
| 408 fail('Expected successful completion'); | 426 fail('Expected successful completion'); |
| 409 }); | 427 }); |
| 410 } | 428 } |
| 411 } | 429 } |
| OLD | NEW |