OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, 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 | |
7 import 'package:stack_trace/stack_trace.dart'; | |
8 import 'package:stream_channel/stream_channel.dart'; | |
9 | |
10 import '../../backend/group.dart'; | |
11 import '../../backend/metadata.dart'; | |
12 import '../../backend/test.dart'; | |
13 import '../../backend/test_platform.dart'; | |
14 import '../../util/io.dart'; | |
15 import '../../util/remote_exception.dart'; | |
16 import '../environment.dart'; | |
17 import '../load_exception.dart'; | |
18 import '../runner_suite.dart'; | |
19 import '../runner_test.dart'; | |
20 import 'environment.dart'; | |
21 | |
22 /// A class that defines a platform for which test suites can be loaded. | |
23 /// | |
24 /// A minimal plugin must define [platforms], which indicates the platforms it | |
25 /// supports, and [loadChannel], which connects to a client in which the tests | |
26 /// are defined. This is enough to support most of the test runner's | |
27 /// functionality. | |
28 /// | |
29 /// In order to support interactive debugging, a plugin must override [load] as | |
30 /// well, which returns a [RunnerSuite] that can contain a custom [Environment] | |
31 /// and control debugging metadata such as [RunnerSuite.isDebugging] and | |
32 /// [RunnerSuite.onDebugging]. To make this easier, implementations can call | |
33 /// [deserializeSuite]. | |
34 /// | |
35 /// A platform plugin can be registered with [Loader.registerPlatformPlugin]. | |
36 abstract class PlatformPlugin { | |
37 /// The platforms supported by this plugin. | |
38 /// | |
39 /// A plugin may declare support for existing platform, in which case it | |
40 /// overrides the previous loading functionality for that platform. | |
41 List<TestPlatform> get platforms; | |
42 | |
43 /// Loads and establishes a connection with the test file at [path] using | |
44 /// [platform]. | |
45 /// | |
46 /// This returns a channel that's connected to a remote client. The client | |
47 /// must connect it to a channel returned by [serializeGroup]. The default | |
48 /// implementation of [load] will take care of wrapping it up in a | |
49 /// [RunnerSuite] and running the tests when necessary. | |
50 /// | |
51 /// The returned channel may emit exceptions, indicating that the suite failed | |
52 /// to load or crashed later on. If the channel is closed by the caller, that | |
53 /// indicates that the suite is no longer needed and its resources may be | |
54 /// released. | |
55 /// | |
56 /// The [platform] is guaranteed to be a member of [platforms]. | |
57 StreamChannel loadChannel(String path, TestPlatform platform); | |
58 | |
59 /// Loads the runner suite for the test file at [path] using [platform], with | |
60 /// [metadata] parsed from the test file's top-level annotations. | |
61 /// | |
62 /// By default, this just calls [loadChannel] and passes its result to | |
63 /// [deserializeSuite]. However, it can be overridden to provide more | |
64 /// fine-grained control over the [RunnerSuite], including providing a custom | |
65 /// implementation of [Environment]. | |
66 /// | |
67 /// It's recommended that subclasses overriding this method call | |
68 /// [deserializeSuite] to obtain a [RunnerSuiteController]. | |
69 Future<RunnerSuite> load(String path, TestPlatform platform, | |
70 Metadata metadata) async { | |
71 // loadChannel may throw an exception. That's fine; it will cause the | |
72 // LoadSuite to emit an error, which will be presented to the user. | |
73 var channel = loadChannel(path, platform); | |
74 var controller = await deserializeSuite( | |
75 path, platform, metadata, new PluginEnvironment(), channel); | |
76 return controller.suite; | |
77 } | |
78 | |
79 /// A helper method for creating a [RunnerSuiteController] containing tests | |
80 /// that communicate over [channel]. | |
81 /// | |
82 /// This is notionally a protected method. It may be called by subclasses, but | |
83 /// it shouldn't be accessed by externally. | |
84 /// | |
85 /// This returns a controller so that the caller has a chance to control the | |
86 /// runner suite's debugging state based on plugin-specific logic. | |
87 Future<RunnerSuiteController> deserializeSuite(String path, | |
88 TestPlatform platform, Metadata metadata, Environment environment, | |
89 StreamChannel channel) async { | |
90 var disconnector = new Disconnector(); | |
91 var suiteChannel = new MultiChannel(channel.transform(disconnector)); | |
92 | |
93 suiteChannel.sink.add({ | |
94 'platform': platform.identifier, | |
95 'metadata': metadata.serialize(), | |
96 'os': platform == TestPlatform.vm ? currentOS.name : null | |
97 }); | |
98 | |
99 var completer = new Completer(); | |
100 | |
101 handleError(error, stackTrace) { | |
102 disconnector.disconnect(); | |
103 | |
104 if (completer.isCompleted) { | |
105 // If we've already provided a controller, send the error to the | |
106 // LoadSuite. This will cause the virtual load test to fail, which will | |
107 // notify the user of the error. | |
108 Zone.current.handleUncaughtError(error, stackTrace); | |
109 } else { | |
110 completer.completeError(error, stackTrace); | |
111 } | |
112 } | |
113 | |
114 suiteChannel.stream.listen((response) { | |
kevmoo
2016/02/11 19:57:49
Couldn't be done with `await for`?
nweiz
2016/02/11 21:29:18
No; we don't want to block the method on the strea
| |
115 switch (response["type"]) { | |
116 case "print": | |
117 print(response["line"]); | |
118 break; | |
119 | |
120 case "loadException": | |
121 handleError( | |
122 new LoadException(path, response["message"]), | |
123 new Trace.current()); | |
124 break; | |
125 | |
126 case "error": | |
127 var asyncError = RemoteException.deserialize(response["error"]); | |
128 handleError( | |
129 new LoadException(path, asyncError.error), | |
130 asyncError.stackTrace); | |
131 break; | |
132 | |
133 case "success": | |
134 completer.complete( | |
135 _deserializeGroup(suiteChannel, response["root"])); | |
136 break; | |
137 } | |
138 }, onError: handleError, onDone: () { | |
139 if (completer.isCompleted) return; | |
140 completer.completeError( | |
141 new LoadException( | |
142 path, "Connection closed before test suite loaded."), | |
143 new Trace.current()); | |
144 }); | |
145 | |
146 return new RunnerSuiteController( | |
147 environment, | |
148 await completer.future, | |
149 path: path, | |
150 platform: platform, | |
151 os: currentOS, | |
152 onClose: disconnector.disconnect); | |
153 } | |
154 | |
155 /// Deserializes [group] into a concrete [Group]. | |
156 Group _deserializeGroup(MultiChannel suiteChannel, Map group) { | |
157 var metadata = new Metadata.deserialize(group['metadata']); | |
158 return new Group(group['name'], group['entries'].map((entry) { | |
159 if (entry['type'] == 'group') { | |
160 return _deserializeGroup(suiteChannel, entry); | |
161 } | |
162 | |
163 return _deserializeTest(suiteChannel, entry); | |
164 }), | |
165 metadata: metadata, | |
166 setUpAll: _deserializeTest(suiteChannel, group['setUpAll']), | |
167 tearDownAll: _deserializeTest(suiteChannel, group['tearDownAll'])); | |
168 } | |
169 | |
170 /// Deserializes [test] into a concrete [Test] class. | |
171 /// | |
172 /// Returns `null` if [test] is `null`. | |
173 Test _deserializeTest(MultiChannel suiteChannel, Map test) { | |
174 if (test == null) return null; | |
175 | |
176 var metadata = new Metadata.deserialize(test['metadata']); | |
177 var testChannel = suiteChannel.virtualChannel(test['channel']); | |
178 return new RunnerTest(test['name'], metadata, testChannel); | |
179 } | |
180 } | |
OLD | NEW |