| 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 /* class = Mint (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Min
tMakerPromiseWithStubsTest.dart: 10) */ | |
| 11 | |
| 12 interface Mint$Proxy extends Proxy { | |
| 13 Purse$Proxy createPurse(int balance); | |
| 14 } | |
| 15 | |
| 16 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy { | |
| 17 Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | |
| 18 Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])
) { } | |
| 19 factory Mint$ProxyImpl.createIsolate() { | |
| 20 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); | |
| 21 return new Mint$ProxyImpl.forIsolate(isolate); | |
| 22 } | |
| 23 factory Mint$ProxyImpl.localProxy(Mint obj) { | |
| 24 return new Mint$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(n
ew Mint$Dispatcher(obj)))); | |
| 25 } | |
| 26 | |
| 27 Purse$Proxy createPurse(int balance) { | |
| 28 return new Purse$ProxyImpl(this.call(["createPurse", balance])); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 class Mint$Dispatcher extends Dispatcher<Mint> { | |
| 33 Mint$Dispatcher(Mint thing) : super(thing) { } | |
| 34 | |
| 35 void process(var message, void reply(var response)) { | |
| 36 String command = message[0]; | |
| 37 if (command == "Mint") { | |
| 38 } else if (command == "createPurse") { | |
| 39 int balance = message[1]; | |
| 40 Purse createPurse = target.createPurse(balance); | |
| 41 SendPort port = Dispatcher.serve(new Purse$Dispatcher(createPurse)); | |
| 42 reply(port); | |
| 43 } else { | |
| 44 // TODO(kasperl,benl): Somehow throw an exception instead. | |
| 45 reply("Exception: command '" + command + "' not understood by Mint."); | |
| 46 } | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 class Mint$Dispatcher$Isolate extends Isolate { | |
| 51 Mint$Dispatcher$Isolate() : super() { } | |
| 52 | |
| 53 void main() { | |
| 54 this.port.receive(void _(var message, SendPort replyTo) { | |
| 55 Mint thing = new Mint(); | |
| 56 SendPort port = Dispatcher.serve(new Mint$Dispatcher(thing)); | |
| 57 Proxy proxy = new Proxy.forPort(replyTo); | |
| 58 proxy.send([port]); | |
| 59 }); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 /* class = Purse (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Mi
ntMakerPromiseWithStubsTest.dart: 18) */ | |
| 64 | |
| 65 interface Purse$Proxy extends Proxy { | |
| 66 Promise<int> queryBalance(); | |
| 67 | |
| 68 Purse$Proxy sproutPurse(); | |
| 69 | |
| 70 Promise<int> deposit(int amount, Purse$Proxy source); | |
| 71 } | |
| 72 | |
| 73 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy { | |
| 74 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | |
| 75 Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null]
)) { } | |
| 76 factory Purse$ProxyImpl.createIsolate() { | |
| 77 Proxy isolate = new Proxy.forIsolate(new Purse$Dispatcher$Isolate()); | |
| 78 return new Purse$ProxyImpl.forIsolate(isolate); | |
| 79 } | |
| 80 factory Purse$ProxyImpl.localProxy(Purse obj) { | |
| 81 return new Purse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(
new Purse$Dispatcher(obj)))); | |
| 82 } | |
| 83 | |
| 84 Promise<int> queryBalance() { | |
| 85 return this.call(["queryBalance"]); | |
| 86 } | |
| 87 | |
| 88 Purse$Proxy sproutPurse() { | |
| 89 return new Purse$ProxyImpl(this.call(["sproutPurse"])); | |
| 90 } | |
| 91 | |
| 92 Promise<int> deposit(int amount, Purse$Proxy source) { | |
| 93 return new PromiseProxy<int>(this.call(["deposit", amount, source])); | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 class Purse$Dispatcher extends Dispatcher<Purse> { | |
| 98 Purse$Dispatcher(Purse thing) : super(thing) { } | |
| 99 | |
| 100 void process(var message, void reply(var response)) { | |
| 101 String command = message[0]; | |
| 102 if (command == "Purse") { | |
| 103 } else if (command == "queryBalance") { | |
| 104 int queryBalance = target.queryBalance(); | |
| 105 reply(queryBalance); | |
| 106 } else if (command == "sproutPurse") { | |
| 107 Purse sproutPurse = target.sproutPurse(); | |
| 108 SendPort port = Dispatcher.serve(new Purse$Dispatcher(sproutPurse)); | |
| 109 reply(port); | |
| 110 } else if (command == "deposit") { | |
| 111 int amount = message[1]; | |
| 112 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | |
| 113 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[2]))); | |
| 114 Purse$Proxy source = new Purse$ProxyImpl(promises[0]); | |
| 115 Promise<int> deposit = target.deposit(amount, source); | |
| 116 reply(deposit); | |
| 117 } else { | |
| 118 // TODO(kasperl,benl): Somehow throw an exception instead. | |
| 119 reply("Exception: command '" + command + "' not understood by Purse."); | |
| 120 } | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 class Purse$Dispatcher$Isolate extends Isolate { | |
| 125 Purse$Dispatcher$Isolate() : super() { } | |
| 126 | |
| 127 void main() { | |
| 128 this.port.receive(void _(var message, SendPort replyTo) { | |
| 129 Purse thing = new Purse(); | |
| 130 SendPort port = Dispatcher.serve(new Purse$Dispatcher(thing)); | |
| 131 Proxy proxy = new Proxy.forPort(replyTo); | |
| 132 proxy.send([port]); | |
| 133 }); | |
| 134 } | |
| 135 } | |
| 136 interface Mint default MintImpl { | |
| 137 | |
| 138 Mint(); | |
| 139 | |
| 140 Purse createPurse(int balance); | |
| 141 | |
| 142 } | |
| 143 | |
| 144 interface Purse default PurseImpl { | |
| 145 | |
| 146 Purse(); | |
| 147 | |
| 148 int queryBalance(); | |
| 149 Purse sproutPurse(); | |
| 150 Promise<int> deposit(int amount, Purse$Proxy source); | |
| 151 | |
| 152 } | |
| 153 | |
| 154 class MintImpl implements Mint { | |
| 155 | |
| 156 MintImpl() { } | |
| 157 | |
| 158 Purse createPurse(int balance) { | |
| 159 PurseImpl purse = new PurseImpl(); | |
| 160 purse.init(this, balance); | |
| 161 | |
| 162 return purse; | |
| 163 } | |
| 164 | |
| 165 } | |
| 166 | |
| 167 class PurseImpl implements Purse { | |
| 168 | |
| 169 PurseImpl() { } | |
| 170 // TODO(benl): implement stub constructors. | |
| 171 // Note that this constructor should _not_ be in the Purse interface, | |
| 172 // only this isolate is trusted to construct purses. | |
| 173 //PurseImpl(this._mint, this._balance) { } | |
| 174 void init(Mint mint, int balance) { | |
| 175 this._mint = mint; | |
| 176 this._balance = balance; | |
| 177 } | |
| 178 | |
| 179 int queryBalance() { | |
| 180 return _balance; | |
| 181 } | |
| 182 | |
| 183 Purse sproutPurse() { | |
| 184 return _mint.createPurse(0); | |
| 185 } | |
| 186 | |
| 187 Promise<int> deposit(int amount, Purse$Proxy proxy) { | |
| 188 if (amount < 0) throw "Ha ha"; | |
| 189 // Because we are in the same isolate as the other purse, we can | |
| 190 // retrieve the proxy's local PurseImpl object and act on it | |
| 191 // directly. Further, a forged purse will not be convertible, and | |
| 192 // so an attempt to use it will fail. | |
| 193 Promise<int> balance = new Promise<int>(); | |
| 194 proxy.addCompleteHandler((_) { | |
| 195 PurseImpl source = proxy.dynamic.local; | |
| 196 if (source._balance < amount) throw "Not enough dough."; | |
| 197 _balance += amount; | |
| 198 source._balance -= amount; | |
| 199 balance.complete(_balance); | |
| 200 }); | |
| 201 return balance; | |
| 202 } | |
| 203 | |
| 204 Mint _mint; | |
| 205 int _balance; | |
| 206 | |
| 207 } | |
| 208 | |
| 209 class MintMakerPromiseWithStubsTest { | |
| 210 | |
| 211 static void testMain(TestExpectation expect) { | |
| 212 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); | |
| 213 Purse$Proxy purse = mint.createPurse(100); | |
| 214 expect.completesWithValue(purse.queryBalance(), 100); | |
| 215 | |
| 216 Purse$Proxy sprouted = purse.sproutPurse(); | |
| 217 expect.completesWithValue(sprouted.queryBalance(), 0); | |
| 218 | |
| 219 // FIXME(benl): We should not have to manually order the calls | |
| 220 // like this. | |
| 221 Promise<int> result = sprouted.deposit(5, purse); | |
| 222 Promise p1 = expect.completesWithValue(result, 5); | |
| 223 Promise<bool> p2 = new Promise<bool>(); | |
| 224 Promise<bool> p3 = new Promise<bool>(); | |
| 225 Promise<bool> p4 = new Promise<bool>(); | |
| 226 Promise<bool> p5 = new Promise<bool>(); | |
| 227 Promise<bool> p6 = new Promise<bool>(); | |
| 228 result.addCompleteHandler((unused) { | |
| 229 expect.completesWithValue(sprouted.queryBalance(), 0 + 5) | |
| 230 .then((unused_) => p2.complete(true)); | |
| 231 expect.completesWithValue(purse.queryBalance(), 100 - 5) | |
| 232 .then((unused_) => p3.complete(true)); | |
| 233 | |
| 234 result = sprouted.deposit(42, purse); | |
| 235 expect.completesWithValue(result, 5 + 42).then((unused__) => p4.complete(t
rue)); | |
| 236 result.addCompleteHandler((unused_) { | |
| 237 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) | |
| 238 .then((unused___) => p5.complete(true)); | |
| 239 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) | |
| 240 .then((unused___) => p6.complete(true)); | |
| 241 }); | |
| 242 }); | |
| 243 Promise<bool> done = new Promise<bool>(); | |
| 244 done.waitFor([p1, p2, p3, p4, p5, p6], 6); | |
| 245 done.then((_) { | |
| 246 expect.succeeded(); | |
| 247 print("##DONE##"); | |
| 248 }); | |
| 249 } | |
| 250 | |
| 251 } | |
| 252 | |
| 253 main() { | |
| 254 runTests([MintMakerPromiseWithStubsTest.testMain]); | |
| 255 } | |
| OLD | NEW |