OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library isolate_current_test; |
| 6 import "dart:isolate"; |
| 7 import "dart:async"; |
| 8 import "package:expect/expect.dart"; |
| 9 import "package:async_helper/async_helper.dart"; |
| 10 |
| 11 void main() { |
| 12 asyncStart(); |
| 13 |
| 14 Expect.isNotNull(Isolate.current); |
| 15 |
| 16 // Sending controlPort and capabilities as list. |
| 17 testSend(i2l, l2i); |
| 18 testSpawnReturnVsCurrent(true); |
| 19 testSpawnReturnVsCurrent2(true); |
| 20 |
| 21 // Sending Isolate itself. |
| 22 testSend(id, id); |
| 23 testSpawnReturnVsCurrent(false); |
| 24 testSpawnReturnVsCurrent2(false); |
| 25 |
| 26 asyncEnd(); |
| 27 } |
| 28 |
| 29 /** Test sending the isolate data or isolate through a [SendPort]. */ |
| 30 void testSend(i2l, l2i) { |
| 31 asyncStart(); |
| 32 RawReceivePort p = new RawReceivePort(); |
| 33 Isolate isolate = Isolate.current; |
| 34 p.handler = (list) { |
| 35 var isolate2 = l2i(list); |
| 36 Expect.equals(isolate.controlPort, isolate2.controlPort); |
| 37 Expect.equals(isolate.pauseCapability, isolate2.pauseCapability); |
| 38 Expect.equals(isolate.terminateCapability, isolate2.terminateCapability); |
| 39 p.close(); |
| 40 asyncEnd(); |
| 41 }; |
| 42 p.sendPort.send(i2l(isolate)); |
| 43 } |
| 44 |
| 45 /** |
| 46 * Test that the isolate returned by [Isolate.spawn] is the same as |
| 47 * the one returned by [Isolate.current] in the spawned isolate. |
| 48 * Checked in the spawning isolate. |
| 49 */ |
| 50 void testSpawnReturnVsCurrent(bool asList) { |
| 51 asyncStart(); |
| 52 Function transform = asList ? l2i : id; |
| 53 Completer response = new Completer(); |
| 54 var p = new RawReceivePort(); |
| 55 p.handler = (v) { |
| 56 response.complete(transform(v)); |
| 57 p.close(); |
| 58 }; |
| 59 |
| 60 Isolate.spawn(replyCurrent, [p.sendPort, asList]).then((Isolate isolate) { |
| 61 return response.future.then((Isolate isolate2) { |
| 62 expectIsolateEquals(isolate, isolate2); |
| 63 asyncEnd(); |
| 64 }); |
| 65 }); |
| 66 } |
| 67 |
| 68 void replyCurrent(args) { |
| 69 SendPort responsePort = args[0]; |
| 70 Function transform = args[1] ? i2l : id; |
| 71 responsePort.send(transform(Isolate.current)); |
| 72 } |
| 73 |
| 74 /** |
| 75 * Test that the isolate returned by [Isolate.spawn] is the same as |
| 76 * the one returned by [Isolate.current] in the spawned isolate. |
| 77 * Checked in the spawned isolate. |
| 78 */ |
| 79 void testSpawnReturnVsCurrent2(bool asList) { |
| 80 asyncStart(); |
| 81 Function transform = asList ? i2l : id; |
| 82 |
| 83 Completer response = new Completer(); |
| 84 var p = new RawReceivePort(); |
| 85 int state = 0; |
| 86 p.handler = (v) { |
| 87 switch (state) { |
| 88 case 0: |
| 89 response.complete(v); |
| 90 state++; |
| 91 break; |
| 92 case 1: |
| 93 p.close(); |
| 94 Expect.isTrue(v); |
| 95 asyncEnd(); |
| 96 } |
| 97 }; |
| 98 |
| 99 Isolate.spawn(expectCurrent, [p.sendPort, asList]).then((Isolate isolate) { |
| 100 return response.future.then((SendPort port) { |
| 101 port.send(transform(isolate)); |
| 102 }); |
| 103 }); |
| 104 } |
| 105 |
| 106 void expectCurrent(args) { |
| 107 SendPort responsePort = args[0]; |
| 108 Function transform = args[1] ? l2i : id; |
| 109 RawReceivePort port = new RawReceivePort(); |
| 110 port.handler = (isoData) { |
| 111 Isolate isolate2 = transform(isoData); |
| 112 port.close(); |
| 113 Isolate isolate = Isolate.current; |
| 114 expectIsolateEquals(isolate, isolate2); |
| 115 responsePort.send(true); |
| 116 }; |
| 117 responsePort.send(port.sendPort); |
| 118 } |
| 119 |
| 120 /** Convert isolate to list (of control port and capabilities). */ |
| 121 i2l(Isolate isolate) => [isolate.controlPort, |
| 122 isolate.pauseCapability, |
| 123 isolate.terminateCapability]; |
| 124 /** Convert list to isolate. */ |
| 125 l2i(List list) => new Isolate(list[0], pauseCapability: list[1], |
| 126 terminateCapability: list[2]); |
| 127 |
| 128 /** Identity transformation. */ |
| 129 id(Isolate isolate) => isolate; |
| 130 |
| 131 void expectIsolateEquals(Isolate expect, Isolate actual) { |
| 132 Expect.equals(expect.controlPort, actual.controlPort); |
| 133 Expect.equals(expect.pauseCapability, actual.pauseCapability); |
| 134 Expect.equals(expect.terminateCapability, actual.terminateCapability); |
| 135 } |
OLD | NEW |