| 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=MintMakerFullyIsolatedTest.dart:Mint,Purse,PowerfulPurse | |
| 6 #library("MintMakerFullyIsolatedTest-generatedTest"); | |
| 7 #import("../../../tests/isolate/src/TestFramework.dart"); | |
| 8 | |
| 9 /* class = Purse (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintM
akerFullyIsolatedTest.dart: 9) */ | |
| 10 | |
| 11 interface Purse$Proxy extends Proxy { | |
| 12 Promise<int> queryBalance(); | |
| 13 | |
| 14 Purse$Proxy sproutPurse(); | |
| 15 | |
| 16 Promise<int> deposit(int amount, Purse$Proxy source); | |
| 17 } | |
| 18 | |
| 19 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy { | |
| 20 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | |
| 21 Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null]
)) { } | |
| 22 factory Purse$ProxyImpl.createIsolate() { | |
| 23 Proxy isolate = new Proxy.forIsolate(new Purse$Dispatcher$Isolate()); | |
| 24 return new Purse$ProxyImpl.forIsolate(isolate); | |
| 25 } | |
| 26 factory Purse$ProxyImpl.localProxy(Purse obj) { | |
| 27 return new Purse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(
new Purse$Dispatcher(obj)))); | |
| 28 } | |
| 29 | |
| 30 Promise<int> queryBalance() { | |
| 31 return this.call(["queryBalance"]); | |
| 32 } | |
| 33 | |
| 34 Purse$Proxy sproutPurse() { | |
| 35 return new Purse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["sproutPurs
e"]))); | |
| 36 } | |
| 37 | |
| 38 Promise<int> deposit(int amount, Purse$Proxy source) { | |
| 39 return new PromiseProxy<int>(this.call(["deposit", amount, source])); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 class Purse$Dispatcher extends Dispatcher<Purse> { | |
| 44 Purse$Dispatcher(Purse thing) : super(thing) { } | |
| 45 | |
| 46 void process(var message, void reply(var response)) { | |
| 47 String command = message[0]; | |
| 48 if (command == "Purse") { | |
| 49 } else if (command == "queryBalance") { | |
| 50 int queryBalance = target.queryBalance(); | |
| 51 reply(queryBalance); | |
| 52 } else if (command == "sproutPurse") { | |
| 53 Purse$Proxy sproutPurse = target.sproutPurse(); | |
| 54 reply(sproutPurse); | |
| 55 } else if (command == "deposit") { | |
| 56 int amount = message[1]; | |
| 57 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | |
| 58 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[2]))); | |
| 59 Purse$Proxy source = new Purse$ProxyImpl(promises[0]); | |
| 60 Promise<int> deposit = target.deposit(amount, source); | |
| 61 reply(deposit); | |
| 62 } else { | |
| 63 // TODO(kasperl,benl): Somehow throw an exception instead. | |
| 64 reply("Exception: command '" + command + "' not understood by Purse."); | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 class Purse$Dispatcher$Isolate extends Isolate { | |
| 70 Purse$Dispatcher$Isolate() : super() { } | |
| 71 | |
| 72 void main() { | |
| 73 this.port.receive(void _(var message, SendPort replyTo) { | |
| 74 Purse thing = new Purse(); | |
| 75 SendPort port = Dispatcher.serve(new Purse$Dispatcher(thing)); | |
| 76 Proxy proxy = new Proxy.forPort(replyTo); | |
| 77 proxy.send([port]); | |
| 78 }); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 /* class = PowerfulPurse (tests/stub-generator/src/MintMakerFullyIsolatedTest.da
rt/MintMakerFullyIsolatedTest.dart: 18) */ | |
| 83 | |
| 84 interface PowerfulPurse$Proxy extends Proxy { | |
| 85 void init(Mint$Proxy mint, int balance); | |
| 86 | |
| 87 Promise<int> grab(int amount); | |
| 88 | |
| 89 Purse$Proxy weak(); | |
| 90 } | |
| 91 | |
| 92 class PowerfulPurse$ProxyImpl extends ProxyImpl implements PowerfulPurse$Proxy { | |
| 93 PowerfulPurse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | |
| 94 PowerfulPurse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.cal
l([null])) { } | |
| 95 factory PowerfulPurse$ProxyImpl.createIsolate() { | |
| 96 Proxy isolate = new Proxy.forIsolate(new PowerfulPurse$Dispatcher$Isolate())
; | |
| 97 return new PowerfulPurse$ProxyImpl.forIsolate(isolate); | |
| 98 } | |
| 99 factory PowerfulPurse$ProxyImpl.localProxy(PowerfulPurse obj) { | |
| 100 return new PowerfulPurse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatche
r.serve(new PowerfulPurse$Dispatcher(obj)))); | |
| 101 } | |
| 102 | |
| 103 void init(Mint$Proxy mint, int balance) { | |
| 104 this.send(["init", mint, balance]); | |
| 105 } | |
| 106 | |
| 107 Promise<int> grab(int amount) { | |
| 108 return this.call(["grab", amount]); | |
| 109 } | |
| 110 | |
| 111 Purse$Proxy weak() { | |
| 112 return new Purse$ProxyImpl(this.call(["weak"])); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 class PowerfulPurse$Dispatcher extends Dispatcher<PowerfulPurse> { | |
| 117 PowerfulPurse$Dispatcher(PowerfulPurse thing) : super(thing) { } | |
| 118 | |
| 119 void process(var message, void reply(var response)) { | |
| 120 String command = message[0]; | |
| 121 if (command == "PowerfulPurse") { | |
| 122 } else if (command == "init") { | |
| 123 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | |
| 124 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[1]))); | |
| 125 int balance = message[2]; | |
| 126 Mint$Proxy mint = new Mint$ProxyImpl(promises[0]); | |
| 127 target.init(mint, balance); | |
| 128 } else if (command == "grab") { | |
| 129 int amount = message[1]; | |
| 130 int grab = target.grab(amount); | |
| 131 reply(grab); | |
| 132 } else if (command == "weak") { | |
| 133 Purse weak = target.weak(); | |
| 134 SendPort port = Dispatcher.serve(new Purse$Dispatcher(weak)); | |
| 135 reply(port); | |
| 136 } else { | |
| 137 // TODO(kasperl,benl): Somehow throw an exception instead. | |
| 138 reply("Exception: command '" + command + "' not understood by PowerfulPurs
e."); | |
| 139 } | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 class PowerfulPurse$Dispatcher$Isolate extends Isolate { | |
| 144 PowerfulPurse$Dispatcher$Isolate() : super() { } | |
| 145 | |
| 146 void main() { | |
| 147 this.port.receive(void _(var message, SendPort replyTo) { | |
| 148 PowerfulPurse thing = new PowerfulPurse(); | |
| 149 SendPort port = Dispatcher.serve(new PowerfulPurse$Dispatcher(thing)); | |
| 150 Proxy proxy = new Proxy.forPort(replyTo); | |
| 151 proxy.send([port]); | |
| 152 }); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 /* class = Mint (tests/stub-generator/src/MintMakerFullyIsolatedTest.dart/MintMa
kerFullyIsolatedTest.dart: 28) */ | |
| 157 | |
| 158 interface Mint$Proxy extends Proxy { | |
| 159 Purse$Proxy createPurse(int balance); | |
| 160 | |
| 161 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse); | |
| 162 } | |
| 163 | |
| 164 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy { | |
| 165 Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | |
| 166 Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])
) { } | |
| 167 factory Mint$ProxyImpl.createIsolate() { | |
| 168 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); | |
| 169 return new Mint$ProxyImpl.forIsolate(isolate); | |
| 170 } | |
| 171 factory Mint$ProxyImpl.localProxy(Mint obj) { | |
| 172 return new Mint$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(n
ew Mint$Dispatcher(obj)))); | |
| 173 } | |
| 174 | |
| 175 Purse$Proxy createPurse(int balance) { | |
| 176 return new Purse$ProxyImpl(new PromiseProxy<SendPort>(this.call(["createPurs
e", balance]))); | |
| 177 } | |
| 178 | |
| 179 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse) { | |
| 180 return new Promise<PowerfulPurse$Proxy>.fromValue(new PowerfulPurse$ProxyImp
l(new PromiseProxy<SendPort>(new PromiseProxy<SendPort>(this.call(["promote", pu
rse]))))); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 class Mint$Dispatcher extends Dispatcher<Mint> { | |
| 185 Mint$Dispatcher(Mint thing) : super(thing) { } | |
| 186 | |
| 187 void process(var message, void reply(var response)) { | |
| 188 String command = message[0]; | |
| 189 if (command == "Mint") { | |
| 190 } else if (command == "createPurse") { | |
| 191 int balance = message[1]; | |
| 192 Purse$Proxy createPurse = target.createPurse(balance); | |
| 193 reply(createPurse); | |
| 194 } else if (command == "promote") { | |
| 195 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | |
| 196 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[1]))); | |
| 197 Purse$Proxy purse = new Purse$ProxyImpl(promises[0]); | |
| 198 Promise<PowerfulPurse$Proxy> promote = target.promote(purse); | |
| 199 reply(promote); | |
| 200 } else { | |
| 201 // TODO(kasperl,benl): Somehow throw an exception instead. | |
| 202 reply("Exception: command '" + command + "' not understood by Mint."); | |
| 203 } | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 class Mint$Dispatcher$Isolate extends Isolate { | |
| 208 Mint$Dispatcher$Isolate() : super() { } | |
| 209 | |
| 210 void main() { | |
| 211 this.port.receive(void _(var message, SendPort replyTo) { | |
| 212 Mint thing = new Mint(); | |
| 213 SendPort port = Dispatcher.serve(new Mint$Dispatcher(thing)); | |
| 214 Proxy proxy = new Proxy.forPort(replyTo); | |
| 215 proxy.send([port]); | |
| 216 }); | |
| 217 } | |
| 218 } | |
| 219 interface Purse default PurseImpl{ | |
| 220 Purse(); | |
| 221 int queryBalance(); | |
| 222 Purse$Proxy sproutPurse(); | |
| 223 // The deposit has not completed until the promise completes. If we | |
| 224 // supported Promise<void> then this could use that. | |
| 225 Promise<int> deposit(int amount, Purse$Proxy source); | |
| 226 } | |
| 227 | |
| 228 interface PowerfulPurse extends Purse default PurseImpl { | |
| 229 PowerfulPurse(); | |
| 230 | |
| 231 void init(Mint$Proxy mint, int balance); | |
| 232 // Return an int so we can wait for it to complete. Shame we can't | |
| 233 // have a Promise<void>. | |
| 234 int grab(int amount); | |
| 235 Purse weak(); | |
| 236 } | |
| 237 | |
| 238 interface Mint default MintImpl { | |
| 239 Mint(); | |
| 240 | |
| 241 Purse$Proxy createPurse(int balance); | |
| 242 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse); | |
| 243 } | |
| 244 | |
| 245 // Because promises can't be used as keys in maps until they have | |
| 246 // completed, provide a wrapper. Note that if any key promise fails to | |
| 247 // resolve, then get()'s return may also fail to resolve. Right now, a | |
| 248 // Proxy can also be used since it has (kludgily) been made to inherit | |
| 249 // from Promise. Perhaps both Proxy and Promise should inherit from | |
| 250 // Completable? | |
| 251 // Note that we cannot extend Set rather than Collection because, for | |
| 252 // example, Set.remove() returns bool, whereas this will have to | |
| 253 // return Promise<bool>. | |
| 254 class PromiseSet<T extends Promise> implements Collection<T> { | |
| 255 | |
| 256 PromiseSet() { | |
| 257 _set = new List<T>(); | |
| 258 } | |
| 259 | |
| 260 PromiseSet.fromList(this._set); | |
| 261 | |
| 262 void add(T t) { | |
| 263 print("ProxySet.add"); | |
| 264 for (T x in _set) { | |
| 265 if (x === t) | |
| 266 return; | |
| 267 } | |
| 268 if (t.hasValue()) { | |
| 269 for (T x in _set) { | |
| 270 if (x.hasValue() && x == t) | |
| 271 return; | |
| 272 } | |
| 273 } | |
| 274 _set.add(t); | |
| 275 t.addCompleteHandler((_) { | |
| 276 // Remove any duplicates. | |
| 277 _remove(t, 1); | |
| 278 }); | |
| 279 } | |
| 280 | |
| 281 void _remove(T t, int threshold) { | |
| 282 print("PromiseSet.remove $threshold"); | |
| 283 int count = 0; | |
| 284 for (int n = 0; n < _set.length; ++n) | |
| 285 if (_set[n].hasValue() && _set[n] == t) | |
| 286 if (++count > threshold) { | |
| 287 print(" remove $n"); | |
| 288 _set.removeRange(n, 1); | |
| 289 --n; | |
| 290 } | |
| 291 } | |
| 292 | |
| 293 void remove(T t) { | |
| 294 t.addCompleteHandler((_) { | |
| 295 _remove(t, 0); | |
| 296 }); | |
| 297 } | |
| 298 | |
| 299 int get length() => _set.length; | |
| 300 void forEach(void f(T element)) { _set.forEach(f); } | |
| 301 PromiseSet<T> filter(bool f(T element)) { | |
| 302 return new PromiseSet<T>.fromList(_set.filter(f)); | |
| 303 } | |
| 304 bool every(bool f(T element)) => _set.every(f); | |
| 305 bool some(bool f(T element)) => _set.some(f); | |
| 306 bool isEmpty() => _set.isEmpty(); | |
| 307 Iterator<T> iterator() => _set.iterator(); | |
| 308 | |
| 309 List<T> _set; | |
| 310 | |
| 311 } | |
| 312 | |
| 313 | |
| 314 class PromiseMap<S extends Promise, T> { | |
| 315 | |
| 316 PromiseMap() { | |
| 317 _map = new Map<S, T>(); | |
| 318 _incomplete = new PromiseSet<S>(); | |
| 319 } | |
| 320 | |
| 321 T add(S s, T t) { | |
| 322 print("PromiseMap.add"); | |
| 323 _incomplete.add(s); | |
| 324 s.addCompleteHandler((_) { | |
| 325 print("PromiseMap.add move to map"); | |
| 326 _map[s] = t; | |
| 327 _incomplete.remove(s); | |
| 328 }); | |
| 329 return t; | |
| 330 } | |
| 331 | |
| 332 Promise<T> find(S s) { | |
| 333 print("PromiseMap.find"); | |
| 334 Promise<T> findResult = new Promise<T>(); | |
| 335 s.addCompleteHandler((unused) { | |
| 336 print("PromiseMap.find s completed"); | |
| 337 T t = _map[s]; | |
| 338 if (t != null) { | |
| 339 print(" immediate"); | |
| 340 findResult.complete(t); | |
| 341 return; | |
| 342 } | |
| 343 // Otherwise, we need to wait for map[s] to complete... | |
| 344 int counter = _incomplete.length; | |
| 345 if (counter == 0) { | |
| 346 print(" none incomplete"); | |
| 347 findResult.complete(null); | |
| 348 return; | |
| 349 } | |
| 350 findResult.join(_incomplete, bool _(S completed) { | |
| 351 if (completed != s) { | |
| 352 if (--counter == 0) { | |
| 353 print("PromiseMap.find failed"); | |
| 354 findResult.complete(null); | |
| 355 return true; | |
| 356 } | |
| 357 print("PromiseMap.find miss"); | |
| 358 return false; | |
| 359 } | |
| 360 print("PromiseMap.find complete"); | |
| 361 findResult.complete(_map[s]); | |
| 362 return true; | |
| 363 }); | |
| 364 }); | |
| 365 return findResult; | |
| 366 } | |
| 367 | |
| 368 PromiseSet<S> _incomplete; | |
| 369 Map<S, T> _map; | |
| 370 | |
| 371 } | |
| 372 | |
| 373 | |
| 374 class MintImpl implements Mint { | |
| 375 | |
| 376 MintImpl() { | |
| 377 print('mint'); | |
| 378 if (_power == null) | |
| 379 _power = new PromiseMap<Purse$Proxy, PowerfulPurse$Proxy>(); | |
| 380 } | |
| 381 | |
| 382 Purse$Proxy createPurse(int balance) { | |
| 383 print('createPurse'); | |
| 384 PowerfulPurse$ProxyImpl purse = | |
| 385 new PowerfulPurse$ProxyImpl.createIsolate(); | |
| 386 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this); | |
| 387 purse.init(thisProxy, balance); | |
| 388 | |
| 389 Purse$Proxy weakPurse = purse.weak(); | |
| 390 weakPurse.addCompleteHandler((_) { | |
| 391 print('cP1'); | |
| 392 _power.add(weakPurse, purse); | |
| 393 print('cP2'); | |
| 394 }); | |
| 395 return weakPurse; | |
| 396 } | |
| 397 | |
| 398 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse) { | |
| 399 print('promote $purse'); | |
| 400 return _power.find(purse); | |
| 401 } | |
| 402 | |
| 403 static PromiseMap<Purse$Proxy, PowerfulPurse$Proxy> _power; | |
| 404 } | |
| 405 | |
| 406 class PurseImpl implements PowerfulPurse { | |
| 407 | |
| 408 // FIXME(benl): autogenerate constructor, get rid of init(...). | |
| 409 // Note that this constructor should not exist in the public interface | |
| 410 // PurseImpl(this._mint, this._balance) { } | |
| 411 PurseImpl() { } | |
| 412 | |
| 413 init(Mint$Proxy mint, int balance) { | |
| 414 this._mint = mint; | |
| 415 this._balance = balance; | |
| 416 } | |
| 417 | |
| 418 int queryBalance() { | |
| 419 return _balance; | |
| 420 } | |
| 421 | |
| 422 Purse$Proxy sproutPurse() { | |
| 423 print('sprout'); | |
| 424 return _mint.createPurse(0); | |
| 425 } | |
| 426 | |
| 427 Promise<int> deposit(int amount, Purse$Proxy proxy) { | |
| 428 print('deposit'); | |
| 429 Promise<PowerfulPurse$Proxy> powerful = _mint.promote(proxy); | |
| 430 | |
| 431 Promise<int> result = new Promise<int>(); | |
| 432 powerful.then((_) { | |
| 433 Promise<int> grabbed = powerful.value.grab(amount); | |
| 434 grabbed.then((int grabbedAmount) { | |
| 435 _balance += grabbedAmount; | |
| 436 result.complete(_balance); | |
| 437 }); | |
| 438 }); | |
| 439 | |
| 440 return result; | |
| 441 } | |
| 442 | |
| 443 int grab(int amount) { | |
| 444 print("grab"); | |
| 445 if (_balance < amount) throw "Not enough dough."; | |
| 446 _balance -= amount; | |
| 447 return amount; | |
| 448 } | |
| 449 | |
| 450 Purse weak() { | |
| 451 return this; | |
| 452 } | |
| 453 | |
| 454 Mint$Proxy _mint; | |
| 455 int _balance; | |
| 456 | |
| 457 } | |
| 458 | |
| 459 class MintMakerFullyIsolatedTest { | |
| 460 | |
| 461 static void testMain(TestExpectation expect) { | |
| 462 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); | |
| 463 Purse$Proxy purse = mint.createPurse(100); | |
| 464 // FIXME(benl): how do I write this? | |
| 465 //PowerfulPurse$Proxy power = (PowerfulPurse$Proxy)purse; | |
| 466 //expectEqualsStr("xxx", power.grab()); | |
| 467 Promise<int> balance = purse.queryBalance(); | |
| 468 expect.completesWithValue(balance, 100); | |
| 469 | |
| 470 Purse$Proxy sprouted = purse.sproutPurse(); | |
| 471 expect.completesWithValue(sprouted.queryBalance(), 0); | |
| 472 | |
| 473 Promise<int> done = sprouted.deposit(5, purse); | |
| 474 Promise<int> d3 = expect.completesWithValue(done, 5); | |
| 475 Promise<int> inner = new Promise<int>(); | |
| 476 Promise<int> inner2 = new Promise<int>(); | |
| 477 // FIXME(benl): it should not be necessary to wait here, I think, | |
| 478 // but without this, the tests seem to execute prematurely. | |
| 479 Promise<int> d1 = done.then((val) { | |
| 480 expect.completesWithValue(sprouted.queryBalance(), 0 + 5); | |
| 481 expect.completesWithValue(purse.queryBalance(), 100 - 5); | |
| 482 | |
| 483 done = sprouted.deposit(42, purse); | |
| 484 expect.completesWithValue(done, 5 + 42); | |
| 485 Promise<int> d2 = done.then((val_) { | |
| 486 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) | |
| 487 .then((int value) => inner.complete(0)); | |
| 488 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) | |
| 489 .then((int value) => inner2.complete(0)); | |
| 490 }); | |
| 491 expect.completes(d2); | |
| 492 | |
| 493 return 0; | |
| 494 }); | |
| 495 expect.completesWithValue(d1, 0); | |
| 496 Promise<int> allDone = new Promise<int>(); | |
| 497 allDone.waitFor([d3, inner, inner2], 3); | |
| 498 allDone.then((_) { | |
| 499 expect.succeeded(); | |
| 500 print("##DONE##"); | |
| 501 }); | |
| 502 } | |
| 503 | |
| 504 } | |
| 505 | |
| 506 main() { | |
| 507 runTests([MintMakerFullyIsolatedTest.testMain]); | |
| 508 } | |
| OLD | NEW |