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 interface Mint factory MintImpl { | 9 interface Mint factory MintImpl { |
10 | 10 |
11 Mint(); | 11 Mint(); |
12 | 12 |
13 Purse createPurse(int balance); | 13 Purse createPurse(int balance); |
14 | 14 |
15 } | 15 } |
16 | 16 |
17 interface Purse factory PurseImpl { | 17 interface Purse factory PurseImpl { |
18 | 18 |
19 Purse(); | 19 Purse(); |
20 | 20 |
21 int queryBalance(); | 21 int queryBalance(); |
22 Purse sproutPurse(); | 22 Purse sproutPurse(); |
23 int deposit(int amount, Purse$Proxy source); | 23 Promise<int> deposit(int amount, Purse$Proxy source); |
24 | 24 |
25 } | 25 } |
26 | 26 |
27 class MintImpl implements Mint { | 27 class MintImpl implements Mint { |
28 | 28 |
29 MintImpl() { } | 29 MintImpl() { } |
30 | 30 |
31 Purse createPurse(int balance) { | 31 Purse createPurse(int balance) { |
32 PurseImpl purse = new PurseImpl(); | 32 PurseImpl purse = new PurseImpl(); |
33 purse.init(this, balance); | 33 purse.init(this, balance); |
(...skipping 16 matching lines...) Expand all Loading... |
50 } | 50 } |
51 | 51 |
52 int queryBalance() { | 52 int queryBalance() { |
53 return _balance; | 53 return _balance; |
54 } | 54 } |
55 | 55 |
56 Purse sproutPurse() { | 56 Purse sproutPurse() { |
57 return _mint.createPurse(0); | 57 return _mint.createPurse(0); |
58 } | 58 } |
59 | 59 |
60 int deposit(int amount, Purse$Proxy proxy) { | 60 Promise<int> deposit(int amount, Purse$Proxy proxy) { |
61 if (amount < 0) throw "Ha ha"; | 61 if (amount < 0) throw "Ha ha"; |
62 // Because we are in the same isolate as the other purse, we can | 62 // Because we are in the same isolate as the other purse, we can |
63 // retrieve the proxy's local PurseImpl object and act on it | 63 // retrieve the proxy's local PurseImpl object and act on it |
64 // directly. Further, a forged purse will not be convertible, and | 64 // directly. Further, a forged purse will not be convertible, and |
65 // so an attempt to use it will fail. | 65 // so an attempt to use it will fail. |
66 PurseImpl source = proxy.dynamic.local; | 66 Promise<int> balance = new Promise<int>(); |
67 if (source._balance < amount) throw "Not enough dough."; | 67 proxy.addCompleteHandler((_) { |
68 _balance += amount; | 68 PurseImpl source = proxy.dynamic.local; |
69 source._balance -= amount; | 69 if (source._balance < amount) throw "Not enough dough."; |
70 return _balance; | 70 _balance += amount; |
| 71 source._balance -= amount; |
| 72 balance.complete(_balance); |
| 73 }); |
| 74 return balance; |
71 } | 75 } |
72 | 76 |
73 Mint _mint; | 77 Mint _mint; |
74 int _balance; | 78 int _balance; |
75 | 79 |
76 } | 80 } |
77 | 81 |
78 class MintMakerPromiseWithStubsTest { | 82 class MintMakerPromiseWithStubsTest { |
79 | 83 |
80 static void testMain(TestExpectation expect) { | 84 static void testMain(TestExpectation expect) { |
81 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); | 85 Mint$Proxy mint = new Mint$ProxyImpl.createIsolate(); |
82 Purse$Proxy purse = mint.createPurse(100); | 86 Purse$Proxy purse = mint.createPurse(100); |
83 expect.completesWithValue(purse.queryBalance(), 100); | 87 expect.completesWithValue(purse.queryBalance(), 100); |
84 | 88 |
85 Purse$Proxy sprouted = purse.sproutPurse(); | 89 Purse$Proxy sprouted = purse.sproutPurse(); |
86 expect.completesWithValue(sprouted.queryBalance(), 0); | 90 expect.completesWithValue(sprouted.queryBalance(), 0); |
87 | 91 |
88 // FIXME(benl): We should not have to manually order the calls | 92 // FIXME(benl): We should not have to manually order the calls |
89 // like this. | 93 // like this. |
90 Promise<int> result = sprouted.deposit(5, purse); | 94 Promise<int> result = sprouted.deposit(5, purse); |
91 expect.completesWithValue(result, 5); | 95 Promise p1 = expect.completesWithValue(result, 5); |
| 96 Promise<bool> p2 = new Promise<bool>(); |
| 97 Promise<bool> p3 = new Promise<bool>(); |
| 98 Promise<bool> p4 = new Promise<bool>(); |
| 99 Promise<bool> p5 = new Promise<bool>(); |
| 100 Promise<bool> p6 = new Promise<bool>(); |
92 result.addCompleteHandler((_) { | 101 result.addCompleteHandler((_) { |
93 expect.completesWithValue(sprouted.queryBalance(), 0 + 5); | 102 expect.completesWithValue(sprouted.queryBalance(), 0 + 5) |
94 expect.completesWithValue(purse.queryBalance(), 100 - 5); | 103 .then((_) => p2.complete(true)); |
| 104 expect.completesWithValue(purse.queryBalance(), 100 - 5) |
| 105 .then((_) => p3.complete(true)); |
95 | 106 |
96 result = sprouted.deposit(42, purse); | 107 result = sprouted.deposit(42, purse); |
97 expect.completesWithValue(result, 5 + 42); | 108 expect.completesWithValue(result, 5 + 42).then((_) => p4.complete(true)); |
98 result.addCompleteHandler((_) { | 109 result.addCompleteHandler((_) { |
99 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42); | 110 expect.completesWithValue(sprouted.queryBalance(), 0 + 5 + 42) |
100 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42); | 111 .then((_) => p5.complete(true)); |
| 112 expect.completesWithValue(purse.queryBalance(), 100 - 5 - 42) |
| 113 .then((_) => p6.complete(true)); |
101 }); | 114 }); |
102 }); | 115 }); |
103 expect.succeeded(); | 116 Promise<bool> done = new Promise<bool>(); |
| 117 done.waitFor([p1, p2, p3, p4, p5, p6], 6); |
| 118 done.then((_) { |
| 119 expect.succeeded(); |
| 120 print("##DONE##"); |
| 121 }); |
104 } | 122 } |
105 | 123 |
106 } | 124 } |
107 | 125 |
108 main() { | 126 main() { |
109 runTests([MintMakerPromiseWithStubsTest.testMain]); | 127 runTests([MintMakerPromiseWithStubsTest.testMain]); |
110 } | 128 } |
OLD | NEW |