| 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 interface Purse default PurseImpl{ | |
| 10 Purse(); | |
| 11 int queryBalance(); | |
| 12 Purse$Proxy sproutPurse(); | |
| 13 // The deposit has not completed until the promise completes. If we | |
| 14 // supported Promise<void> then this could use that. | |
| 15 Promise<int> deposit(int amount, Purse$Proxy source); | |
| 16 } | |
| 17 | |
| 18 interface PowerfulPurse extends Purse default PurseImpl { | |
| 19 PowerfulPurse(); | |
| 20 | |
| 21 void init(Mint$Proxy mint, int balance); | |
| 22 // Return an int so we can wait for it to complete. Shame we can't | |
| 23 // have a Promise<void>. | |
| 24 int grab(int amount); | |
| 25 Purse weak(); | |
| 26 } | |
| 27 | |
| 28 interface Mint default MintImpl { | |
| 29 Mint(); | |
| 30 | |
| 31 Purse$Proxy createPurse(int balance); | |
| 32 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse); | |
| 33 } | |
| 34 | |
| 35 // Because promises can't be used as keys in maps until they have | |
| 36 // completed, provide a wrapper. Note that if any key promise fails to | |
| 37 // resolve, then get()'s return may also fail to resolve. Right now, a | |
| 38 // Proxy can also be used since it has (kludgily) been made to inherit | |
| 39 // from Promise. Perhaps both Proxy and Promise should inherit from | |
| 40 // Completable? | |
| 41 // Note that we cannot extend Set rather than Collection because, for | |
| 42 // example, Set.remove() returns bool, whereas this will have to | |
| 43 // return Promise<bool>. | |
| 44 class PromiseSet<T extends Promise> implements Collection<T> { | |
| 45 | |
| 46 PromiseSet() { | |
| 47 _set = new List<T>(); | |
| 48 } | |
| 49 | |
| 50 PromiseSet.fromList(this._set); | |
| 51 | |
| 52 void add(T t) { | |
| 53 print("ProxySet.add"); | |
| 54 for (T x in _set) { | |
| 55 if (x === t) | |
| 56 return; | |
| 57 } | |
| 58 if (t.hasValue()) { | |
| 59 for (T x in _set) { | |
| 60 if (x.hasValue() && x == t) | |
| 61 return; | |
| 62 } | |
| 63 } | |
| 64 _set.add(t); | |
| 65 t.addCompleteHandler((_) { | |
| 66 // Remove any duplicates. | |
| 67 _remove(t, 1); | |
| 68 }); | |
| 69 } | |
| 70 | |
| 71 void _remove(T t, int threshold) { | |
| 72 print("PromiseSet.remove $threshold"); | |
| 73 int count = 0; | |
| 74 for (int n = 0; n < _set.length; ++n) | |
| 75 if (_set[n].hasValue() && _set[n] == t) | |
| 76 if (++count > threshold) { | |
| 77 print(" remove $n"); | |
| 78 _set.removeRange(n, 1); | |
| 79 --n; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void remove(T t) { | |
| 84 t.addCompleteHandler((_) { | |
| 85 _remove(t, 0); | |
| 86 }); | |
| 87 } | |
| 88 | |
| 89 int get length() => _set.length; | |
| 90 void forEach(void f(T element)) { _set.forEach(f); } | |
| 91 PromiseSet<T> filter(bool f(T element)) { | |
| 92 return new PromiseSet<T>.fromList(_set.filter(f)); | |
| 93 } | |
| 94 bool every(bool f(T element)) => _set.every(f); | |
| 95 bool some(bool f(T element)) => _set.some(f); | |
| 96 bool isEmpty() => _set.isEmpty(); | |
| 97 Iterator<T> iterator() => _set.iterator(); | |
| 98 | |
| 99 List<T> _set; | |
| 100 | |
| 101 } | |
| 102 | |
| 103 | |
| 104 class PromiseMap<S extends Promise, T> { | |
| 105 | |
| 106 PromiseMap() { | |
| 107 _map = new Map<S, T>(); | |
| 108 _incomplete = new PromiseSet<S>(); | |
| 109 } | |
| 110 | |
| 111 T add(S s, T t) { | |
| 112 print("PromiseMap.add"); | |
| 113 _incomplete.add(s); | |
| 114 s.addCompleteHandler((_) { | |
| 115 print("PromiseMap.add move to map"); | |
| 116 _map[s] = t; | |
| 117 _incomplete.remove(s); | |
| 118 }); | |
| 119 return t; | |
| 120 } | |
| 121 | |
| 122 Promise<T> find(S s) { | |
| 123 print("PromiseMap.find"); | |
| 124 Promise<T> findResult = new Promise<T>(); | |
| 125 s.addCompleteHandler((unused) { | |
| 126 print("PromiseMap.find s completed"); | |
| 127 T t = _map[s]; | |
| 128 if (t != null) { | |
| 129 print(" immediate"); | |
| 130 findResult.complete(t); | |
| 131 return; | |
| 132 } | |
| 133 // Otherwise, we need to wait for map[s] to complete... | |
| 134 int counter = _incomplete.length; | |
| 135 if (counter == 0) { | |
| 136 print(" none incomplete"); | |
| 137 findResult.complete(null); | |
| 138 return; | |
| 139 } | |
| 140 findResult.join(_incomplete, bool _(S completed) { | |
| 141 if (completed != s) { | |
| 142 if (--counter == 0) { | |
| 143 print("PromiseMap.find failed"); | |
| 144 findResult.complete(null); | |
| 145 return true; | |
| 146 } | |
| 147 print("PromiseMap.find miss"); | |
| 148 return false; | |
| 149 } | |
| 150 print("PromiseMap.find complete"); | |
| 151 findResult.complete(_map[s]); | |
| 152 return true; | |
| 153 }); | |
| 154 }); | |
| 155 return findResult; | |
| 156 } | |
| 157 | |
| 158 PromiseSet<S> _incomplete; | |
| 159 Map<S, T> _map; | |
| 160 | |
| 161 } | |
| 162 | |
| 163 | |
| 164 class MintImpl implements Mint { | |
| 165 | |
| 166 MintImpl() { | |
| 167 print('mint'); | |
| 168 if (_power == null) | |
| 169 _power = new PromiseMap<Purse$Proxy, PowerfulPurse$Proxy>(); | |
| 170 } | |
| 171 | |
| 172 Purse$Proxy createPurse(int balance) { | |
| 173 print('createPurse'); | |
| 174 PowerfulPurse$ProxyImpl purse = | |
| 175 new PowerfulPurse$ProxyImpl.createIsolate(); | |
| 176 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this); | |
| 177 purse.init(thisProxy, balance); | |
| 178 | |
| 179 Purse$Proxy weakPurse = purse.weak(); | |
| 180 weakPurse.addCompleteHandler((_) { | |
| 181 print('cP1'); | |
| 182 _power.add(weakPurse, purse); | |
| 183 print('cP2'); | |
| 184 }); | |
| 185 return weakPurse; | |
| 186 } | |
| 187 | |
| 188 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse) { | |
| 189 print('promote $purse'); | |
| 190 return _power.find(purse); | |
| 191 } | |
| 192 | |
| 193 static PromiseMap<Purse$Proxy, PowerfulPurse$Proxy> _power; | |
| 194 } | |
| 195 | |
| 196 class PurseImpl implements PowerfulPurse { | |
| 197 | |
| 198 // FIXME(benl): autogenerate constructor, get rid of init(...). | |
| 199 // Note that this constructor should not exist in the public interface | |
| 200 // PurseImpl(this._mint, this._balance) { } | |
| 201 PurseImpl() { } | |
| 202 | |
| 203 init(Mint$Proxy mint, int balance) { | |
| 204 this._mint = mint; | |
| 205 this._balance = balance; | |
| 206 } | |
| 207 | |
| 208 int queryBalance() { | |
| 209 return _balance; | |
| 210 } | |
| 211 | |
| 212 Purse$Proxy sproutPurse() { | |
| 213 print('sprout'); | |
| 214 return _mint.createPurse(0); | |
| 215 } | |
| 216 | |
| 217 Promise<int> deposit(int amount, Purse$Proxy proxy) { | |
| 218 print('deposit'); | |
| 219 Promise<PowerfulPurse$Proxy> powerful = _mint.promote(proxy); | |
| 220 | |
| 221 Promise<int> result = new Promise<int>(); | |
| 222 powerful.then((_) { | |
| 223 Promise<int> grabbed = powerful.value.grab(amount); | |
| 224 grabbed.then((int grabbedAmount) { | |
| 225 _balance += grabbedAmount; | |
| 226 result.complete(_balance); | |
| 227 }); | |
| 228 }); | |
| 229 | |
| 230 return result; | |
| 231 } | |
| 232 | |
| 233 int grab(int amount) { | |
| 234 print("grab"); | |
| 235 if (_balance < amount) throw "Not enough dough."; | |
| 236 _balance -= amount; | |
| 237 return amount; | |
| 238 } | |
| 239 | |
| 240 Purse weak() { | |
| 241 return this; | |
| 242 } | |
| 243 | |
| 244 Mint$Proxy _mint; | |
| 245 int _balance; | |
| 246 | |
| 247 } | |
| 248 | |
| 249 class MintMakerFullyIsolatedTest { | |
| 250 | |
| 251 static void testMain(TestExpectation expect) { | |
| 252 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); | |
| 253 Purse$Proxy purse = mint.createPurse(100); | |
| 254 // FIXME(benl): how do I write this? | |
| 255 //PowerfulPurse$Proxy power = (PowerfulPurse$Proxy)purse; | |
| 256 //expectEqualsStr("xxx", power.grab()); | |
| 257 Promise<int> balance = purse.queryBalance(); | |
| 258 expect.completesWithValue(balance, 100); | |
| 259 | |
| 260 Purse$Proxy sprouted = purse.sproutPurse(); | |
| 261 expect.completesWithValue(sprouted.queryBalance(), 0); | |
| 262 | |
| 263 Promise<int> done = sprouted.deposit(5, purse); | |
| 264 Promise<int> d3 = expect.completesWithValue(done, 5); | |
| 265 Promise<int> inner = new Promise<int>(); | |
| 266 Promise<int> inner2 = new Promise<int>(); | |
| 267 // FIXME(benl): it should not be necessary to wait here, I think, | |
| 268 // but without this, the tests seem to execute prematurely. | |
| 269 Promise<int> d1 = done.then((val) { | |
| 270 expect.completesWithValue(sprouted.queryBalance(), 0 + 5); | |
| 271 expect.completesWithValue(purse.queryBalance(), 100 - 5); | |
| 272 | |
| 273 done = sprouted.deposit(42, purse); | |
| 274 expect.completesWithValue(done, 5 + 42); | |
| 275 Promise<int> d2 = done.then((val_) { | |
| 276 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) | |
| 277 .then((int value) => inner.complete(0)); | |
| 278 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) | |
| 279 .then((int value) => inner2.complete(0)); | |
| 280 }); | |
| 281 expect.completes(d2); | |
| 282 | |
| 283 return 0; | |
| 284 }); | |
| 285 expect.completesWithValue(d1, 0); | |
| 286 Promise<int> allDone = new Promise<int>(); | |
| 287 allDone.waitFor([d3, inner, inner2], 3); | |
| 288 allDone.then((_) { | |
| 289 expect.succeeded(); | |
| 290 print("##DONE##"); | |
| 291 }); | |
| 292 } | |
| 293 | |
| 294 } | |
| 295 | |
| 296 main() { | |
| 297 runTests([MintMakerFullyIsolatedTest.testMain]); | |
| 298 } | |
| OLD | NEW |