| 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 MintMakerTest; | 5 library MintMakerTest; |
| 6 |
| 6 import 'dart:async'; | 7 import 'dart:async'; |
| 7 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 8 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
| 9 import "remote_unittest_helper.dart"; | 10 import "remote_unittest_helper.dart"; |
| 10 | 11 |
| 11 class Mint { | 12 class Mint { |
| 12 Map<SendPort, Purse> _registry; | 13 Map<SendPort, Purse> _registry; |
| 13 SendPort port; | 14 SendPort port; |
| 14 | 15 |
| 15 Mint() : _registry = new Map<SendPort, Purse>() { | 16 Mint() : _registry = new Map<SendPort, Purse>() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 30 Purse purse = new Purse(this, balance); | 31 Purse purse = new Purse(this, balance); |
| 31 _registry[purse.port] = purse; | 32 _registry[purse.port] = purse; |
| 32 return purse; | 33 return purse; |
| 33 } | 34 } |
| 34 | 35 |
| 35 Purse lookupPurse(SendPort port) { | 36 Purse lookupPurse(SendPort port) { |
| 36 return _registry[port]; | 37 return _registry[port]; |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 | 40 |
| 40 | |
| 41 class MintWrapper { | 41 class MintWrapper { |
| 42 SendPort _mint; | 42 SendPort _mint; |
| 43 MintWrapper(SendPort this._mint) {} | 43 MintWrapper(SendPort this._mint) {} |
| 44 | 44 |
| 45 void createPurse(int balance, handlePurse(PurseWrapper purse)) { | 45 void createPurse(int balance, handlePurse(PurseWrapper purse)) { |
| 46 ReceivePort reply = new ReceivePort(); | 46 ReceivePort reply = new ReceivePort(); |
| 47 reply.first.then((SendPort purse) { | 47 reply.first.then((SendPort purse) { |
| 48 handlePurse(new PurseWrapper(purse)); | 48 handlePurse(new PurseWrapper(purse)); |
| 49 }); | 49 }); |
| 50 _mint.send([balance, reply.sendPort]); | 50 _mint.send([balance, reply.sendPort]); |
| 51 } | 51 } |
| 52 | |
| 53 } | 52 } |
| 54 | 53 |
| 55 class Purse { | 54 class Purse { |
| 56 Mint mint; | 55 Mint mint; |
| 57 int balance; | 56 int balance; |
| 58 SendPort port; | 57 SendPort port; |
| 59 | 58 |
| 60 Purse(this.mint, this.balance) { | 59 Purse(this.mint, this.balance) { |
| 61 ReceivePort recipient = new ReceivePort(); | 60 ReceivePort recipient = new ReceivePort(); |
| 62 port = recipient.sendPort; | 61 port = recipient.sendPort; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 76 SendPort replyTo = message.last; | 75 SendPort replyTo = message.last; |
| 77 Purse result = sproutPurse(); | 76 Purse result = sproutPurse(); |
| 78 replyTo.send(result.port); | 77 replyTo.send(result.port); |
| 79 } else { | 78 } else { |
| 80 // TODO: Send an exception back. | 79 // TODO: Send an exception back. |
| 81 throw UnsupportedError("Unsupported commend: $command"); | 80 throw UnsupportedError("Unsupported commend: $command"); |
| 82 } | 81 } |
| 83 }); | 82 }); |
| 84 } | 83 } |
| 85 | 84 |
| 86 int queryBalance() { return balance; } | 85 int queryBalance() { |
| 86 return balance; |
| 87 } |
| 87 | 88 |
| 88 Purse sproutPurse() { return mint.createPurse(0); } | 89 Purse sproutPurse() { |
| 90 return mint.createPurse(0); |
| 91 } |
| 89 | 92 |
| 90 void deposit(int amount, Purse source) { | 93 void deposit(int amount, Purse source) { |
| 91 // TODO: Throw an exception if the source purse doesn't hold | 94 // TODO: Throw an exception if the source purse doesn't hold |
| 92 // enough dough. | 95 // enough dough. |
| 93 balance += amount; | 96 balance += amount; |
| 94 source.balance -= amount; | 97 source.balance -= amount; |
| 95 } | 98 } |
| 96 } | 99 } |
| 97 | 100 |
| 98 | |
| 99 class PurseWrapper { | 101 class PurseWrapper { |
| 100 SendPort _purse; | 102 SendPort _purse; |
| 101 | 103 |
| 102 PurseWrapper(this._purse) {} | 104 PurseWrapper(this._purse) {} |
| 103 | 105 |
| 104 void _sendReceive(message, replyHandler(reply)) { | 106 void _sendReceive(message, replyHandler(reply)) { |
| 105 ReceivePort reply = new ReceivePort(); | 107 ReceivePort reply = new ReceivePort(); |
| 106 _purse.send([message, reply.sendPort]); | 108 _purse.send([message, reply.sendPort]); |
| 107 reply.first.then(replyHandler); | 109 reply.first.then(replyHandler); |
| 108 } | 110 } |
| 109 | 111 |
| 110 void queryBalance(handleBalance(int balance)) { | 112 void queryBalance(handleBalance(int balance)) { |
| 111 _sendReceive("balance", handleBalance); | 113 _sendReceive("balance", handleBalance); |
| 112 } | 114 } |
| 113 | 115 |
| 114 void sproutPurse(handleSprouted(PurseWrapper sprouted)) { | 116 void sproutPurse(handleSprouted(PurseWrapper sprouted)) { |
| 115 _sendReceive("sprout", (SendPort sprouted) { | 117 _sendReceive("sprout", (SendPort sprouted) { |
| 116 handleSprouted(new PurseWrapper(sprouted)); | 118 handleSprouted(new PurseWrapper(sprouted)); |
| 117 }); | 119 }); |
| 118 } | 120 } |
| 119 | 121 |
| 120 void deposit(PurseWrapper source, int amount) { | 122 void deposit(PurseWrapper source, int amount) { |
| 121 _purse.send([ "deposit", amount, source._purse ]); | 123 _purse.send(["deposit", amount, source._purse]); |
| 122 } | 124 } |
| 123 } | 125 } |
| 124 | 126 |
| 125 mintMakerWrapper(SendPort replyPort) { | 127 mintMakerWrapper(SendPort replyPort) { |
| 126 ReceivePort receiver = new ReceivePort(); | 128 ReceivePort receiver = new ReceivePort(); |
| 127 replyPort.send(receiver.sendPort); | 129 replyPort.send(receiver.sendPort); |
| 128 receiver.listen((SendPort replyTo) { | 130 receiver.listen((SendPort replyTo) { |
| 129 Mint mint = new Mint(); | 131 Mint mint = new Mint(); |
| 130 replyTo.send(mint.port); | 132 replyTo.send(mint.port); |
| 131 }); | 133 }); |
| 132 } | 134 } |
| 133 | 135 |
| 134 class MintMakerWrapper { | 136 class MintMakerWrapper { |
| 135 final SendPort _port; | 137 final SendPort _port; |
| 136 | 138 |
| 137 static Future<MintMakerWrapper> create() { | 139 static Future<MintMakerWrapper> create() { |
| 138 ReceivePort reply = new ReceivePort(); | 140 ReceivePort reply = new ReceivePort(); |
| 139 return Isolate.spawn(mintMakerWrapper, reply.sendPort).then((_) => | 141 return Isolate |
| 140 reply.first.then((port) => new MintMakerWrapper._(port))); | 142 .spawn(mintMakerWrapper, reply.sendPort) |
| 143 .then((_) => reply.first.then((port) => new MintMakerWrapper._(port))); |
| 141 } | 144 } |
| 142 | 145 |
| 143 MintMakerWrapper._(this._port); | 146 MintMakerWrapper._(this._port); |
| 144 | 147 |
| 145 void makeMint(handleMint(MintWrapper mint)) { | 148 void makeMint(handleMint(MintWrapper mint)) { |
| 146 ReceivePort reply = new ReceivePort(); | 149 ReceivePort reply = new ReceivePort(); |
| 147 reply.first.then((SendPort mint) { handleMint(new MintWrapper(mint)); }); | 150 reply.first.then((SendPort mint) { |
| 151 handleMint(new MintWrapper(mint)); |
| 152 }); |
| 148 _port.send(reply.sendPort); | 153 _port.send(reply.sendPort); |
| 149 } | 154 } |
| 150 } | 155 } |
| 151 | 156 |
| 152 _checkBalance(PurseWrapper wrapper, expected) { | 157 _checkBalance(PurseWrapper wrapper, expected) { |
| 153 wrapper.queryBalance(expectAsync((int balance) { | 158 wrapper.queryBalance(expectAsync((int balance) { |
| 154 expect(balance, equals(expected)); | 159 expect(balance, equals(expected)); |
| 155 })); | 160 })); |
| 156 } | 161 } |
| 157 | 162 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 172 | 177 |
| 173 sprouted.deposit(purse, 42); | 178 sprouted.deposit(purse, 42); |
| 174 _checkBalance(sprouted, 0 + 5 + 42); | 179 _checkBalance(sprouted, 0 + 5 + 42); |
| 175 _checkBalance(purse, 100 - 5 - 42); | 180 _checkBalance(purse, 100 - 5 - 42); |
| 176 })); | 181 })); |
| 177 })); | 182 })); |
| 178 })); | 183 })); |
| 179 })); | 184 })); |
| 180 }); | 185 }); |
| 181 } | 186 } |
| OLD | NEW |