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

Side by Side Diff: tests/stub-generator/src/MintMakerFullyIsolatedTest.dart

Issue 8403040: Don't wait unnecessarily. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years, 1 month 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
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=MintMakerFullyIsolatedTest.dart:Mint,Purse,PowerfulPurse 5 // IsolateStubs=MintMakerFullyIsolatedTest.dart:Mint,Purse,PowerfulPurse
6 6
7 #import("../../isolate/src/TestFramework.dart"); 7 #import("../../isolate/src/TestFramework.dart");
8 8
9 interface Purse { 9 interface Purse {
10 Purse(); 10 Purse();
(...skipping 11 matching lines...) Expand all
22 // Return an int so we can wait for it to complete. Shame we can't 22 // Return an int so we can wait for it to complete. Shame we can't
23 // have a Promise<void>. 23 // have a Promise<void>.
24 int grab(int amount); 24 int grab(int amount);
25 Purse weak(); 25 Purse weak();
26 } 26 }
27 27
28 interface Mint factory MintImpl { 28 interface Mint factory MintImpl {
29 Mint(); 29 Mint();
30 30
31 Purse$Proxy createPurse(int balance); 31 Purse$Proxy createPurse(int balance);
32 PowerfulPurse$Proxy promote(Purse$Proxy purse); 32 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse);
33 } 33 }
34 34
35 // Because promises can't be used as keys in maps until they have 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 36 // completed, provide a wrapper. Note that if any key promise fails to
37 // resolve, then get()'s return may also fail to resolve. Also, 37 // resolve, then get()'s return may also fail to resolve. Right now, a
38 // although the logic is fine, this can't be used for a 38 // Proxy can also be used since it has (kludgily) been made to inherit
39 // ProxyMap. Perhaps both Proxy and Promise should inherit from 39 // from Promise. Perhaps both Proxy and Promise should inherit from
40 // Completable? 40 // Completable?
41 // NB: not tested and known to be buggy. Will fix in a future change. 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
42 class PromiseMap<S extends Promise, T> { 104 class PromiseMap<S extends Promise, T> {
43 105
44 PromiseMap() { 106 PromiseMap() {
45 _map = new Map<S, T>(); 107 _map = new Map<S, T>();
46 _incomplete = new Set<S>(); 108 _incomplete = new PromiseSet<S>();
47 } 109 }
48 110
49 T add(S s, T t) { 111 T add(S s, T t) {
112 print("PromiseMap.add");
50 _incomplete.add(s); 113 _incomplete.add(s);
51 s.addCompleteHandler((_) { 114 s.addCompleteHandler((_) {
115 print("PromiseMap.add move to map");
52 _map[s] = t; 116 _map[s] = t;
53 _incomplete.remove(s); 117 _incomplete.remove(s);
54 }); 118 });
55 return t; 119 return t;
56 } 120 }
57 121
58 Promise<T> find(S s) { 122 Promise<T> find(S s) {
59 T t = _map[s]; 123 print("PromiseMap.find");
60 if (t != null) 124 Promise<T> result = new Promise<T>();
61 return new Promise<T>.fromValue(t); 125 s.addCompleteHandler((_) {
62 Promise<T> p = new Promise<T>(); 126 print("PromiseMap.find s completed");
63 int counter = _incomplete.length; 127 T t = _map[s];
64 p.join(_incomplete, bool (S completed) { 128 if (t != null) {
65 if (completed != s) { 129 print(" immediate");
66 if (--counter == 0) { 130 result.complete(t);
67 p.complete(null); 131 return;
68 return true; 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 result.complete(null);
138 return;
139 }
140 result.join(_incomplete, bool (S completed) {
141 if (completed != s) {
142 if (--counter == 0) {
143 print("PromiseMap.find failed");
144 result.complete(null);
145 return true;
146 }
147 print("PromiseMap.find miss");
148 return false;
69 } 149 }
70 return false; 150 print("PromiseMap.find complete");
71 } 151 result.complete(_map[s]);
72 p.complete(_map[s]); 152 return true;
73 return true; 153 });
74 }); 154 });
75 return p; 155 return result;
76 } 156 }
77 157
78 Set<S> _incomplete; 158 PromiseSet<S> _incomplete;
79 Map<S, T> _map; 159 Map<S, T> _map;
80 160
81 } 161 }
82 162
163
83 class MintImpl implements Mint { 164 class MintImpl implements Mint {
84 165
85 MintImpl() { 166 MintImpl() {
86 //print('mint'); 167 print('mint');
87 if (_power == null) 168 if (_power == null)
88 _power = new Map<Purse$Proxy, PowerfulPurse$Proxy>(); 169 _power = new PromiseMap<Purse$Proxy, PowerfulPurse$Proxy>();
89 } 170 }
90 171
91 Purse$Proxy createPurse(int balance) { 172 Purse$Proxy createPurse(int balance) {
92 //print('createPurse'); 173 print('createPurse');
93 PowerfulPurse$ProxyImpl purse = 174 PowerfulPurse$ProxyImpl purse =
94 new PowerfulPurse$ProxyImpl.createIsolate(); 175 new PowerfulPurse$ProxyImpl.createIsolate();
95 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this); 176 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this);
96 purse.init(thisProxy, balance); 177 purse.init(thisProxy, balance);
97 178
98 Purse$Proxy weakPurse = purse.weak(); 179 Purse$Proxy weakPurse = purse.weak();
99 weakPurse.addCompleteHandler(() { 180 weakPurse.addCompleteHandler((_) {
100 //print('cP1'); 181 print('cP1');
101 _power[weakPurse] = purse; 182 _power.add(weakPurse, purse);
102 //print('cP2'); 183 print('cP2');
103 }); 184 });
104 return weakPurse; 185 return weakPurse;
105 } 186 }
106 187
107 PowerfulPurse$Proxy promote(Purse$Proxy purse) { 188 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse) {
108 // FIXME(benl): we should be using a PromiseMap here. But we get 189 print('promote $purse');
109 // away with it in this test for now. 190 return _power.find(purse);
110 //print('promote $purse/${_power[purse]}');
111 return _power[purse];
112 } 191 }
113 192
114 static Map<Purse$Proxy, PowerfulPurse$Proxy> _power; 193 static PromiseMap<Purse$Proxy, PowerfulPurse$Proxy> _power;
115 } 194 }
116 195
117 class PurseImpl implements PowerfulPurse { 196 class PurseImpl implements PowerfulPurse {
118 197
119 // FIXME(benl): autogenerate constructor, get rid of init(...). 198 // FIXME(benl): autogenerate constructor, get rid of init(...).
120 // Note that this constructor should not exist in the public interface 199 // Note that this constructor should not exist in the public interface
121 // PurseImpl(this._mint, this._balance) { } 200 // PurseImpl(this._mint, this._balance) { }
122 PurseImpl() { } 201 PurseImpl() { }
123 202
124 init(Mint$Proxy mint, int balance) { 203 init(Mint$Proxy mint, int balance) {
125 this._mint = mint; 204 this._mint = mint;
126 this._balance = balance; 205 this._balance = balance;
127 } 206 }
128 207
129 int queryBalance() { 208 int queryBalance() {
130 return _balance; 209 return _balance;
131 } 210 }
132 211
133 Purse$Proxy sproutPurse() { 212 Purse$Proxy sproutPurse() {
134 //print('sprout'); 213 print('sprout');
135 return _mint.createPurse(0); 214 return _mint.createPurse(0);
136 } 215 }
137 216
138 Promise<int> deposit(int amount, Purse$Proxy proxy) { 217 Promise<int> deposit(int amount, Purse$Proxy proxy) {
139 //print('deposit'); 218 print('deposit');
140 Promise<int> grabbed = _mint.promote(proxy).grab(amount); 219 Promise<PowerfulPurse$Proxy> powerful = _mint.promote(proxy);
141 Promise<int> done = new Promise<int>(); 220
142 grabbed.then((int) { 221 Promise<int> result = new Promise<int>();
143 //print("deposit done"); 222 powerful.then((_) {
144 _balance += amount; 223 Promise<int> grabbed = powerful.value.grab(amount);
145 done.complete(_balance); 224 grabbed.then((int grabbedAmount) {
225 _balance += grabbedAmount;
226 result.complete(_balance);
227 });
146 }); 228 });
147 return done; 229
230 return result;
148 } 231 }
149 232
150 int grab(int amount) { 233 int grab(int amount) {
151 //print("grab"); 234 print("grab");
152 if (_balance < amount) throw "Not enough dough."; 235 if (_balance < amount) throw "Not enough dough.";
153 _balance -= amount; 236 _balance -= amount;
154 return amount; 237 return amount;
155 } 238 }
156 239
157 Purse weak() { 240 Purse weak() {
158 return this; 241 return this;
159 } 242 }
160 243
161 Mint$Proxy _mint; 244 Mint$Proxy _mint;
(...skipping 27 matching lines...) Expand all
189 272
190 done = sprouted.deposit(42, purse); 273 done = sprouted.deposit(42, purse);
191 expect.completesWithValue(done, 5 + 42); 274 expect.completesWithValue(done, 5 + 42);
192 Promise<int> d2 = done.then((val) { 275 Promise<int> d2 = done.then((val) {
193 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) 276 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42)
194 .then((int value) => inner.complete(0)); 277 .then((int value) => inner.complete(0));
195 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) 278 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42)
196 .then((int value) => inner2.complete(0)); 279 .then((int value) => inner2.complete(0));
197 }); 280 });
198 expect.completes(d2); 281 expect.completes(d2);
282
283 return 0;
199 }); 284 });
200 expect.completes(d1); 285 expect.completesWithValue(d1, 0);
201 Promise<int> allDone = new Promise<int>(); 286 Promise<int> allDone = new Promise<int>();
202 allDone.waitFor([d3, inner, inner2], 3); 287 allDone.waitFor([d3, inner, inner2], 3);
203 allDone.then((_) => expect.succeeded()); 288 allDone.then((_) {
289 expect.succeeded();
290 print("##DONE##");
291 });
204 } 292 }
205 293
206 } 294 }
207 295
208 main() { 296 main() {
209 runTests([MintMakerFullyIsolatedTest.testMain]); 297 runTests([MintMakerFullyIsolatedTest.testMain]);
210 } 298 }
OLDNEW
« no previous file with comments | « corelib/src/promise.dart ('k') | tests/stub-generator/src/MintMakerFullyIsolatedTest-generatedTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698