| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2015, 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 library isolate.test.ports_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:isolate'; |
| 9 |
| 10 import 'package:isolate/ports.dart'; |
| 11 import 'package:test/test.dart'; |
| 12 |
| 13 const Duration MS = const Duration(milliseconds: 1); |
| 14 |
| 15 main() { |
| 16 group('SingleCallbackPort', testSingleCallbackPort); |
| 17 group('SingleCompletePort', testSingleCompletePort); |
| 18 group('SingleResponseFuture', testSingleResponseFuture); |
| 19 group('SingleResponseFuture', testSingleResultFuture); |
| 20 group('SingleResponseChannel', testSingleResponseChannel); |
| 21 } |
| 22 |
| 23 void testSingleCallbackPort() { |
| 24 test("Value", () { |
| 25 Completer completer = new Completer.sync(); |
| 26 SendPort p = singleCallbackPort(completer.complete); |
| 27 p.send(42); |
| 28 return completer.future.then((v) { |
| 29 expect(v, 42); |
| 30 }); |
| 31 }); |
| 32 |
| 33 test("FirstValue", () { |
| 34 Completer completer = new Completer.sync(); |
| 35 SendPort p = singleCallbackPort(completer.complete); |
| 36 p.send(42); |
| 37 p.send(37); |
| 38 return completer.future.then((v) { |
| 39 expect(v, 42); |
| 40 }); |
| 41 }); |
| 42 test("Value", () { |
| 43 Completer completer = new Completer.sync(); |
| 44 SendPort p = singleCallbackPort(completer.complete); |
| 45 p.send(42); |
| 46 return completer.future.then((v) { |
| 47 expect(v, 42); |
| 48 }); |
| 49 }); |
| 50 |
| 51 test("ValueBeforeTimeout", () { |
| 52 Completer completer = new Completer.sync(); |
| 53 SendPort p = singleCallbackPort(completer.complete, timeout: MS * 500); |
| 54 p.send(42); |
| 55 return completer.future.then((v) { |
| 56 expect(v, 42); |
| 57 }); |
| 58 }); |
| 59 |
| 60 test("Timeout", () { |
| 61 Completer completer = new Completer.sync(); |
| 62 singleCallbackPort(completer.complete, timeout: MS * 100, timeoutValue: 37); |
| 63 return completer.future.then((v) { |
| 64 expect(v, 37); |
| 65 }); |
| 66 }); |
| 67 |
| 68 test("TimeoutFirst", () { |
| 69 Completer completer = new Completer.sync(); |
| 70 SendPort p = singleCallbackPort(completer.complete, |
| 71 timeout: MS * 100, |
| 72 timeoutValue: 37); |
| 73 new Timer(MS * 500, () => p.send(42)); |
| 74 return completer.future.then((v) { |
| 75 expect(v, 37); |
| 76 }); |
| 77 }); |
| 78 } |
| 79 |
| 80 |
| 81 void testSingleCompletePort() { |
| 82 test("Value", () { |
| 83 Completer completer = new Completer.sync(); |
| 84 SendPort p = singleCompletePort(completer); |
| 85 p.send(42); |
| 86 return completer.future.then((v) { |
| 87 expect(v, 42); |
| 88 }); |
| 89 }); |
| 90 |
| 91 test("ValueCallback", () { |
| 92 Completer completer = new Completer.sync(); |
| 93 SendPort p = singleCompletePort(completer, callback: (v) { |
| 94 expect(42, v); |
| 95 return 87; |
| 96 }); |
| 97 p.send(42); |
| 98 return completer.future.then((v) { |
| 99 expect(v, 87); |
| 100 }); |
| 101 }); |
| 102 |
| 103 test("ValueCallbackFuture", () { |
| 104 Completer completer = new Completer.sync(); |
| 105 SendPort p = singleCompletePort(completer, callback: (v) { |
| 106 expect(42, v); |
| 107 return new Future.delayed(MS * 500, |
| 108 () => 88); |
| 109 }); |
| 110 p.send(42); |
| 111 return completer.future.then((v) { |
| 112 expect(v, 88); |
| 113 }); |
| 114 }); |
| 115 |
| 116 test("ValueCallbackThrows", () { |
| 117 Completer completer = new Completer.sync(); |
| 118 SendPort p = singleCompletePort(completer, callback: (v) { |
| 119 expect(42, v); |
| 120 throw 89; |
| 121 }); |
| 122 p.send(42); |
| 123 return completer.future.then((v) { |
| 124 fail("unreachable"); |
| 125 }, onError: (e, s) { |
| 126 expect(e, 89); |
| 127 }); |
| 128 }); |
| 129 |
| 130 test("ValueCallbackThrowsFuture", () { |
| 131 Completer completer = new Completer.sync(); |
| 132 SendPort p = singleCompletePort(completer, callback: (v) { |
| 133 expect(42, v); |
| 134 return new Future.error(90); |
| 135 }); |
| 136 p.send(42); |
| 137 return completer.future.then((v) { |
| 138 fail("unreachable"); |
| 139 }, onError: (e, s) { |
| 140 expect(e, 90); |
| 141 }); |
| 142 }); |
| 143 |
| 144 test("FirstValue", () { |
| 145 Completer completer = new Completer.sync(); |
| 146 SendPort p = singleCompletePort(completer); |
| 147 p.send(42); |
| 148 p.send(37); |
| 149 return completer.future.then((v) { |
| 150 expect(v, 42); |
| 151 }); |
| 152 }); |
| 153 |
| 154 test("FirstValueCallback", () { |
| 155 Completer completer = new Completer.sync(); |
| 156 SendPort p = singleCompletePort(completer, callback: (v) { |
| 157 expect(v, 42); |
| 158 return 87; |
| 159 }); |
| 160 p.send(42); |
| 161 p.send(37); |
| 162 return completer.future.then((v) { |
| 163 expect(v, 87); |
| 164 }); |
| 165 }); |
| 166 |
| 167 test("ValueBeforeTimeout", () { |
| 168 Completer completer = new Completer.sync(); |
| 169 SendPort p = singleCompletePort(completer, timeout: MS * 500); |
| 170 p.send(42); |
| 171 return completer.future.then((v) { |
| 172 expect(v, 42); |
| 173 }); |
| 174 }); |
| 175 |
| 176 test("Timeout", () { |
| 177 Completer completer = new Completer.sync(); |
| 178 singleCompletePort(completer, timeout: MS * 100); |
| 179 return completer.future.then((v) { |
| 180 fail("unreachable"); |
| 181 }, onError: (e, s) { |
| 182 expect(e is TimeoutException, isTrue); |
| 183 }); |
| 184 }); |
| 185 |
| 186 test("TimeoutCallback", () { |
| 187 Completer completer = new Completer.sync(); |
| 188 singleCompletePort(completer, timeout: MS * 100, onTimeout: () => 87); |
| 189 return completer.future.then((v) { |
| 190 expect(v, 87); |
| 191 }); |
| 192 }); |
| 193 |
| 194 test("TimeoutCallbackThrows", () { |
| 195 Completer completer = new Completer.sync(); |
| 196 singleCompletePort(completer, timeout: MS * 100, onTimeout: () => throw 91); |
| 197 return completer.future.then((v) { |
| 198 fail("unreachable"); |
| 199 }, onError: (e, s) { |
| 200 expect(e, 91); |
| 201 }); |
| 202 }); |
| 203 |
| 204 test("TimeoutCallbackFuture", () { |
| 205 Completer completer = new Completer.sync(); |
| 206 singleCompletePort(completer, |
| 207 timeout: MS * 100, |
| 208 onTimeout: () => new Future.value(87)); |
| 209 return completer.future.then((v) { |
| 210 expect(v, 87); |
| 211 }); |
| 212 }); |
| 213 |
| 214 test("TimeoutCallbackThrowsFuture", () { |
| 215 Completer completer = new Completer.sync(); |
| 216 singleCompletePort(completer, |
| 217 timeout: MS * 100, |
| 218 onTimeout: () => new Future.error(92)); |
| 219 return completer.future.then((v) { |
| 220 fail("unreachable"); |
| 221 }, onError: (e, s) { |
| 222 expect(e, 92); |
| 223 }); |
| 224 }); |
| 225 |
| 226 test("TimeoutCallbackSLow", () { |
| 227 Completer completer = new Completer.sync(); |
| 228 singleCompletePort( |
| 229 completer, |
| 230 timeout: MS * 100, |
| 231 onTimeout: () => new Future.delayed(MS * 500, () => 87)); |
| 232 return completer.future.then((v) { |
| 233 expect(v, 87); |
| 234 }); |
| 235 }); |
| 236 |
| 237 test("TimeoutCallbackThrowsSlow", () { |
| 238 Completer completer = new Completer.sync(); |
| 239 singleCompletePort( |
| 240 completer, |
| 241 timeout: MS * 100, |
| 242 onTimeout: () => new Future.delayed(MS * 500, () => throw 87)); |
| 243 return completer.future.then((v) { |
| 244 fail("unreachable"); |
| 245 }, onError: (e, s) { |
| 246 expect(e, 87); |
| 247 }); |
| 248 }); |
| 249 |
| 250 test("TimeoutFirst", () { |
| 251 Completer completer = new Completer.sync(); |
| 252 SendPort p = |
| 253 singleCompletePort(completer, timeout: MS * 100, onTimeout: () => 37); |
| 254 new Timer(MS * 500, () => p.send(42)); |
| 255 return completer.future.then((v) { |
| 256 expect(v, 37); |
| 257 }); |
| 258 }); |
| 259 } |
| 260 |
| 261 void testSingleResponseFuture() { |
| 262 test("FutureValue", () { |
| 263 return singleResponseFuture((SendPort p) { |
| 264 p.send(42); |
| 265 }).then((v) { |
| 266 expect(v, 42); |
| 267 }); |
| 268 }); |
| 269 |
| 270 test("FutureValueFirst", () { |
| 271 return singleResponseFuture((SendPort p) { |
| 272 p.send(42); |
| 273 p.send(37); |
| 274 }).then((v) { |
| 275 expect(v, 42); |
| 276 }); |
| 277 }); |
| 278 |
| 279 test("FutureError", () { |
| 280 return singleResponseFuture((SendPort p) { |
| 281 throw 93; |
| 282 }).then((v) { |
| 283 fail("unreachable"); |
| 284 }, onError: (e, s) { |
| 285 expect(e, 93); |
| 286 }); |
| 287 }); |
| 288 |
| 289 test("FutureTimeout", () { |
| 290 return singleResponseFuture((SendPort p) { |
| 291 // no-op. |
| 292 }, timeout: MS * 100).then((v) { |
| 293 expect(v, null); |
| 294 }); |
| 295 }); |
| 296 |
| 297 test("FutureTimeoutValue", () { |
| 298 return singleResponseFuture((SendPort p) { |
| 299 // no-op. |
| 300 }, timeout: MS * 100, timeoutValue: 42).then((v) { |
| 301 expect(v, 42); |
| 302 }); |
| 303 }); |
| 304 } |
| 305 |
| 306 void testSingleResultFuture() { |
| 307 test("Value", () { |
| 308 return singleResultFuture((SendPort p) { |
| 309 sendFutureResult(new Future.value(42), p); |
| 310 }).then((v) { |
| 311 expect(v, 42); |
| 312 }); |
| 313 }); |
| 314 |
| 315 test("ValueFirst", () { |
| 316 return singleResultFuture((SendPort p) { |
| 317 sendFutureResult(new Future.value(42), p); |
| 318 sendFutureResult(new Future.value(37), p); |
| 319 }).then((v) { |
| 320 expect(v, 42); |
| 321 }); |
| 322 }); |
| 323 |
| 324 test("Error", () { |
| 325 return singleResultFuture((SendPort p) { |
| 326 sendFutureResult(new Future.error(94), p); |
| 327 }).then((v) { |
| 328 fail("unreachable"); |
| 329 }, onError: (e, s) { |
| 330 expect(e is RemoteError, isTrue); |
| 331 }); |
| 332 }); |
| 333 |
| 334 test("ErrorFirst", () { |
| 335 return singleResultFuture((SendPort p) { |
| 336 sendFutureResult(new Future.error(95), p); |
| 337 sendFutureResult(new Future.error(96), p); |
| 338 }).then((v) { |
| 339 fail("unreachable"); |
| 340 }, onError: (e, s) { |
| 341 expect(e is RemoteError, isTrue); |
| 342 }); |
| 343 }); |
| 344 |
| 345 test("Error", () { |
| 346 return singleResultFuture((SendPort p) { |
| 347 throw 93; |
| 348 }).then((v) { |
| 349 fail("unreachable"); |
| 350 }, onError: (e, s) { |
| 351 expect(e is RemoteError, isTrue); |
| 352 }); |
| 353 }); |
| 354 |
| 355 test("Timeout", () { |
| 356 return singleResultFuture((SendPort p) { |
| 357 // no-op. |
| 358 }, timeout: MS * 100).then((v) { |
| 359 fail("unreachable"); |
| 360 }, onError: (e, s) { |
| 361 expect(e is TimeoutException, isTrue); |
| 362 }); |
| 363 }); |
| 364 |
| 365 test("TimeoutValue", () { |
| 366 return singleResultFuture((SendPort p) { |
| 367 // no-op. |
| 368 }, timeout: MS * 100, onTimeout: () => 42).then((v) { |
| 369 expect(v, 42); |
| 370 }); |
| 371 }); |
| 372 |
| 373 test("TimeoutError", () { |
| 374 return singleResultFuture((SendPort p) { |
| 375 // no-op. |
| 376 }, timeout: MS * 100, onTimeout: () => throw 97).then((v) { |
| 377 expect(v, 42); |
| 378 }, onError: (e, s) { |
| 379 expect(e, 97); |
| 380 }); |
| 381 }); |
| 382 } |
| 383 |
| 384 void testSingleResponseChannel() { |
| 385 test("Value", () { |
| 386 var channel = new SingleResponseChannel(); |
| 387 channel.port.send(42); |
| 388 return channel.result.then((v) { |
| 389 expect(v, 42); |
| 390 }); |
| 391 }); |
| 392 |
| 393 test("ValueFirst", () { |
| 394 var channel = new SingleResponseChannel(); |
| 395 channel.port.send(42); |
| 396 channel.port.send(37); |
| 397 return channel.result.then((v) { |
| 398 expect(v, 42); |
| 399 }); |
| 400 }); |
| 401 |
| 402 test("ValueCallback", () { |
| 403 var channel = new SingleResponseChannel(callback: (v) => v * 2); |
| 404 channel.port.send(42); |
| 405 return channel.result.then((v) { |
| 406 expect(v, 84); |
| 407 }); |
| 408 }); |
| 409 |
| 410 test("ErrorCallback", () { |
| 411 var channel = new SingleResponseChannel(callback: (v) => throw 42); |
| 412 channel.port.send(37); |
| 413 return channel.result.then((v) { fail("unreachable"); }, |
| 414 onError: (v, s) { |
| 415 expect(v, 42); |
| 416 }); |
| 417 }); |
| 418 |
| 419 test("AsyncValueCallback", () { |
| 420 var channel = new SingleResponseChannel( |
| 421 callback: (v) => new Future.value(v * 2)); |
| 422 channel.port.send(42); |
| 423 return channel.result.then((v) { |
| 424 expect(v, 84); |
| 425 }); |
| 426 }); |
| 427 |
| 428 test("AsyncErrorCallback", () { |
| 429 var channel = new SingleResponseChannel(callback: |
| 430 (v) => new Future.error(42)); |
| 431 channel.port.send(37); |
| 432 return channel.result.then((v) { fail("unreachable"); }, |
| 433 onError: (v, s) { |
| 434 expect(v, 42); |
| 435 }); |
| 436 }); |
| 437 |
| 438 test("Timeout", () { |
| 439 var channel = new SingleResponseChannel(timeout: MS * 100); |
| 440 return channel.result.then((v) { |
| 441 expect(v, null); |
| 442 }); |
| 443 }); |
| 444 |
| 445 test("TimeoutThrow", () { |
| 446 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 447 throwOnTimeout: true); |
| 448 return channel.result.then((v) { fail("unreachable"); }, |
| 449 onError: (v, s) { |
| 450 expect(v is TimeoutException, isTrue); |
| 451 }); |
| 452 }); |
| 453 |
| 454 test("TimeoutThrowOnTimeoutAndValue", () { |
| 455 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 456 throwOnTimeout: true, |
| 457 onTimeout: () => 42, |
| 458 timeoutValue: 42); |
| 459 return channel.result.then((v) { fail("unreachable"); }, |
| 460 onError: (v, s) { |
| 461 expect(v is TimeoutException, isTrue); |
| 462 }); |
| 463 }); |
| 464 |
| 465 test("TimeoutOnTimeout", () { |
| 466 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 467 onTimeout: () => 42); |
| 468 return channel.result.then((v) { |
| 469 expect(v, 42); |
| 470 }); |
| 471 }); |
| 472 |
| 473 test("TimeoutOnTimeoutAndValue", () { |
| 474 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 475 onTimeout: () => 42, |
| 476 timeoutValue: 37); |
| 477 return channel.result.then((v) { |
| 478 expect(v, 42); |
| 479 }); |
| 480 }); |
| 481 |
| 482 test("TimeoutValue", () { |
| 483 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 484 timeoutValue: 42); |
| 485 return channel.result.then((v) { |
| 486 expect(v, 42); |
| 487 }); |
| 488 }); |
| 489 |
| 490 test("TimeoutOnTimeoutError", () { |
| 491 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 492 onTimeout: () => throw 42); |
| 493 return channel.result.then((v) { fail("unreachable"); }, |
| 494 onError: (v, s) { |
| 495 expect(v, 42); |
| 496 }); |
| 497 }); |
| 498 |
| 499 test("TimeoutOnTimeoutAsync", () { |
| 500 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 501 onTimeout: |
| 502 () => new Future.value(42)); |
| 503 return channel.result.then((v) { |
| 504 expect(v, 42); |
| 505 }); |
| 506 }); |
| 507 |
| 508 test("TimeoutOnTimeoutAsyncError", () { |
| 509 var channel = new SingleResponseChannel(timeout: MS * 100, |
| 510 onTimeout: |
| 511 () => new Future.error(42)); |
| 512 return channel.result.then((v) { fail("unreachable"); }, |
| 513 onError: (v, s) { |
| 514 expect(v, 42); |
| 515 }); |
| 516 }); |
| 517 } |
| OLD | NEW |