Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: samples/tests/samples/src/proxy/MintMakerPromiseTest.dart

Issue 10253022: test rename overhaul: step 13 - samples (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 #library("MintMakerPromiseTest");
6 #import("dart:isolate");
7 #import("../../../../proxy/promise.dart");
8
9 interface Mint default MintImpl {
10
11 Mint();
12
13 Purse createPurse(int balance);
14
15 }
16
17
18 class MintImpl implements Mint {
19
20 MintImpl() { }
21
22 Purse createPurse(int balance) {
23 return new PurseImpl(this, balance);
24 }
25
26 }
27
28
29 interface Purse {
30
31 int queryBalance();
32 Purse sproutPurse();
33 int deposit(int amount, Purse$Proxy source);
34
35 }
36
37
38 class PurseImpl implements Purse {
39
40 PurseImpl(this._mint, this._balance) { }
41
42 int queryBalance() {
43 return _balance;
44 }
45
46 Purse sproutPurse() {
47 return _mint.createPurse(0);
48 }
49
50 int deposit(int amount, Purse$Proxy purse) {
51 Purse$ProxyImpl impl = purse.dynamic; // TODO: Get rid of this 'cast'.
52 PurseImpl source = impl.local;
53 if (source._balance < amount) throw "Not enough dough.";
54 _balance += amount;
55 source._balance -= amount;
56 //print("Moved $amount, leaving ${source._balance}");
57 return _balance;
58 }
59
60 Mint _mint;
61 int _balance;
62
63 }
64
65
66 class MintMakerPromiseTest {
67
68 static void testMain() {
69 Mint$Proxy mint = createMint();
70 Purse$Proxy purse = mint.createPurse(100);
71 expectEquals(100, purse.queryBalance());
72
73 Purse$Proxy sprouted = purse.sproutPurse();
74 expectEquals(0, sprouted.queryBalance());
75
76 Promise<int> balance = sprouted.deposit(5, purse);
77 expectEquals(0 + 5, balance);
78 // FIXME(benl): because we have no ordering constraints we have to
79 // manually order the messages or it all falls apart. We should
80 // implement E-ORDER.
81 balance.addCompleteHandler((_) {
82 expectEquals(0 + 5, sprouted.queryBalance());
83 expectEquals(100 - 5, purse.queryBalance());
84
85 balance = sprouted.deposit(42, purse);
86 expectEquals(0 + 5 + 42, balance);
87 balance.addCompleteHandler((_) {
88 expectEquals(0 + 5 + 42, sprouted.queryBalance());
89 expectEquals(100 - 5 - 42, purse.queryBalance());
90 // FIXME(benl): once more we could "pass" by not running anything much.
91 expectDone(8);
92 });
93 });
94
95 }
96
97 static Mint$Proxy createMint() {
98 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate());
99 return new Mint$ProxyImpl(isolate);
100 }
101
102
103 static List<Promise> results;
104
105 static void expectEquals(int expected, Promise<int> promise) {
106 if (results === null) {
107 results = new List<Promise>();
108 }
109 results.add(promise.then((int actual) {
110 //print("done $expected/$actual");
111 Expect.equals(expected, actual);
112 }));
113 }
114
115 static void expectDone(int n) {
116 if (results === null) {
117 Expect.equals(0, n);
118 } else {
119 Promise done = new Promise();
120 done.waitFor(results, results.length);
121 done.then((ignored) {
122 //print("expectDone $n/${results.length}");
123 Expect.equals(n, results.length);
124 });
125 }
126 }
127
128 }
129
130
131 // ---------------------------------------------------------------------------
132 // THE REST OF THIS FILE COULD BE AUTOGENERATED
133 // ---------------------------------------------------------------------------
134
135 interface Mint$Proxy {
136
137 Purse$Proxy createPurse(int balance); // Promise<int> balance.
138
139 }
140
141
142 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy {
143
144 Mint$ProxyImpl(Proxy isolate) : super.forReply(isolate.call([null])) {}
145
146 Purse$Proxy createPurse(int balance) {
147 return new Purse$ProxyImpl(this.call([balance]));
148 }
149
150 }
151
152
153 class Mint$Dispatcher extends Dispatcher<Mint> {
154
155 Mint$Dispatcher(Mint mint) : super(mint) { }
156
157 void process(var message, void reply(var response)) {
158 int balance = message[0];
159 Purse purse = target.createPurse(balance);
160 SendPort port = Dispatcher.serve(new Purse$Dispatcher(purse));
161 reply(port);
162 }
163
164 }
165
166
167 class Mint$Dispatcher$Isolate extends Isolate {
168
169 Mint$Dispatcher$Isolate() : super() { }
170
171 void main() {
172 this.port.receive((var message, SendPort replyTo) {
173 Mint mint = new Mint();
174 SendPort port = Dispatcher.serve(new Mint$Dispatcher(mint));
175 Proxy proxy = new Proxy.forPort(replyTo);
176 proxy.send([port]);
177 });
178 }
179
180 }
181
182
183 interface Purse$Proxy {
184
185 Promise<int> queryBalance();
186 Purse$Proxy sproutPurse();
187 Promise<int> deposit(int amount, Purse$Proxy source); // Promise<int> amount.
188
189 }
190
191
192 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy {
193
194 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { }
195
196 Promise<int> queryBalance() {
197 return this.call(["balance"]);
198 }
199
200 Promise<int> deposit(int amount, Purse$Proxy source) {
201 return this.call(["deposit", amount, source]);
202 }
203
204 Purse$Proxy sproutPurse() {
205 return new Purse$ProxyImpl(this.call(["sprout"]));
206 }
207
208 }
209
210
211 class Purse$Dispatcher extends Dispatcher<Purse> {
212
213 Purse$Dispatcher(Purse purse) : super(purse) { }
214
215 void process(var message, void reply(var response)) {
216 String command = message[0];
217 //print("command $command");
218 if (command == "balance") {
219 int balance = target.queryBalance();
220 reply(balance);
221 } else if (command == "deposit") {
222 int amount = message[1];
223 Promise<SendPort> port =
224 new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(message[2]));
225 port.addCompleteHandler((_) {
226 Purse$Proxy source = new Purse$ProxyImpl(port);
227 int balance = target.deposit(amount, source);
228 reply(balance);
229 });
230 } else if (command == "sprout") {
231 Purse purse = target.sproutPurse();
232 SendPort port = Dispatcher.serve(new Purse$Dispatcher(purse));
233 reply(port);
234 } else {
235 // TODO: Send an exception back.
236 reply("Exception: Command not understood");
237 }
238 //print("command $command done");
239 }
240
241 }
242
243 main() {
244 MintMakerPromiseTest.testMain();
245 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698