| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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("PromiseBasedTest"); | 5 #library("PromiseBasedTest"); |
| 6 #import("dart:isolate"); | 6 #import("dart:isolate"); |
| 7 #import("../../../../proxy/promise.dart"); | 7 #import("../../../../proxy/promise.dart"); |
| 8 #import("../../../../../tests/isolate/src/TestFramework.dart"); | 8 #import("../../../../../lib/unittest/unittest.dart"); |
| 9 | 9 |
| 10 class TestIsolate extends Isolate { | 10 class TestIsolate extends Isolate { |
| 11 | 11 |
| 12 TestIsolate() : super(); | 12 TestIsolate() : super(); |
| 13 | 13 |
| 14 void main() { | 14 void main() { |
| 15 int seed = 0; | 15 int seed = 0; |
| 16 this.port.receive((var message, SendPort replyTo) { | 16 this.port.receive((var message, SendPort replyTo) { |
| 17 //print("Got ${message[0]}"); | 17 //print("Got ${message[0]}"); |
| 18 if (seed == 0) { | 18 if (seed == 0) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 30 } | 30 } |
| 31 | 31 |
| 32 } | 32 } |
| 33 | 33 |
| 34 Future promiseToFuture(Promise p) { | 34 Future promiseToFuture(Promise p) { |
| 35 Completer c = new Completer(); | 35 Completer c = new Completer(); |
| 36 p.then((v) { c.complete(v); }); | 36 p.then((v) { c.complete(v); }); |
| 37 return c.future; | 37 return c.future; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void test(TestExpectation expect) { | 40 void main() { |
| 41 Proxy proxy = new Proxy.forIsolate(new TestIsolate()); | 41 test("promise based proxies", () { |
| 42 proxy.send([42]); // Seed the isolate. | 42 Proxy proxy = new Proxy.forIsolate(new TestIsolate()); |
| 43 Promise<int> result = new PromiseProxy<int>(proxy.call([87])); | 43 proxy.send([42]); // Seed the isolate. |
| 44 Completer completer = new Completer(); | 44 Promise<int> result = new PromiseProxy<int>(proxy.call([87])); |
| 45 expect.completes(promiseToFuture(result)).then((int value) { | 45 Completer completer = new Completer(); |
| 46 //print("expect 1: $value"); | 46 result.then(later1((int value) { |
| 47 Expect.equals(42 + 87, value); | 47 //print("expect 1: $value"); |
| 48 completer.complete(99); | 48 Expect.equals(42 + 87, value); |
| 49 completer.complete(99); |
| 50 })); |
| 51 completer.future.then(later1((int value) { |
| 52 //print("expect 2: $value"); |
| 53 Expect.equals(99, value); |
| 54 })); |
| 49 }); | 55 }); |
| 50 expect.completes(completer.future).then((int value) { | 56 |
| 51 //print("expect 2: $value"); | 57 test("expanded test", () { |
| 52 Expect.equals(99, value); | 58 Proxy proxy = new Proxy.forIsolate(new TestIsolate()); |
| 53 expect.succeeded(); | 59 proxy.send([42]); // Seed the isolate. |
| 60 Promise<SendPort> sendCompleter = proxy.call([87]); |
| 61 Promise<int> result = new Promise<int>(); |
| 62 ReceivePort receivePort = new ReceivePort(); |
| 63 receivePort.receive((var msg, SendPort _) { |
| 64 receivePort.close(); |
| 65 //print("test completer"); |
| 66 result.complete(msg[0]); |
| 67 }); |
| 68 sendCompleter.addCompleteHandler((SendPort port) { |
| 69 //print("test send"); |
| 70 port.send([receivePort.toSendPort()], null); |
| 71 }); |
| 72 Completer completer = new Completer(); |
| 73 promiseToFuture(result).then(later1((int value) { |
| 74 //print("expect 1: $value"); |
| 75 Expect.equals(42 + 87, value); |
| 76 completer.complete(99); |
| 77 })); |
| 78 completer.future.then(later1((int value) { |
| 79 //print("expect 2: $value"); |
| 80 Expect.equals(99, value); |
| 81 })); |
| 54 }); | 82 }); |
| 55 } | 83 } |
| 56 | |
| 57 void expandedTest(TestExpectation expect) { | |
| 58 Proxy proxy = new Proxy.forIsolate(new TestIsolate()); | |
| 59 proxy.send([42]); // Seed the isolate. | |
| 60 Promise<SendPort> sendCompleter = proxy.call([87]); | |
| 61 Promise<int> result = new Promise<int>(); | |
| 62 ReceivePort receivePort = new ReceivePort(); | |
| 63 receivePort.receive((var msg, SendPort _) { | |
| 64 receivePort.close(); | |
| 65 //print("test completer"); | |
| 66 result.complete(msg[0]); | |
| 67 }); | |
| 68 sendCompleter.addCompleteHandler((SendPort port) { | |
| 69 //print("test send"); | |
| 70 port.send([receivePort.toSendPort()], null); | |
| 71 }); | |
| 72 Completer completer = new Completer(); | |
| 73 expect.completes(promiseToFuture(result)).then((int value) { | |
| 74 //print("expect 1: $value"); | |
| 75 Expect.equals(42 + 87, value); | |
| 76 completer.complete(99); | |
| 77 }); | |
| 78 expect.completes(completer.future).then((int value) { | |
| 79 //print("expect 2: $value"); | |
| 80 Expect.equals(99, value); | |
| 81 expect.succeeded(); | |
| 82 }); | |
| 83 } | |
| 84 | |
| 85 void main() { | |
| 86 runTests([test, expandedTest]); | |
| 87 } | |
| OLD | NEW |