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

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. Also,
38 // although the logic is fine, this can't be used for a 38 // although the logic is fine, this can't be used for a
39 // ProxyMap. Perhaps both Proxy and Promise should inherit from 39 // ProxyMap. Perhaps both Proxy and Promise should inherit from
40 // Completable? 40 // Completable? Also, not sure if implementing Collection is really
41 // NB: not tested and known to be buggy. Will fix in a future change. 41 // sustainable, we shall see.
42 class PromiseMap<S extends Promise, T> { 42 class ProxySet<T extends Proxy> implements Collection<T> {
43 43
44 PromiseMap() { 44 ProxySet() {
45 _set = new List<T>();
46 }
47
48 ProxySet.fromList(this._set);
49
50 void add (T t) {
51 for (T x in _set)
52 if (x === t)
53 return;
54 if (t.hasValue())
55 for (T x in _set)
56 if (x.hasValue() && x == t)
57 return;
58 _set.add(t);
59 t.addCompleteHandler((_) {
60 _remove(t, 1);
61 });
62 }
63
64 void _remove(T t, int threshold) {
65 int count = 0;
66 for (int n = 0; n < _set.length; ++n)
67 if (_set[n].hasValue() && _set[n] == t)
68 if (++count > threshold) {
69 _set.removeRange(n, 1);
70 --n;
71 }
72 }
73
74 void remove(T t) {
75 t.addCompleteHandler((_) {
76 _remove(t, 0);
77 });
78 }
79
80 int get length() => _set.length;
81 void forEach(void f(T element)) { _set.forEach(f); }
82 ProxySet<T> filter(bool f(T element))
83 => new ProxySet<T>.fromList(_set.filter(f));
84 bool every(bool f(T element)) => _set.every(f);
85 bool some(bool f(T element)) => _set.some(f);
86 bool isEmpty() => _set.isEmpty();
87 Iterator<T> iterator() => _set.iterator();
88
89 List<T> _set;
90
91 }
92
93
94 class ProxyMap<S extends Proxy, T> {
95
96 ProxyMap() {
45 _map = new Map<S, T>(); 97 _map = new Map<S, T>();
46 _incomplete = new Set<S>(); 98 _incomplete = new ProxySet<S>();
47 } 99 }
48 100
49 T add(S s, T t) { 101 T add(S s, T t) {
50 _incomplete.add(s); 102 _incomplete.add(s);
51 s.addCompleteHandler((_) { 103 s.addCompleteHandler((_) {
52 _map[s] = t; 104 _map[s] = t;
53 _incomplete.remove(s); 105 _incomplete.remove(s);
54 }); 106 });
55 return t; 107 return t;
56 } 108 }
57 109
58 Promise<T> find(S s) { 110 Promise<T> find(S s) {
59 T t = _map[s]; 111 // premature optimisation?
60 if (t != null) 112 if (s.hasValue()) {
61 return new Promise<T>.fromValue(t); 113 T t = _map[s];
114 if (t != null)
115 return new Promise<T>.fromValue(t);
116 }
62 Promise<T> p = new Promise<T>(); 117 Promise<T> p = new Promise<T>();
63 int counter = _incomplete.length; 118 int counter = _incomplete.length;
64 p.join(_incomplete, bool (S completed) { 119 p.join(_incomplete, bool (S completed) {
65 if (completed != s) { 120 if (completed != s) {
66 if (--counter == 0) { 121 if (--counter == 0) {
67 p.complete(null); 122 p.complete(null);
68 return true; 123 return true;
69 } 124 }
70 return false; 125 return false;
71 } 126 }
72 p.complete(_map[s]); 127 p.complete(_map[s]);
73 return true; 128 return true;
74 }); 129 });
75 return p; 130 return p;
76 } 131 }
77 132
78 Set<S> _incomplete; 133 ProxySet<S> _incomplete;
79 Map<S, T> _map; 134 Map<S, T> _map;
80 135
81 } 136 }
82 137
138
83 class MintImpl implements Mint { 139 class MintImpl implements Mint {
84 140
85 MintImpl() { 141 MintImpl() {
86 //print('mint'); 142 //print('mint');
87 if (_power == null) 143 if (_power == null)
88 _power = new Map<Purse$Proxy, PowerfulPurse$Proxy>(); 144 _power = new ProxyMap<Purse$Proxy, PowerfulPurse$Proxy>();
89 } 145 }
90 146
91 Purse$Proxy createPurse(int balance) { 147 Purse$Proxy createPurse(int balance) {
92 //print('createPurse'); 148 //print('createPurse');
93 PowerfulPurse$ProxyImpl purse = 149 PowerfulPurse$ProxyImpl purse =
94 new PowerfulPurse$ProxyImpl.createIsolate(); 150 new PowerfulPurse$ProxyImpl.createIsolate();
95 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this); 151 Mint$Proxy thisProxy = new Mint$ProxyImpl.localProxy(this);
96 purse.init(thisProxy, balance); 152 purse.init(thisProxy, balance);
97 153
98 Purse$Proxy weakPurse = purse.weak(); 154 Purse$Proxy weakPurse = purse.weak();
99 weakPurse.addCompleteHandler(() { 155 weakPurse.addCompleteHandler((_) {
100 //print('cP1'); 156 //print('cP1');
101 _power[weakPurse] = purse; 157 _power.add(weakPurse, purse);
102 //print('cP2'); 158 //print('cP2');
103 }); 159 });
104 return weakPurse; 160 return weakPurse;
105 } 161 }
106 162
107 PowerfulPurse$Proxy promote(Purse$Proxy purse) { 163 Promise<PowerfulPurse$Proxy> promote(Purse$Proxy purse) {
108 // FIXME(benl): we should be using a PromiseMap here. But we get 164 // FIXME(benl): we should be using a PromiseMap here. But we get
109 // away with it in this test for now. 165 // away with it in this test for now.
110 //print('promote $purse/${_power[purse]}'); 166 //print('promote $purse/${_power[purse]}');
111 return _power[purse]; 167 return _power.find(purse);
112 } 168 }
113 169
114 static Map<Purse$Proxy, PowerfulPurse$Proxy> _power; 170 static ProxyMap<Purse$Proxy, PowerfulPurse$Proxy> _power;
115 } 171 }
116 172
117 class PurseImpl implements PowerfulPurse { 173 class PurseImpl implements PowerfulPurse {
118 174
119 // FIXME(benl): autogenerate constructor, get rid of init(...). 175 // FIXME(benl): autogenerate constructor, get rid of init(...).
120 // Note that this constructor should not exist in the public interface 176 // Note that this constructor should not exist in the public interface
121 // PurseImpl(this._mint, this._balance) { } 177 // PurseImpl(this._mint, this._balance) { }
122 PurseImpl() { } 178 PurseImpl() { }
123 179
124 init(Mint$Proxy mint, int balance) { 180 init(Mint$Proxy mint, int balance) {
125 this._mint = mint; 181 this._mint = mint;
126 this._balance = balance; 182 this._balance = balance;
127 } 183 }
128 184
129 int queryBalance() { 185 int queryBalance() {
130 return _balance; 186 return _balance;
131 } 187 }
132 188
133 Purse$Proxy sproutPurse() { 189 Purse$Proxy sproutPurse() {
134 //print('sprout'); 190 //print('sprout');
135 return _mint.createPurse(0); 191 return _mint.createPurse(0);
136 } 192 }
137 193
138 Promise<int> deposit(int amount, Purse$Proxy proxy) { 194 Promise<int> deposit(int amount, Purse$Proxy proxy) {
139 //print('deposit'); 195 //print('deposit');
140 Promise<int> grabbed = _mint.promote(proxy).grab(amount); 196 Promise<PowerfulPurse$Proxy> powerful = _mint.promote(proxy);
141 Promise<int> done = new Promise<int>(); 197
142 grabbed.then((int) { 198 return powerful.then(() {
143 //print("deposit done"); 199 powerful.value.grab(amount);
200 return amount;
201 }).then((int amount) {
144 _balance += amount; 202 _balance += amount;
145 done.complete(_balance); 203 return _balance;
146 }); 204 });
147 return done;
148 } 205 }
149 206
150 int grab(int amount) { 207 int grab(int amount) {
151 //print("grab"); 208 //print("grab");
152 if (_balance < amount) throw "Not enough dough."; 209 if (_balance < amount) throw "Not enough dough.";
153 _balance -= amount; 210 _balance -= amount;
154 return amount; 211 return amount;
155 } 212 }
156 213
157 Purse weak() { 214 Purse weak() {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 Promise<int> allDone = new Promise<int>(); 258 Promise<int> allDone = new Promise<int>();
202 allDone.waitFor([d3, inner, inner2], 3); 259 allDone.waitFor([d3, inner, inner2], 3);
203 allDone.then((_) => expect.succeeded()); 260 allDone.then((_) => expect.succeeded());
204 } 261 }
205 262
206 } 263 }
207 264
208 main() { 265 main() {
209 runTests([MintMakerFullyIsolatedTest.testMain]); 266 runTests([MintMakerFullyIsolatedTest.testMain]);
210 } 267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698