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

Side by Side Diff: tests/isolate/src/MintMakerPromiseTest.dart

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

Powered by Google App Engine
This is Rietveld 408576698