OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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 // IsolateStubs=MintMakerPromiseWithStubsTest.dart:Mint,Purse | 5 // IsolateStubs=MintMakerPromiseWithStubsTest.dart:Mint,Purse |
6 | 6 |
7 #import("../../isolate/src/TestFramework.dart"); | 7 #import("../../isolate/src/TestFramework.dart"); |
8 | 8 |
9 /* class = Mint (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Min
tMakerPromiseWithStubsTest.dart: 9) */ | 9 /* class = Mint (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Min
tMakerPromiseWithStubsTest.dart: 9) */ |
10 | 10 |
11 interface Mint$Proxy { | 11 interface Mint$Proxy extends Proxy { |
12 Purse$Proxy createPurse(int balance); | 12 Purse$Proxy createPurse(int balance); |
13 } | 13 } |
14 | 14 |
15 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy { | 15 class Mint$ProxyImpl extends ProxyImpl implements Mint$Proxy { |
16 Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | 16 Mint$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } |
17 Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])
) { } | 17 Mint$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null])
) { } |
18 factory Mint$ProxyImpl.createIsolate() { | 18 factory Mint$ProxyImpl.createIsolate() { |
19 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); | 19 Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate()); |
20 return new Mint$ProxyImpl.forIsolate(isolate); | 20 return new Mint$ProxyImpl.forIsolate(isolate); |
21 } | 21 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 Mint thing = new Mint(); | 54 Mint thing = new Mint(); |
55 SendPort port = Dispatcher.serve(new Mint$Dispatcher(thing)); | 55 SendPort port = Dispatcher.serve(new Mint$Dispatcher(thing)); |
56 Proxy proxy = new Proxy.forPort(replyTo); | 56 Proxy proxy = new Proxy.forPort(replyTo); |
57 proxy.send([port]); | 57 proxy.send([port]); |
58 }); | 58 }); |
59 } | 59 } |
60 } | 60 } |
61 | 61 |
62 /* class = Purse (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Mi
ntMakerPromiseWithStubsTest.dart: 17) */ | 62 /* class = Purse (tests/stub-generator/src/MintMakerPromiseWithStubsTest.dart/Mi
ntMakerPromiseWithStubsTest.dart: 17) */ |
63 | 63 |
64 interface Purse$Proxy { | 64 interface Purse$Proxy extends Proxy { |
65 Promise<int> queryBalance(); | 65 Promise<int> queryBalance(); |
66 | 66 |
67 Purse$Proxy sproutPurse(); | 67 Purse$Proxy sproutPurse(); |
68 | 68 |
69 Promise<int> deposit(int amount, Purse$Proxy source); | 69 Promise<int> deposit(int amount, Purse$Proxy source); |
70 } | 70 } |
71 | 71 |
72 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy { | 72 class Purse$ProxyImpl extends ProxyImpl implements Purse$Proxy { |
73 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } | 73 Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { } |
74 Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null]
)) { } | 74 Purse$ProxyImpl.forIsolate(Proxy isolate) : super.forReply(isolate.call([null]
)) { } |
75 factory Purse$ProxyImpl.createIsolate() { | 75 factory Purse$ProxyImpl.createIsolate() { |
76 Proxy isolate = new Proxy.forIsolate(new Purse$Dispatcher$Isolate()); | 76 Proxy isolate = new Proxy.forIsolate(new Purse$Dispatcher$Isolate()); |
77 return new Purse$ProxyImpl.forIsolate(isolate); | 77 return new Purse$ProxyImpl.forIsolate(isolate); |
78 } | 78 } |
79 factory Purse$ProxyImpl.localProxy(Purse obj) { | 79 factory Purse$ProxyImpl.localProxy(Purse obj) { |
80 return new Purse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(
new Purse$Dispatcher(obj)))); | 80 return new Purse$ProxyImpl(new Promise<SendPort>.fromValue(Dispatcher.serve(
new Purse$Dispatcher(obj)))); |
81 } | 81 } |
82 | 82 |
83 Promise<int> queryBalance() { | 83 Promise<int> queryBalance() { |
84 return this.call(["queryBalance"]); | 84 return this.call(["queryBalance"]); |
85 } | 85 } |
86 | 86 |
87 Purse$Proxy sproutPurse() { | 87 Purse$Proxy sproutPurse() { |
88 return new Purse$ProxyImpl(this.call(["sproutPurse"])); | 88 return new Purse$ProxyImpl(this.call(["sproutPurse"])); |
89 } | 89 } |
90 | 90 |
91 Promise<int> deposit(int amount, Purse$Proxy source) { | 91 Promise<int> deposit(int amount, Purse$Proxy source) { |
92 return this.call(["deposit", amount, source]); | 92 return new PromiseProxy<int>(this.call(["deposit", amount, source])); |
93 } | 93 } |
94 } | 94 } |
95 | 95 |
96 class Purse$Dispatcher extends Dispatcher<Purse> { | 96 class Purse$Dispatcher extends Dispatcher<Purse> { |
97 Purse$Dispatcher(Purse thing) : super(thing) { } | 97 Purse$Dispatcher(Purse thing) : super(thing) { } |
98 | 98 |
99 void process(var message, void reply(var response)) { | 99 void process(var message, void reply(var response)) { |
100 String command = message[0]; | 100 String command = message[0]; |
101 if (command == "Purse") { | 101 if (command == "Purse") { |
102 } else if (command == "queryBalance") { | 102 } else if (command == "queryBalance") { |
103 int queryBalance = target.queryBalance(); | 103 int queryBalance = target.queryBalance(); |
104 reply(queryBalance); | 104 reply(queryBalance); |
105 } else if (command == "sproutPurse") { | 105 } else if (command == "sproutPurse") { |
106 Purse sproutPurse = target.sproutPurse(); | 106 Purse sproutPurse = target.sproutPurse(); |
107 SendPort port = Dispatcher.serve(new Purse$Dispatcher(sproutPurse)); | 107 SendPort port = Dispatcher.serve(new Purse$Dispatcher(sproutPurse)); |
108 reply(port); | 108 reply(port); |
109 } else if (command == "deposit") { | 109 } else if (command == "deposit") { |
110 int amount = message[1]; | 110 int amount = message[1]; |
111 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); | 111 List<Promise<SendPort>> promises = new List<Promise<SendPort>>(); |
112 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[2]))); | 112 promises.add(new PromiseProxy<SendPort>(new Promise<SendPort>.fromValue(me
ssage[2]))); |
113 Promise done = new Promise(); | 113 Purse$Proxy source = new Purse$ProxyImpl(promises[0]); |
114 done.waitFor(promises, 1); | 114 Promise<int> deposit = target.deposit(amount, source); |
115 done.addCompleteHandler((_) { | 115 reply(deposit); |
116 Purse$Proxy source = new Purse$ProxyImpl(promises[0]); | |
117 int deposit = target.deposit(amount, source); | |
118 reply(deposit); | |
119 }); | |
120 } else { | 116 } else { |
121 // TODO(kasperl,benl): Somehow throw an exception instead. | 117 // TODO(kasperl,benl): Somehow throw an exception instead. |
122 reply("Exception: command '" + command + "' not understood by Purse."); | 118 reply("Exception: command '" + command + "' not understood by Purse."); |
123 } | 119 } |
124 } | 120 } |
125 } | 121 } |
126 | 122 |
127 class Purse$Dispatcher$Isolate extends Isolate { | 123 class Purse$Dispatcher$Isolate extends Isolate { |
128 Purse$Dispatcher$Isolate() : super() { } | 124 Purse$Dispatcher$Isolate() : super() { } |
129 | 125 |
(...skipping 13 matching lines...) Expand all Loading... |
143 Purse createPurse(int balance); | 139 Purse createPurse(int balance); |
144 | 140 |
145 } | 141 } |
146 | 142 |
147 interface Purse factory PurseImpl { | 143 interface Purse factory PurseImpl { |
148 | 144 |
149 Purse(); | 145 Purse(); |
150 | 146 |
151 int queryBalance(); | 147 int queryBalance(); |
152 Purse sproutPurse(); | 148 Purse sproutPurse(); |
153 int deposit(int amount, Purse$Proxy source); | 149 Promise<int> deposit(int amount, Purse$Proxy source); |
154 | 150 |
155 } | 151 } |
156 | 152 |
157 class MintImpl implements Mint { | 153 class MintImpl implements Mint { |
158 | 154 |
159 MintImpl() { } | 155 MintImpl() { } |
160 | 156 |
161 Purse createPurse(int balance) { | 157 Purse createPurse(int balance) { |
162 PurseImpl purse = new PurseImpl(); | 158 PurseImpl purse = new PurseImpl(); |
163 purse.init(this, balance); | 159 purse.init(this, balance); |
(...skipping 16 matching lines...) Expand all Loading... |
180 } | 176 } |
181 | 177 |
182 int queryBalance() { | 178 int queryBalance() { |
183 return _balance; | 179 return _balance; |
184 } | 180 } |
185 | 181 |
186 Purse sproutPurse() { | 182 Purse sproutPurse() { |
187 return _mint.createPurse(0); | 183 return _mint.createPurse(0); |
188 } | 184 } |
189 | 185 |
190 int deposit(int amount, Purse$Proxy proxy) { | 186 Promise<int> deposit(int amount, Purse$Proxy proxy) { |
191 if (amount < 0) throw "Ha ha"; | 187 if (amount < 0) throw "Ha ha"; |
192 // Because we are in the same isolate as the other purse, we can | 188 // Because we are in the same isolate as the other purse, we can |
193 // retrieve the proxy's local PurseImpl object and act on it | 189 // retrieve the proxy's local PurseImpl object and act on it |
194 // directly. Further, a forged purse will not be convertible, and | 190 // directly. Further, a forged purse will not be convertible, and |
195 // so an attempt to use it will fail. | 191 // so an attempt to use it will fail. |
196 PurseImpl source = proxy.dynamic.local; | 192 Promise<int> balance = new Promise<int>(); |
197 if (source._balance < amount) throw "Not enough dough."; | 193 proxy.addCompleteHandler((_) { |
198 _balance += amount; | 194 PurseImpl source = proxy.dynamic.local; |
199 source._balance -= amount; | 195 if (source._balance < amount) throw "Not enough dough."; |
200 return _balance; | 196 _balance += amount; |
| 197 source._balance -= amount; |
| 198 balance.complete(_balance); |
| 199 }); |
| 200 return balance; |
201 } | 201 } |
202 | 202 |
203 Mint _mint; | 203 Mint _mint; |
204 int _balance; | 204 int _balance; |
205 | 205 |
206 } | 206 } |
207 | 207 |
208 class MintMakerPromiseWithStubsTest { | 208 class MintMakerPromiseWithStubsTest { |
209 | 209 |
210 static void testMain(TestExpectation expect) { | 210 static void testMain(TestExpectation expect) { |
211 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); | 211 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); |
212 Purse$Proxy purse = mint.createPurse(100); | 212 Purse$Proxy purse = mint.createPurse(100); |
213 expect.completesWithValue(purse.queryBalance(), 100); | 213 expect.completesWithValue(purse.queryBalance(), 100); |
214 | 214 |
215 Purse$Proxy sprouted = purse.sproutPurse(); | 215 Purse$Proxy sprouted = purse.sproutPurse(); |
216 expect.completesWithValue(sprouted.queryBalance(), 0); | 216 expect.completesWithValue(sprouted.queryBalance(), 0); |
217 | 217 |
218 // FIXME(benl): We should not have to manually order the calls | 218 // FIXME(benl): We should not have to manually order the calls |
219 // like this. | 219 // like this. |
220 Promise<int> result = sprouted.deposit(5, purse); | 220 Promise<int> result = sprouted.deposit(5, purse); |
221 expect.completesWithValue(result, 5); | 221 Promise p1 = expect.completesWithValue(result, 5); |
| 222 Promise<bool> p2 = new Promise<bool>(); |
| 223 Promise<bool> p3 = new Promise<bool>(); |
| 224 Promise<bool> p4 = new Promise<bool>(); |
| 225 Promise<bool> p5 = new Promise<bool>(); |
| 226 Promise<bool> p6 = new Promise<bool>(); |
222 result.addCompleteHandler((_) { | 227 result.addCompleteHandler((_) { |
223 expect.completesWithValue(sprouted.queryBalance(), 0 + 5); | 228 expect.completesWithValue(sprouted.queryBalance(), 0 + 5) |
224 expect.completesWithValue(purse.queryBalance(), 100 - 5); | 229 .then((_) => p2.complete(true)); |
| 230 expect.completesWithValue(purse.queryBalance(), 100 - 5) |
| 231 .then((_) => p3.complete(true)); |
225 | 232 |
226 result = sprouted.deposit(42, purse); | 233 result = sprouted.deposit(42, purse); |
227 expect.completesWithValue(result, 5 + 42); | 234 expect.completesWithValue(result, 5 + 42).then((_) => p4.complete(true)); |
228 result.addCompleteHandler((_) { | 235 result.addCompleteHandler((_) { |
229 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42); | 236 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) |
230 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42); | 237 .then((_) => p5.complete(true)); |
| 238 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) |
| 239 .then((_) => p6.complete(true)); |
231 }); | 240 }); |
232 }); | 241 }); |
233 expect.succeeded(); | 242 Promise<bool> done = new Promise<bool>(); |
| 243 done.waitFor([p1, p2, p3, p4, p5, p6], 6); |
| 244 done.then((_) { |
| 245 expect.succeeded(); |
| 246 print("##DONE##"); |
| 247 }); |
234 } | 248 } |
235 | 249 |
236 } | 250 } |
237 | 251 |
238 main() { | 252 main() { |
239 runTests([MintMakerPromiseWithStubsTest.testMain]); | 253 runTests([MintMakerPromiseWithStubsTest.testMain]); |
240 } | 254 } |
OLD | NEW |