| 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. |
| 11 // See http://dartbug.com/23394 for more information. | 11 // See http://dartbug.com/23394 for more information. |
| 12 | 12 |
| 13 import 'dart:async'; | 13 import 'dart:async'; |
| 14 import 'package:expect/expect.dart'; | 14 import 'package:expect/expect.dart'; |
| 15 import 'package:async_helper/async_helper.dart'; | 15 import 'package:async_helper/async_helper.dart'; |
| 16 | 16 |
| 17 gee(i) async { | 17 gee(i) async { |
| 18 return await i; | 18 return await i; |
| 19 } | 19 } |
| 20 | 20 |
| 21 bar() async* { | 21 bar() async* { |
| 22 var i = 0; | 22 var i = 0; |
| 23 while (true) yield await gee(i++); | 23 while (true) yield await gee(i++); |
| 24 } | 24 } |
| 25 | 25 |
| 26 | |
| 27 awaitForTest() async { | 26 awaitForTest() async { |
| 28 var sum = 0; | 27 var sum = 0; |
| 29 await for (var x in bar().take(100)) { | 28 await for (var x in bar().take(100)) { |
| 30 sum += x; | 29 sum += x; |
| 31 } | 30 } |
| 32 Expect.equals(4950, sum); | 31 Expect.equals(4950, sum); |
| 33 } | 32 } |
| 34 | 33 |
| 35 awaitTest() async { | 34 awaitTest() async { |
| 36 await null; | 35 await null; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 } | 95 } |
| 97 | 96 |
| 98 registerCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | 97 registerCallback(Zone self, ZoneDelegate parent, Zone zone, 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 } |
| 105 |
| 106 registerUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | 106 registerUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 107 var oldDepth = depth; | 107 var oldDepth = depth; |
| 108 increaseDepth(); | 108 increaseDepth(); |
| 109 return parent.registerUnaryCallback(zone, (x) { | 109 return parent.registerUnaryCallback(zone, (x) { |
| 110 depth = oldDepth; | 110 depth = oldDepth; |
| 111 return f(x); | 111 return f(x); |
| 112 }); | 112 }); |
| 113 } | 113 } |
| 114 |
| 114 registerBinaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | 115 registerBinaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 115 var oldDepth = depth; | 116 var oldDepth = depth; |
| 116 increaseDepth(); | 117 increaseDepth(); |
| 117 return parent.registerBinaryCallback(zone, (x, y) { | 118 return parent.registerBinaryCallback(zone, (x, y) { |
| 118 depth = oldDepth; | 119 depth = oldDepth; |
| 119 return f(x, y); | 120 return f(x, y); |
| 120 }); | 121 }); |
| 121 } | 122 } |
| 122 | 123 |
| 123 sm(Zone self, ZoneDelegate parent, Zone zone, f) { | 124 sm(Zone self, ZoneDelegate parent, Zone zone, f) { |
| 124 var oldDepth = depth; | 125 var oldDepth = depth; |
| 125 increaseDepth(); | 126 increaseDepth(); |
| 126 return parent.scheduleMicrotask(zone, () { | 127 return parent.scheduleMicrotask(zone, () { |
| 127 depth = oldDepth; | 128 depth = oldDepth; |
| 128 return f(); | 129 return f(); |
| 129 }); | 130 }); |
| 130 } | 131 } |
| 131 | 132 |
| 132 main() { | 133 main() { |
| 133 asyncStart(); | 134 asyncStart(); |
| 134 var desc = new ZoneSpecification( | 135 var desc = new ZoneSpecification( |
| 135 registerCallback: registerCallback, | 136 registerCallback: registerCallback, |
| 136 registerUnaryCallback: registerUnaryCallback, | 137 registerUnaryCallback: registerUnaryCallback, |
| 137 registerBinaryCallback: registerBinaryCallback, | 138 registerBinaryCallback: registerBinaryCallback, |
| 138 scheduleMicrotask: sm | 139 scheduleMicrotask: sm); |
| 139 ); | |
| 140 var future = runZoned(runTests, zoneSpecification: desc); | 140 var future = runZoned(runTests, zoneSpecification: desc); |
| 141 future.then((_) => asyncEnd()); | 141 future.then((_) => asyncEnd()); |
| 142 } | 142 } |
| OLD | NEW |