| 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 24 matching lines...) Expand all Loading... |
| 35 /// | 35 /// |
| 36 /// This will return `null` if the suite is unavailable for some reason (for | 36 /// This will return `null` if the suite is unavailable for some reason (for |
| 37 /// example if an error occurred while loading it). | 37 /// example if an error occurred while loading it). |
| 38 final Future<Suite> suite; | 38 final Future<Suite> suite; |
| 39 | 39 |
| 40 /// Creates a load suite named [name] on [platform]. | 40 /// Creates a load suite named [name] on [platform]. |
| 41 /// | 41 /// |
| 42 /// [body] may return either a [Suite] or a [Future] that completes to a | 42 /// [body] may return either a [Suite] or a [Future] that completes to a |
| 43 /// [Suite]. Its return value is forwarded through [suite], although if it | 43 /// [Suite]. Its return value is forwarded through [suite], although if it |
| 44 /// throws an error that will be forwarded through the suite's test. | 44 /// throws an error that will be forwarded through the suite's test. |
| 45 /// |
| 46 /// If the the load test is closed before [body] is complete, it will close |
| 47 /// the suite returned by [body] once it completes. |
| 45 factory LoadSuite(String name, body(), {String platform}) { | 48 factory LoadSuite(String name, body(), {String platform}) { |
| 46 var completer = new Completer.sync(); | 49 var completer = new Completer.sync(); |
| 47 return new LoadSuite._(name, () { | 50 return new LoadSuite._(name, () { |
| 48 var invoker = Invoker.current; | 51 var invoker = Invoker.current; |
| 49 invoker.addOutstandingCallback(); | 52 invoker.addOutstandingCallback(); |
| 50 | 53 |
| 51 invoke(() async { | 54 invoke(() async { |
| 52 try { | 55 try { |
| 53 var suite = await body(); | 56 var suite = await body(); |
| 54 if (completer.isCompleted) return; | 57 if (completer.isCompleted) { |
| 58 // If the load test has already been closed, close the suite it |
| 59 // generated. |
| 60 suite.close(); |
| 61 return; |
| 62 } |
| 63 |
| 55 completer.complete(suite); | 64 completer.complete(suite); |
| 56 invoker.removeOutstandingCallback(); | 65 invoker.removeOutstandingCallback(); |
| 57 } catch (error, stackTrace) { | 66 } catch (error, stackTrace) { |
| 58 registerException(error, stackTrace); | 67 registerException(error, stackTrace); |
| 59 if (!completer.isCompleted) completer.complete(); | 68 if (!completer.isCompleted) completer.complete(); |
| 60 } | 69 } |
| 61 }); | 70 }); |
| 62 | 71 |
| 63 // If the test is forcibly closed, exit immediately. It doesn't have any | 72 // If the test is forcibly closed, exit immediately. It doesn't have any |
| 64 // cleanup to do that won't be handled by Loader.close. | 73 // cleanup to do that won't be handled by Loader.close. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 88 platform: suite.platform); | 97 platform: suite.platform); |
| 89 } | 98 } |
| 90 | 99 |
| 91 LoadSuite._(String name, void body(), this.suite, {String platform}) | 100 LoadSuite._(String name, void body(), this.suite, {String platform}) |
| 92 : super([ | 101 : super([ |
| 93 new LocalTest(name, | 102 new LocalTest(name, |
| 94 new Metadata(timeout: new Timeout(new Duration(minutes: 5))), | 103 new Metadata(timeout: new Timeout(new Duration(minutes: 5))), |
| 95 body) | 104 body) |
| 96 ], platform: platform); | 105 ], platform: platform); |
| 97 | 106 |
| 107 /// A constructor used by [changeSuite]. |
| 108 LoadSuite._changeSuite(LoadSuite old, Future<Suite> this.suite) |
| 109 : super(old.tests, platform: old.platform); |
| 110 |
| 111 /// Creates a new [LoadSuite] that's identical to this one, but that |
| 112 /// transforms [suite] once it's loaded. |
| 113 /// |
| 114 /// If [suite] completes to `null`, [change] won't be run. |
| 115 Suite changeSuite(Suite change(Suite suite)) { |
| 116 return new LoadSuite._changeSuite(this, suite.then((loadedSuite) { |
| 117 if (loadedSuite == null) return null; |
| 118 return change(loadedSuite); |
| 119 })); |
| 120 } |
| 121 |
| 98 /// Runs the test and returns the suite. | 122 /// Runs the test and returns the suite. |
| 99 /// | 123 /// |
| 100 /// Rather than emitting errors through a [LiveTest], this just pipes them | 124 /// Rather than emitting errors through a [LiveTest], this just pipes them |
| 101 /// through the return value. | 125 /// through the return value. |
| 102 Future<Suite> getSuite() async { | 126 Future<Suite> getSuite() async { |
| 103 var liveTest = await tests.single.load(this); | 127 var liveTest = await tests.single.load(this); |
| 104 liveTest.onPrint.listen(print); | 128 liveTest.onPrint.listen(print); |
| 105 await liveTest.run(); | 129 await liveTest.run(); |
| 106 | 130 |
| 107 if (liveTest.errors.isEmpty) return await suite; | 131 if (liveTest.errors.isEmpty) return await suite; |
| 108 | 132 |
| 109 var error = liveTest.errors.first; | 133 var error = liveTest.errors.first; |
| 110 await new Future.error(error.error, error.stackTrace); | 134 await new Future.error(error.error, error.stackTrace); |
| 111 throw 'unreachable'; | 135 throw 'unreachable'; |
| 112 } | 136 } |
| 113 } | 137 } |
| OLD | NEW |