| 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 ZoneCallback<R> registerCallback<R>( | 97 registerCallback(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 98 Zone self, ZoneDelegate parent, Zone zone, R f()) { | |
| 99 var oldDepth = depth; | 98 var oldDepth = depth; |
| 100 increaseDepth(); | 99 increaseDepth(); |
| 101 return parent.registerCallback(zone, () { | 100 return parent.registerCallback(zone, () { |
| 102 depth = oldDepth; | 101 depth = oldDepth; |
| 103 return f(); | 102 return f(); |
| 104 }); | 103 }); |
| 105 } | 104 } |
| 106 | 105 |
| 107 ZoneUnaryCallback<R, T> registerUnaryCallback<R, T>( | 106 registerUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 108 Zone self, ZoneDelegate parent, Zone zone, R f(T arg)) { | |
| 109 var oldDepth = depth; | 107 var oldDepth = depth; |
| 110 increaseDepth(); | 108 increaseDepth(); |
| 111 return parent.registerUnaryCallback(zone, (x) { | 109 return parent.registerUnaryCallback(zone, (x) { |
| 112 depth = oldDepth; | 110 depth = oldDepth; |
| 113 return f(x); | 111 return f(x); |
| 114 }); | 112 }); |
| 115 } | 113 } |
| 116 | 114 |
| 117 ZoneBinaryCallback<R, T1, T2> registerBinaryCallback<R, T1, T2>( | 115 registerBinaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 118 Zone self, ZoneDelegate parent, Zone zone, R f(T1 arg1, T2 arg2)) { | |
| 119 var oldDepth = depth; | 116 var oldDepth = depth; |
| 120 increaseDepth(); | 117 increaseDepth(); |
| 121 return parent.registerBinaryCallback(zone, (x, y) { | 118 return parent.registerBinaryCallback(zone, (x, y) { |
| 122 depth = oldDepth; | 119 depth = oldDepth; |
| 123 return f(x, y); | 120 return f(x, y); |
| 124 }); | 121 }); |
| 125 } | 122 } |
| 126 | 123 |
| 127 sm(Zone self, ZoneDelegate parent, Zone zone, f) { | 124 sm(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 128 var oldDepth = depth; | 125 var oldDepth = depth; |
| 129 increaseDepth(); | 126 increaseDepth(); |
| 130 return parent.scheduleMicrotask(zone, () { | 127 return parent.scheduleMicrotask(zone, () { |
| 131 depth = oldDepth; | 128 depth = oldDepth; |
| 132 return f(); | 129 return f(); |
| 133 }); | 130 }); |
| 134 } | 131 } |
| 135 | 132 |
| 136 main() { | 133 main() { |
| 137 asyncStart(); | 134 asyncStart(); |
| 138 var desc = new ZoneSpecification( | 135 var desc = new ZoneSpecification( |
| 139 registerCallback: registerCallback, | 136 registerCallback: registerCallback, |
| 140 registerUnaryCallback: registerUnaryCallback, | 137 registerUnaryCallback: registerUnaryCallback, |
| 141 registerBinaryCallback: registerBinaryCallback, | 138 registerBinaryCallback: registerBinaryCallback, |
| 142 scheduleMicrotask: sm); | 139 scheduleMicrotask: sm); |
| 143 var future = runZoned(runTests, zoneSpecification: desc); | 140 var future = runZoned(runTests, zoneSpecification: desc); |
| 144 future.then((_) => asyncEnd()); | 141 future.then((_) => asyncEnd()); |
| 145 } | 142 } |
| OLD | NEW |