| Index: tests/stub-generator/src/MintMakerFullyIsolatedTest-generatedTest.dart
|
| ===================================================================
|
| --- tests/stub-generator/src/MintMakerFullyIsolatedTest-generatedTest.dart (revision 0)
|
| +++ tests/stub-generator/src/MintMakerFullyIsolatedTest-generatedTest.dart (revision 0)
|
| @@ -0,0 +1,463 @@
|
| +/* class = Purse (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintMakerFullyIsolatedTest.dart: 10) */
|
| +
|
| +interface Purse$Proxy {
|
| + Promise<int> queryBalance();
|
| +
|
| + Purse$Proxy sproutPurse();
|
| +
|
| + Promise<int> deposit(int amount, Purse$Proxy source);
|
| +}
|
| +
|
| +class Purse$ProxyImpl extends Proxy implements Purse$Proxy {
|
| + Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { }
|
| + Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])) { }
|
| + factory Purse$ProxyImpl.createIsolate() {
|
| + Proxy isolate = new Proxy.forIsolate(new Purse$Dispatcher$Isolate());
|
| + return new Purse$ProxyImpl.forIsolate(isolate);
|
| + }
|
| + factory Purse$ProxyImpl.localProxy(Purse obj) {
|
| + return new Purse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(new Purse$Dispatcher(obj))));
|
| + }
|
| +
|
| + Promise<int> queryBalance() {
|
| + return this.call(["queryBalance"]);
|
| + }
|
| +
|
| + Purse$Proxy sproutPurse() {
|
| + return new Purse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["sproutPurse"])));
|
| + }
|
| +
|
| + Promise<int> deposit(int amount, Purse$Proxy source) {
|
| + return new PromiseProxy<int>(this.call(["deposit", amount, source]));
|
| + }
|
| +}
|
| +
|
| +class Purse$Dispatcher extends Dispatcher<Purse> {
|
| + Purse$Dispatcher(Purse thing) : super(thing) { }
|
| +
|
| + void process(var message, void reply(var response)) {
|
| + String command = message[0];
|
| + if (command == "Purse") {
|
| + } else if (command == "queryBalance") {
|
| + int queryBalance = target.queryBalance();
|
| + reply(queryBalance);
|
| + } else if (command == "sproutPurse") {
|
| + Purse$Proxy sproutPurse = target.sproutPurse();
|
| + reply(sproutPurse);
|
| + } else if (command == "deposit") {
|
| + int amount = message[1];
|
| + List<Promise<SendPort>> promises = new List<Promise<SendPort>>();
|
| + promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(message[2])));
|
| + Promise done = new Promise();
|
| + done.waitFor(promises, 1);
|
| + done.addCompleteHandler((_) {
|
| + Purse$Proxy source = new Purse$ProxyImpl(promises[0]);
|
| + Promise<int> deposit = target.deposit(amount, source);
|
| + reply(deposit);
|
| + });
|
| + } else {
|
| + // TODO(kasperl,benl): Somehow throw an exception instead.
|
| + reply("Exception: command '" + command + "' not understood by Purse.");
|
| + }
|
| + }
|
| +}
|
| +
|
| +class Purse$Dispatcher$Isolate extends Isolate {
|
| + Purse$Dispatcher$Isolate() : super() { }
|
| +
|
| + void main() {
|
| + this.port.receive(void _(var message, SendPort replyTo) {
|
| + Purse thing = new Purse();
|
| + SendPort port = Dispatcher.serve(new Purse$Dispatcher(thing));
|
| + Proxy proxy = new Proxy.forPort(replyTo);
|
| + proxy.send([port]);
|
| + });
|
| + }
|
| +}
|
| +
|
| +/* class = PowerfulPurse (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintMakerFullyIsolatedTest.dart: 19) */
|
| +
|
| +interface PowerfulPurse$Proxy {
|
| + void init(Mint$Proxy mint, int balance);
|
| +
|
| + Promise<int> grab(int amount);
|
| +
|
| + Purse$Proxy weak();
|
| +}
|
| +
|
| +class PowerfulPurse$ProxyImpl extends Proxy implements PowerfulPurse$Proxy {
|
| + PowerfulPurse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { }
|
| + PowerfulPurse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])) { }
|
| + factory PowerfulPurse$ProxyImpl.createIsolate() {
|
| + Proxy isolate = new Proxy.forIsolate(new PowerfulPurse$Dispatcher$Isolate());
|
| + return new PowerfulPurse$ProxyImpl.forIsolate(isolate);
|
| + }
|
| + factory PowerfulPurse$ProxyImpl.localProxy(PowerfulPurse obj) {
|
| + return new PowerfulPurse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(new PowerfulPurse$Dispatcher(obj))));
|
| + }
|
| +
|
| + void init(Mint$Proxy mint, int balance) {
|
| + this.send(["init", mint, balance]);
|
| + }
|
| +
|
| + Promise<int> grab(int amount) {
|
| + return this.call(["grab", amount]);
|
| + }
|
| +
|
| + Purse$Proxy weak() {
|
| + return new Purse$ProxyImpl(this.call(["weak"]));
|
| + }
|
| +}
|
| +
|
| +class PowerfulPurse$Dispatcher extends Dispatcher<PowerfulPurse> {
|
| + PowerfulPurse$Dispatcher(PowerfulPurse thing) : super(thing) { }
|
| +
|
| + void process(var message, void reply(var response)) {
|
| + String command = message[0];
|
| + if (command == "PowerfulPurse") {
|
| + } else if (command == "init") {
|
| + List<Promise<SendPort>> promises = new List<Promise<SendPort>>();
|
| + promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(message[1])));
|
| + int balance = message[2];
|
| + Promise done = new Promise();
|
| + done.waitFor(promises, 1);
|
| + done.addCompleteHandler((_) {
|
| + Mint$Proxy mint = new Mint$ProxyImpl(promises[0]);
|
| + target.init(mint, balance);
|
| + });
|
| + } else if (command == "grab") {
|
| + int amount = message[1];
|
| + int grab = target.grab(amount);
|
| + reply(grab);
|
| + } else if (command == "weak") {
|
| + Purse weak = target.weak();
|
| + SendPort port = Dispatcher.serve(new Purse$Dispatcher(weak));
|
| + reply(port);
|
| + } else {
|
| + // TODO(kasperl,benl): Somehow throw an exception instead.
|
| + reply("Exception: command '" + command + "' not understood by PowerfulPurse.");
|
| + }
|
| + }
|
| +}
|
| +
|
| +class PowerfulPurse$Dispatcher$Isolate extends Isolate {
|
| + PowerfulPurse$Dispatcher$Isolate() : super() { }
|
| +
|
| + void main() {
|
| + this.port.receive(void _(var message, SendPort replyTo) {
|
| + PowerfulPurse thing = new PowerfulPurse();
|
| + SendPort port = Dispatcher.serve(new PowerfulPurse$Dispatcher(thing));
|
| + Proxy proxy = new Proxy.forPort(replyTo);
|
| + proxy.send([port]);
|
| + });
|
| + }
|
| +}
|
| +
|
| +/* class = Mint (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintMakerFullyIsolatedTest.dart: 29) */
|
| +
|
| +interface Mint$Proxy {
|
| + Purse$Proxy createPurse(int balance);
|
| +
|
| + PowerfulPurse$Proxy promote(Purse$Proxy purse);
|
| +}
|
| +
|
| +class Mint$ProxyImpl extends Proxy implements Mint$Proxy {
|
| + Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { }
|
| + Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])) { }
|
| + factory Mint$ProxyImpl.createIsolate() {
|
| + Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate());
|
| + return new Mint$ProxyImpl.forIsolate(isolate);
|
| + }
|
| + factory Mint$ProxyImpl.localProxy(Mint obj) {
|
| + return new Mint$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(new Mint$Dispatcher(obj))));
|
| + }
|
| +
|
| + Purse$Proxy createPurse(int balance) {
|
| + return new Purse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["createPurse", balance])));
|
| + }
|
| +
|
| + PowerfulPurse$Proxy promote(Purse$Proxy purse) {
|
| + return new PowerfulPurse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["promote", purse])));
|
| + }
|
| +}
|
| +
|
| +class Mint$Dispatcher extends Dispatcher<Mint> {
|
| + Mint$Dispatcher(Mint thing) : super(thing) { }
|
| +
|
| + void process(var message, void reply(var response)) {
|
| + String command = message[0];
|
| + if (command == "Mint") {
|
| + } else if (command == "createPurse") {
|
| + int balance = message[1];
|
| + Purse$Proxy createPurse = target.createPurse(balance);
|
| + reply(createPurse);
|
| + } else if (command == "promote") {
|
| + List<Promise<SendPort>> promises = new List<Promise<SendPort>>();
|
| + promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(message[1])));
|
| + Promise done = new Promise();
|
| + done.waitFor(promises, 1);
|
| + done.addCompleteHandler((_) {
|
| + Purse$Proxy purse = new Purse$ProxyImpl(promises[0]);
|
| + PowerfulPurse$Proxy promote = target.promote(purse);
|
| + reply(promote);
|
| + });
|
| + } else {
|
| + // TODO(kasperl,benl): Somehow throw an exception instead.
|
| + reply("Exception: command '" + command + "' not understood by Mint.");
|
| + }
|
| + }
|
| +}
|
| +
|
| +class Mint$Dispatcher$Isolate extends Isolate {
|
| + Mint$Dispatcher$Isolate() : super() { }
|
| +
|
| + void main() {
|
| + this.port.receive(void _(var message, SendPort replyTo) {
|
| + Mint thing = new Mint();
|
| + SendPort port = Dispatcher.serve(new Mint$Dispatcher(thing));
|
| + Proxy proxy = new Proxy.forPort(replyTo);
|
| + proxy.send([port]);
|
| + });
|
| + }
|
| +}
|
| +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +// IsolateStubs=MintMakerFullyIsolatedTest.dart:Mint,Purse,PowerfulPurse
|
| +
|
| +//#library("MintMakerFullyIsolatedTest");
|
| +//#import("../../isolate/src/TestFramework.dart");
|
| +
|
| +interface Purse {
|
| + Purse();
|
| + int queryBalance();
|
| + Purse$Proxy sproutPurse();
|
| + // The deposit has not completed until the promise completes. If we
|
| + // supported Promise<void> then this could use that.
|
| + Promise<int> deposit(int amount, Purse$Proxy source);
|
| +}
|
| +
|
| +interface PowerfulPurse extends Purse factory PurseImpl {
|
| + PowerfulPurse();
|
| +
|
| + void init(Mint$Proxy mint, int balance);
|
| + // Return an int so we can wait for it to complete. Shame we can't
|
| + // have a Promise<void>.
|
| + int grab(int amount);
|
| + Purse weak();
|
| +}
|
| +
|
| +interface Mint factory MintImpl {
|
| + Mint();
|
| +
|
| + Purse$Proxy createPurse(int balance);
|
| + PowerfulPurse$Proxy promote(Purse$Proxy purse);
|
| +}
|
| +
|
| +// Because promises can't be used as keys in maps until they have
|
| +// completed, provide a wrapper. Note that if any key promise fails to
|
| +// resolve, then get()'s return may also fail to resolve. Also,
|
| +// although the logic is fine, this can't be used for a
|
| +// ProxyMap. Perhaps both Proxy and Promise should inherit from
|
| +// Completable?
|
| +// NB: not tested and known to be buggy. Will fix in a future change.
|
| +class PromiseMap<S extends Promise, T> {
|
| +
|
| + PromiseMap() {
|
| + _map = new Map<S, T>();
|
| + _incomplete = new Set<S>();
|
| + }
|
| +
|
| + T add(S s, T t) {
|
| + _incomplete.add(s);
|
| + s.addCompleteHandler((_) {
|
| + _map[s] = t;
|
| + _incomplete.remove(s);
|
| + });
|
| + return t;
|
| + }
|
| +
|
| + Promise<T> find(S s) {
|
| + T t = _map[s];
|
| + if (t != null)
|
| + return new Promise<T>.fromValue(t);
|
| + Promise<T> p = new Promise<T>();
|
| + int counter = _incomplete.length;
|
| + p.join(_incomplete, bool (S completed) {
|
| + if (completed != s) {
|
| + if (--counter == 0) {
|
| + p.complete(null);
|
| + return true;
|
| + }
|
| + return false;
|
| + }
|
| + p.complete(_map[s]);
|
| + return true;
|
| + });
|
| + return p;
|
| + }
|
| +
|
| + Set<S> _incomplete;
|
| + Map<S, T> _map;
|
| +
|
| +}
|
| +
|
| +class MintImpl implements Mint {
|
| +
|
| + MintImpl() {
|
| + //print('mint');
|
| + if (_power == null)
|
| + _power = new Map<Purse$Proxy, PowerfulPurse$Proxy>();
|
| + }
|
| +
|
| + Purse$Proxy createPurse(int balance) {
|
| + //print('createPurse');
|
| + PowerfulPurse$ProxyImpl purse =
|
| + new PowerfulPurse$ProxyImpl.createIsolate();
|
| + Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this);
|
| + purse.init(thisProxy, balance);
|
| +
|
| + Purse$Proxy weakPurse = purse.weak();
|
| + weakPurse.addCompleteHandler(() {
|
| + //print('cP1');
|
| + _power[weakPurse] = purse;
|
| + //print('cP2');
|
| + });
|
| + return weakPurse;
|
| + }
|
| +
|
| + PowerfulPurse$Proxy promote(Purse$Proxy purse) {
|
| + // FIXME(benl): we should be using a PromiseMap here. But we get
|
| + // away with it in this test for now.
|
| + //print('promote $purse/${_power[purse]}');
|
| + return _power[purse];
|
| + }
|
| +
|
| + static Map<Purse$Proxy, PowerfulPurse$Proxy> _power;
|
| +}
|
| +
|
| +class PurseImpl implements PowerfulPurse {
|
| +
|
| + // FIXME(benl): autogenerate constructor, get rid of init(...).
|
| + // Note that this constructor should not exist in the public interface
|
| + // PurseImpl(this._mint, this._balance) { }
|
| + PurseImpl() { }
|
| +
|
| + init(Mint$Proxy mint, int balance) {
|
| + this._mint = mint;
|
| + this._balance = balance;
|
| + }
|
| +
|
| + int queryBalance() {
|
| + return _balance;
|
| + }
|
| +
|
| + Purse$Proxy sproutPurse() {
|
| + //print('sprout');
|
| + return _mint.createPurse(0);
|
| + }
|
| +
|
| + Promise<int> deposit(int amount, Purse$Proxy proxy) {
|
| + //print('deposit');
|
| + Promise<int> grabbed = _mint.promote(proxy).grab(amount);
|
| + Promise<int> done = new Promise<int>();
|
| + grabbed.then((int) {
|
| + //print("deposit done");
|
| + _balance += amount;
|
| + done.complete(_balance);
|
| + });
|
| + return done;
|
| + }
|
| +
|
| + int grab(int amount) {
|
| + //print("grab");
|
| + if (_balance < amount) throw "Not enough dough.";
|
| + _balance -= amount;
|
| + return amount;
|
| + }
|
| +
|
| + Purse weak() {
|
| + return this;
|
| + }
|
| +
|
| + Mint$Proxy _mint;
|
| + int _balance;
|
| +
|
| +}
|
| +
|
| +class MintMakerFullyIsolatedTest {
|
| +
|
| + static void testMain() {
|
| + Mint$Proxy mint = new Mint$ProxyImpl.createIsolate();
|
| + Purse$Proxy purse = mint.createPurse(100);
|
| + // FIXME(benl): how do I write this?
|
| + //PowerfulPurse$Proxy power = (PowerfulPurse$Proxy)purse;
|
| + //expectEqualsStr("xxx", power.grab());
|
| + expectEquals(100, purse.queryBalance());
|
| +
|
| + Purse$Proxy sprouted = purse.sproutPurse();
|
| + expectEquals(0, sprouted.queryBalance());
|
| +
|
| + Promise<int> done = sprouted.deposit(5, purse);
|
| + // FIXME(benl): it should not be necessary to wait here, I think,
|
| + // but without this, the tests seem to execute prematurely.
|
| + expectEquals(5, done);
|
| + done.then((int) {
|
| + expectEquals(0 + 5, sprouted.queryBalance());
|
| + expectEquals(100 - 5, purse.queryBalance());
|
| +
|
| + done = sprouted.deposit(42, purse);
|
| + expectEquals(5 + 42, done);
|
| + done.then((int) {
|
| + expectEquals(0 + 5 + 42, sprouted.queryBalance());
|
| + expectEquals(100 - 5 - 42, purse.queryBalance());
|
| +
|
| + expectDone(8);
|
| + });
|
| + });
|
| + }
|
| +
|
| + static List<Promise> results;
|
| +
|
| + static void expectEqualsStr(String expected, Promise<String> promise) {
|
| + if (results === null) {
|
| + results = new List<Promise>();
|
| + }
|
| + results.add(promise.then((String actual) {
|
| + //print('done ' + expected + '/' + actual);
|
| + Expect.equals(expected, actual);
|
| + }));
|
| + }
|
| +
|
| + static void expectEquals(int expected, Promise<int> promise) {
|
| + if (results === null) {
|
| + results = new List<Promise>();
|
| + }
|
| + results.add(promise.then((int actual) {
|
| + //print('done ' + expected + '/' + actual);
|
| + Expect.equals(expected, actual);
|
| + }));
|
| + }
|
| +
|
| + static void expectDone(int n) {
|
| + if (results === null) {
|
| + Expect.equals(0, n);
|
| + print('##DONE##');
|
| + } else {
|
| + Promise done = new Promise();
|
| + done.waitFor(results, results.length);
|
| + done.then((ignored) {
|
| + //print('done all ' + n + '/' + results.length);
|
| + Expect.equals(n, results.length);
|
| + print('##DONE##');
|
| + });
|
| + }
|
| + }
|
| +
|
| +}
|
| +
|
| +main() {
|
| + MintMakerFullyIsolatedTest.testMain();
|
| + //runTests([MintMakerFullyIsolatedTest.testMain]);
|
| +}
|
|
|