| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, 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("PromiseBasedTest"); | |
| 6 #import("TestFramework.dart"); | |
| 7 | |
| 8 class TestIsolate extends Isolate { | |
| 9 | |
| 10 TestIsolate() : super(); | |
| 11 | |
| 12 void main() { | |
| 13 int seed = 0; | |
| 14 this.port.receive((var message, SendPort replyTo) { | |
| 15 //print("Got ${message[0]}"); | |
| 16 if (seed == 0) { | |
| 17 seed = message[0]; | |
| 18 } else { | |
| 19 Promise<int> response = new Promise<int>(); | |
| 20 var proxy = new Proxy.forPort(replyTo); | |
| 21 //print("send to proxy"); | |
| 22 proxy.send([response]); | |
| 23 //print("sent"); | |
| 24 response.complete(seed + message[0]); | |
| 25 this.port.close(); | |
| 26 } | |
| 27 }); | |
| 28 } | |
| 29 | |
| 30 } | |
| 31 | |
| 32 void test(TestExpectation expect) { | |
| 33 Proxy proxy = new Proxy.forIsolate(new TestIsolate()); | |
| 34 proxy.send([42]); // Seed the isolate. | |
| 35 Promise<int> result = new PromiseProxy<int>(proxy.call([87])); | |
| 36 Completer completer = new Completer(); | |
| 37 expect.completes(result).then((int value) { | |
| 38 //print("expect 1: $value"); | |
| 39 Expect.equals(42 + 87, value); | |
| 40 completer.complete(99); | |
| 41 }); | |
| 42 expect.completes(completer.future).then((int value) { | |
| 43 //print("expect 2: $value"); | |
| 44 Expect.equals(99, value); | |
| 45 expect.succeeded(); | |
| 46 }); | |
| 47 } | |
| 48 | |
| 49 void expandedTest(TestExpectation expect) { | |
| 50 Proxy proxy = new Proxy.forIsolate(new TestIsolate()); | |
| 51 proxy.send([42]); // Seed the isolate. | |
| 52 Promise<SendPort> sendCompleter = proxy.call([87]); | |
| 53 Promise<int> result = new Promise<int>(); | |
| 54 ReceivePort receivePort = new ReceivePort.singleShot(); | |
| 55 receivePort.receive((var msg, SendPort _) { | |
| 56 //print("test completer"); | |
| 57 result.complete(msg[0]); | |
| 58 }); | |
| 59 sendCompleter.addCompleteHandler((SendPort port) { | |
| 60 //print("test send"); | |
| 61 port.send([receivePort.toSendPort()], null); | |
| 62 }); | |
| 63 Completer completer = new Completer(); | |
| 64 expect.completes(result).then((int value) { | |
| 65 //print("expect 1: $value"); | |
| 66 Expect.equals(42 + 87, value); | |
| 67 completer.complete(99); | |
| 68 }); | |
| 69 expect.completes(completer.future).then((int value) { | |
| 70 //print("expect 2: $value"); | |
| 71 Expect.equals(99, value); | |
| 72 expect.succeeded(); | |
| 73 }); | |
| 74 } | |
| 75 | |
| 76 void main() { | |
| 77 runTests([test, expandedTest]); | |
| 78 } | |
| OLD | NEW |