| 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 // IsolateStubs=MintMakerPromiseWithStubsTest.dart:Mint,Purse | |
| 6 | |
| 7 #library("MintMakerPromiseWithStubsTest-generatedTest"); | |
| 8 #import("../../../tests/isolate/src/TestFramework.dart"); | |
| 9 | |
| 10 interface Mint default MintImpl { | |
| 11 | |
| 12 Mint(); | |
| 13 | |
| 14 Purse createPurse(int balance); | |
| 15 | |
| 16 } | |
| 17 | |
| 18 interface Purse default PurseImpl { | |
| 19 | |
| 20 Purse(); | |
| 21 | |
| 22 int queryBalance(); | |
| 23 Purse sproutPurse(); | |
| 24 Promise<int> deposit(int amount, Purse$Proxy source); | |
| 25 | |
| 26 } | |
| 27 | |
| 28 class MintImpl implements Mint { | |
| 29 | |
| 30 MintImpl() { } | |
| 31 | |
| 32 Purse createPurse(int balance) { | |
| 33 PurseImpl purse = new PurseImpl(); | |
| 34 purse.init(this, balance); | |
| 35 | |
| 36 return purse; | |
| 37 } | |
| 38 | |
| 39 } | |
| 40 | |
| 41 class PurseImpl implements Purse { | |
| 42 | |
| 43 PurseImpl() { } | |
| 44 // TODO(benl): implement stub constructors. | |
| 45 // Note that this constructor should _not_ be in the Purse interface, | |
| 46 // only this isolate is trusted to construct purses. | |
| 47 //PurseImpl(this._mint, this._balance) { } | |
| 48 void init(Mint mint, int balance) { | |
| 49 this._mint = mint; | |
| 50 this._balance = balance; | |
| 51 } | |
| 52 | |
| 53 int queryBalance() { | |
| 54 return _balance; | |
| 55 } | |
| 56 | |
| 57 Purse sproutPurse() { | |
| 58 return _mint.createPurse(0); | |
| 59 } | |
| 60 | |
| 61 Promise<int> deposit(int amount, Purse$Proxy proxy) { | |
| 62 if (amount < 0) throw "Ha ha"; | |
| 63 // Because we are in the same isolate as the other purse, we can | |
| 64 // retrieve the proxy's local PurseImpl object and act on it | |
| 65 // directly. Further, a forged purse will not be convertible, and | |
| 66 // so an attempt to use it will fail. | |
| 67 Promise<int> balance = new Promise<int>(); | |
| 68 proxy.addCompleteHandler((_) { | |
| 69 PurseImpl source = proxy.dynamic.local; | |
| 70 if (source._balance < amount) throw "Not enough dough."; | |
| 71 _balance += amount; | |
| 72 source._balance -= amount; | |
| 73 balance.complete(_balance); | |
| 74 }); | |
| 75 return balance; | |
| 76 } | |
| 77 | |
| 78 Mint _mint; | |
| 79 int _balance; | |
| 80 | |
| 81 } | |
| 82 | |
| 83 class MintMakerPromiseWithStubsTest { | |
| 84 | |
| 85 static void testMain(TestExpectation expect) { | |
| 86 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); | |
| 87 Purse$Proxy purse = mint.createPurse(100); | |
| 88 expect.completesWithValue(purse.queryBalance(), 100); | |
| 89 | |
| 90 Purse$Proxy sprouted = purse.sproutPurse(); | |
| 91 expect.completesWithValue(sprouted.queryBalance(), 0); | |
| 92 | |
| 93 // FIXME(benl): We should not have to manually order the calls | |
| 94 // like this. | |
| 95 Promise<int> result = sprouted.deposit(5, purse); | |
| 96 Promise p1 = expect.completesWithValue(result, 5); | |
| 97 Promise<bool> p2 = new Promise<bool>(); | |
| 98 Promise<bool> p3 = new Promise<bool>(); | |
| 99 Promise<bool> p4 = new Promise<bool>(); | |
| 100 Promise<bool> p5 = new Promise<bool>(); | |
| 101 Promise<bool> p6 = new Promise<bool>(); | |
| 102 result.addCompleteHandler((unused) { | |
| 103 expect.completesWithValue(sprouted.queryBalance(), 0 + 5) | |
| 104 .then((unused_) => p2.complete(true)); | |
| 105 expect.completesWithValue(purse.queryBalance(), 100 - 5) | |
| 106 .then((unused_) => p3.complete(true)); | |
| 107 | |
| 108 result = sprouted.deposit(42, purse); | |
| 109 expect.completesWithValue(result, 5 + 42).then((unused__) => p4.complete(t
rue)); | |
| 110 result.addCompleteHandler((unused_) { | |
| 111 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) | |
| 112 .then((unused___) => p5.complete(true)); | |
| 113 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) | |
| 114 .then((unused___) => p6.complete(true)); | |
| 115 }); | |
| 116 }); | |
| 117 Promise<bool> done = new Promise<bool>(); | |
| 118 done.waitFor([p1, p2, p3, p4, p5, p6], 6); | |
| 119 done.then((_) { | |
| 120 expect.succeeded(); | |
| 121 print("##DONE##"); | |
| 122 }); | |
| 123 } | |
| 124 | |
| 125 } | |
| 126 | |
| 127 main() { | |
| 128 runTests([MintMakerPromiseWithStubsTest.testMain]); | |
| 129 } | |
| OLD | NEW |