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 library test.runner.load_suite; | 5 library test.runner.load_suite; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
10 | 10 |
(...skipping 21 matching lines...) Expand all Loading... |
32 /// A suite is constructed with logic necessary to produce a test suite. As with | 32 /// A suite is constructed with logic necessary to produce a test suite. As with |
33 /// a normal test body, this logic isn't run until [LiveTest.run] is called. The | 33 /// a normal test body, this logic isn't run until [LiveTest.run] is called. The |
34 /// suite itself is returned by [suite] once it's avaialble, but any errors or | 34 /// suite itself is returned by [suite] once it's avaialble, but any errors or |
35 /// prints will be emitted through the running [LiveTest]. | 35 /// prints will be emitted through the running [LiveTest]. |
36 class LoadSuite extends RunnerSuite { | 36 class LoadSuite extends RunnerSuite { |
37 /// A future that completes to the loaded suite once the suite's test has been | 37 /// A future that completes to the loaded suite once the suite's test has been |
38 /// run and completed successfully. | 38 /// run and completed successfully. |
39 /// | 39 /// |
40 /// This will return `null` if the suite is unavailable for some reason (for | 40 /// This will return `null` if the suite is unavailable for some reason (for |
41 /// example if an error occurred while loading it). | 41 /// example if an error occurred while loading it). |
42 final Future<RunnerSuite> suite; | 42 Future<RunnerSuite> get suite async => (await _suiteAndZone)?.first; |
| 43 |
| 44 /// A future that completes to a pair of [suite] and the load test's [Zone]. |
| 45 /// |
| 46 /// This will return `null` if the suite is unavailable for some reason (for |
| 47 /// example if an error occurred while loading it). |
| 48 final Future<Pair<RunnerSuite, Zone>> _suiteAndZone; |
43 | 49 |
44 /// Returns the test that loads the suite. | 50 /// Returns the test that loads the suite. |
45 /// | 51 /// |
46 /// Load suites are guaranteed to only contain one test. This is a utility | 52 /// Load suites are guaranteed to only contain one test. This is a utility |
47 /// method for accessing it directly. | 53 /// method for accessing it directly. |
48 Test get test => this.group.entries.single as Test; | 54 Test get test => this.group.entries.single as Test; |
49 | 55 |
50 /// Creates a load suite named [name] on [platform]. | 56 /// Creates a load suite named [name] on [platform]. |
51 /// | 57 /// |
52 /// [body] may return either a [RunnerSuite] or a [Future] that completes to a | 58 /// [body] may return either a [RunnerSuite] or a [Future] that completes to a |
(...skipping 11 matching lines...) Expand all Loading... |
64 invoke(() async { | 70 invoke(() async { |
65 try { | 71 try { |
66 var suite = await body(); | 72 var suite = await body(); |
67 if (completer.isCompleted) { | 73 if (completer.isCompleted) { |
68 // If the load test has already been closed, close the suite it | 74 // If the load test has already been closed, close the suite it |
69 // generated. | 75 // generated. |
70 suite.close(); | 76 suite.close(); |
71 return; | 77 return; |
72 } | 78 } |
73 | 79 |
74 completer.complete(suite); | 80 completer.complete(new Pair(suite, Zone.current)); |
75 invoker.removeOutstandingCallback(); | 81 invoker.removeOutstandingCallback(); |
76 } catch (error, stackTrace) { | 82 } catch (error, stackTrace) { |
77 registerException(error, stackTrace); | 83 registerException(error, stackTrace); |
78 if (!completer.isCompleted) completer.complete(); | 84 if (!completer.isCompleted) completer.complete(); |
79 } | 85 } |
80 }); | 86 }); |
81 | 87 |
82 // If the test is forcibly closed, exit immediately. It doesn't have any | 88 // If the test is forcibly closed, exit immediately. It doesn't have any |
83 // cleanup to do that won't be handled by Loader.close. | 89 // cleanup to do that won't be handled by Loader.close. |
84 invoker.onClose.then((_) { | 90 invoker.onClose.then((_) { |
(...skipping 15 matching lines...) Expand all Loading... |
100 return new Future.error(exception, stackTrace); | 106 return new Future.error(exception, stackTrace); |
101 }, platform: platform); | 107 }, platform: platform); |
102 } | 108 } |
103 | 109 |
104 /// A utility constructor for a load suite that just emits [suite]. | 110 /// A utility constructor for a load suite that just emits [suite]. |
105 factory LoadSuite.forSuite(RunnerSuite suite) { | 111 factory LoadSuite.forSuite(RunnerSuite suite) { |
106 return new LoadSuite("loading ${suite.path}", () => suite, | 112 return new LoadSuite("loading ${suite.path}", () => suite, |
107 platform: suite.platform); | 113 platform: suite.platform); |
108 } | 114 } |
109 | 115 |
110 LoadSuite._(String name, void body(), this.suite, {TestPlatform platform}) | 116 LoadSuite._(String name, void body(), this._suiteAndZone, |
| 117 {TestPlatform platform}) |
111 : super(const VMEnvironment(), new Group.root([ | 118 : super(const VMEnvironment(), new Group.root([ |
112 new LocalTest(name, | 119 new LocalTest(name, |
113 new Metadata(timeout: new Timeout(new Duration(minutes: 5))), | 120 new Metadata(timeout: new Timeout(new Duration(minutes: 5))), |
114 body) | 121 body) |
115 ]), platform: platform); | 122 ]), platform: platform); |
116 | 123 |
117 /// A constructor used by [changeSuite]. | 124 /// A constructor used by [changeSuite]. |
118 LoadSuite._changeSuite(LoadSuite old, Future<RunnerSuite> this.suite) | 125 LoadSuite._changeSuite(LoadSuite old, this._suiteAndZone) |
119 : super(const VMEnvironment(), old.group, platform: old.platform); | 126 : super(const VMEnvironment(), old.group, platform: old.platform); |
120 | 127 |
121 /// Creates a new [LoadSuite] that's identical to this one, but that | 128 /// Creates a new [LoadSuite] that's identical to this one, but that |
122 /// transforms [suite] once it's loaded. | 129 /// transforms [suite] once it's loaded. |
123 /// | 130 /// |
124 /// If [suite] completes to `null`, [change] won't be run. | 131 /// If [suite] completes to `null`, [change] won't be run. [change] is run |
| 132 /// within the load test's zone, so any errors or prints it emits will be |
| 133 /// associated with that test. |
125 LoadSuite changeSuite(RunnerSuite change(RunnerSuite suite)) { | 134 LoadSuite changeSuite(RunnerSuite change(RunnerSuite suite)) { |
126 return new LoadSuite._changeSuite(this, suite.then((loadedSuite) { | 135 return new LoadSuite._changeSuite(this, _suiteAndZone.then((pair) { |
127 if (loadedSuite == null) return null; | 136 if (pair == null) return null; |
128 return change(loadedSuite); | 137 |
| 138 var zone = pair.last; |
| 139 return new Pair(zone.runUnaryGuarded(change, pair.first), zone); |
129 })); | 140 })); |
130 } | 141 } |
131 | 142 |
132 /// Runs the test and returns the suite. | 143 /// Runs the test and returns the suite. |
133 /// | 144 /// |
134 /// Rather than emitting errors through a [LiveTest], this just pipes them | 145 /// Rather than emitting errors through a [LiveTest], this just pipes them |
135 /// through the return value. | 146 /// through the return value. |
136 Future<RunnerSuite> getSuite() async { | 147 Future<RunnerSuite> getSuite() async { |
137 var liveTest = await test.load(this); | 148 var liveTest = await test.load(this); |
138 liveTest.onPrint.listen(print); | 149 liveTest.onPrint.listen(print); |
139 await liveTest.run(); | 150 await liveTest.run(); |
140 | 151 |
141 if (liveTest.errors.isEmpty) return await suite; | 152 if (liveTest.errors.isEmpty) return await suite; |
142 | 153 |
143 var error = liveTest.errors.first; | 154 var error = liveTest.errors.first; |
144 await new Future.error(error.error, error.stackTrace); | 155 await new Future.error(error.error, error.stackTrace); |
145 throw 'unreachable'; | 156 throw 'unreachable'; |
146 } | 157 } |
147 } | 158 } |
OLD | NEW |