| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 // Test that async functions don't zone-register their callbacks for each | 5 // Test that async functions don't zone-register their callbacks for each |
| 6 // await. Async functions should register their callback once in the beginning | 6 // await. Async functions should register their callback once in the beginning |
| 7 // and then reuse it for all awaits in their body. | 7 // and then reuse it for all awaits in their body. |
| 8 // This has two advantages: it is faster, when there are several awaits (on | 8 // This has two advantages: it is faster, when there are several awaits (on |
| 9 // the Future class from dart:async), and it avoids zone-nesting when tracing | 9 // the Future class from dart:async), and it avoids zone-nesting when tracing |
| 10 // stacktraces. | 10 // stacktraces. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 increaseDepth() { | 87 increaseDepth() { |
| 88 depthIncreases++; | 88 depthIncreases++; |
| 89 depth++; | 89 depth++; |
| 90 // The async/await code should not register callbacks recursively in the | 90 // The async/await code should not register callbacks recursively in the |
| 91 // then-calls. As such the depth should never grow too much. We don't want | 91 // then-calls. As such the depth should never grow too much. We don't want |
| 92 // to commit to a specific value, since implementations still have some | 92 // to commit to a specific value, since implementations still have some |
| 93 // room in how async/await is implemented, but 20 should be safe. | 93 // room in how async/await is implemented, but 20 should be safe. |
| 94 Expect.isTrue(depth < 20); | 94 Expect.isTrue(depth < 20); |
| 95 } | 95 } |
| 96 | 96 |
| 97 registerCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | 97 ZoneCallback<R> registerCallback<R>( |
| 98 Zone self, ZoneDelegate parent, Zone zone, R f()) { |
| 98 var oldDepth = depth; | 99 var oldDepth = depth; |
| 99 increaseDepth(); | 100 increaseDepth(); |
| 100 return parent.registerCallback(zone, () { | 101 return parent.registerCallback(zone, () { |
| 101 depth = oldDepth; | 102 depth = oldDepth; |
| 102 return f(); | 103 return f(); |
| 103 }); | 104 }); |
| 104 } | 105 } |
| 105 | 106 |
| 106 registerUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | 107 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>( |
| 108 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)) { |
| 107 var oldDepth = depth; | 109 var oldDepth = depth; |
| 108 increaseDepth(); | 110 increaseDepth(); |
| 109 return parent.registerUnaryCallback(zone, (x) { | 111 return parent.registerUnaryCallback(zone, (x) { |
| 110 depth = oldDepth; | 112 depth = oldDepth; |
| 111 return f(x); | 113 return f(x); |
| 112 }); | 114 }); |
| 113 } | 115 } |
| 114 | 116 |
| 115 registerBinaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | 117 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( |
| 118 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)) { |
| 116 var oldDepth = depth; | 119 var oldDepth = depth; |
| 117 increaseDepth(); | 120 increaseDepth(); |
| 118 return parent.registerBinaryCallback(zone, (x, y) { | 121 return parent.registerBinaryCallback(zone, (x, y) { |
| 119 depth = oldDepth; | 122 depth = oldDepth; |
| 120 return f(x, y); | 123 return f(x, y); |
| 121 }); | 124 }); |
| 122 } | 125 } |
| 123 | 126 |
| 124 sm(Zone self, ZoneDelegate parent, Zone zone, f) { | 127 sm(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 125 var oldDepth = depth; | 128 var oldDepth = depth; |
| 126 increaseDepth(); | 129 increaseDepth(); |
| 127 return parent.scheduleMicrotask(zone, () { | 130 return parent.scheduleMicrotask(zone, () { |
| 128 depth = oldDepth; | 131 depth = oldDepth; |
| 129 return f(); | 132 return f(); |
| 130 }); | 133 }); |
| 131 } | 134 } |
| 132 | 135 |
| 133 main() { | 136 main() { |
| 134 asyncStart(); | 137 asyncStart(); |
| 135 var desc = new ZoneSpecification( | 138 var desc = new ZoneSpecification( |
| 136 registerCallback: registerCallback, | 139 registerCallback: registerCallback, |
| 137 registerUnaryCallback: registerUnaryCallback, | 140 registerUnaryCallback: registerUnaryCallback, |
| 138 registerBinaryCallback: registerBinaryCallback, | 141 registerBinaryCallback: registerBinaryCallback, |
| 139 scheduleMicrotask: sm); | 142 scheduleMicrotask: sm); |
| 140 var future = runZoned(runTests, zoneSpecification: desc); | 143 var future = runZoned(runTests, zoneSpecification: desc); |
| 141 future.then((_) => asyncEnd()); | 144 future.then((_) => asyncEnd()); |
| 142 } | 145 } |
| OLD | NEW |