Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import 'dart:async'; | |
| 6 import 'package:expect/expect.dart'; | |
| 7 import 'package:async_helper/async_helper.dart'; | |
| 8 | |
|
hausner
2015/08/11 21:22:07
Maybe a sentence or two to document what this test
floitsch
2015/08/12 15:03:14
Done.
| |
| 9 gee(i) async { | |
| 10 return await i; | |
| 11 } | |
| 12 | |
| 13 bar() async* { | |
| 14 var i = 0; | |
| 15 while (true) yield await gee(i++); | |
| 16 } | |
| 17 | |
| 18 | |
| 19 awaitForTest() async { | |
| 20 var sum = 0; | |
| 21 await for (var x in bar().take(100)) { | |
| 22 sum += x; | |
| 23 } | |
| 24 Expect.equals(4950, sum); | |
| 25 } | |
| 26 | |
| 27 awaitTest() async { | |
| 28 await(null); | |
|
hausner
2015/08/11 21:22:07
Curious: why use await like a function call here?
floitsch
2015/08/12 15:03:14
My mistake...
done.
| |
| 29 await(null); | |
| 30 await(null); | |
| 31 await(null); | |
| 32 await(null); | |
| 33 await(null); | |
| 34 await(null); | |
| 35 await(null); | |
| 36 await(null); | |
| 37 await(null); | |
| 38 await(null); | |
| 39 await(null); | |
| 40 await(null); | |
| 41 await(null); | |
| 42 await(null); | |
| 43 await(null); | |
| 44 await(null); | |
| 45 await(null); | |
| 46 await(null); | |
| 47 await(null); | |
| 48 await(null); | |
| 49 await(null); | |
| 50 await(null); | |
| 51 await(null); | |
| 52 await(null); | |
| 53 await(null); | |
| 54 await(null); | |
| 55 await(null); | |
| 56 await(null); | |
| 57 await(null); | |
| 58 await(null); | |
| 59 await(null); | |
| 60 await(null); | |
| 61 await(null); | |
| 62 await(null); | |
| 63 await(null); | |
| 64 await(null); | |
| 65 await(null); | |
| 66 await(null); | |
| 67 await(null); | |
| 68 return await 499; | |
| 69 } | |
| 70 | |
| 71 runTests() async { | |
| 72 await awaitTest(); | |
| 73 await awaitForTest(); | |
| 74 } | |
| 75 | |
| 76 var depth = 0; | |
| 77 | |
| 78 var depthIncreases = 0; | |
| 79 | |
| 80 increaseDepth() { | |
| 81 depthIncreases++; | |
| 82 depth++; | |
| 83 // The async/await code should not register callbacks recursively in the | |
| 84 // then-calls. As such the depth should never grow too much. We don't want | |
| 85 // to commit to a specific value, since implementations still have some | |
| 86 // room in how async/await is implemented, but 20 should be safe. | |
| 87 Expect.isTrue(depth < 20); | |
| 88 } | |
| 89 | |
| 90 registerCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | |
| 91 var oldDepth = depth; | |
| 92 increaseDepth(); | |
| 93 return parent.registerCallback(zone, () { | |
| 94 depth = oldDepth; | |
| 95 return f(); | |
| 96 }); | |
| 97 } | |
| 98 registerUnaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | |
| 99 var oldDepth = depth; | |
| 100 increaseDepth(); | |
| 101 return parent.registerUnaryCallback(zone, (x) { | |
| 102 depth = oldDepth; | |
| 103 return f(x); | |
| 104 }); | |
| 105 } | |
| 106 registerBinaryCallback(Zone self, ZoneDelegate parent, Zone zone, f) { | |
| 107 var oldDepth = depth; | |
| 108 increaseDepth(); | |
| 109 return parent.registerBinaryCallback(zone, (x, y) { | |
| 110 depth = oldDepth; | |
| 111 return f(x, y); | |
| 112 }); | |
| 113 } | |
| 114 | |
| 115 sm(Zone self, ZoneDelegate parent, Zone zone, f) { | |
| 116 var oldDepth = depth; | |
| 117 increaseDepth(); | |
| 118 return parent.scheduleMicrotask(zone, () { | |
| 119 depth = oldDepth; | |
| 120 return f(); | |
| 121 }); | |
| 122 } | |
| 123 | |
| 124 main() { | |
| 125 asyncStart(); | |
| 126 var desc = new ZoneSpecification( | |
| 127 registerCallback: registerCallback, | |
| 128 registerUnaryCallback: registerUnaryCallback, | |
| 129 registerBinaryCallback: registerBinaryCallback, | |
| 130 scheduleMicrotask: sm | |
| 131 ); | |
| 132 var future = runZoned(runTests, zoneSpecification: desc); | |
| 133 future.then((_) => asyncEnd()); | |
| 134 } | |
| OLD | NEW |