Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: lib/src/runner/browser/server.dart

Issue 1264393004: Pass Configuration to Loader and Server. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Code review changes Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/src/runner.dart ('k') | lib/src/runner/configuration.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.browser.server; 5 library test.runner.browser.server;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:io'; 9 import 'dart:io';
10 10
11 import 'package:async/async.dart'; 11 import 'package:async/async.dart';
12 import 'package:http_multi_server/http_multi_server.dart'; 12 import 'package:http_multi_server/http_multi_server.dart';
13 import 'package:path/path.dart' as p; 13 import 'package:path/path.dart' as p;
14 import 'package:pool/pool.dart'; 14 import 'package:pool/pool.dart';
15 import 'package:shelf/shelf.dart' as shelf; 15 import 'package:shelf/shelf.dart' as shelf;
16 import 'package:shelf/shelf_io.dart' as shelf_io; 16 import 'package:shelf/shelf_io.dart' as shelf_io;
17 import 'package:shelf_static/shelf_static.dart'; 17 import 'package:shelf_static/shelf_static.dart';
18 import 'package:shelf_web_socket/shelf_web_socket.dart'; 18 import 'package:shelf_web_socket/shelf_web_socket.dart';
19 19
20 import '../../backend/metadata.dart'; 20 import '../../backend/metadata.dart';
21 import '../../backend/suite.dart'; 21 import '../../backend/suite.dart';
22 import '../../backend/test_platform.dart'; 22 import '../../backend/test_platform.dart';
23 import '../../util/io.dart'; 23 import '../../util/io.dart';
24 import '../../util/one_off_handler.dart'; 24 import '../../util/one_off_handler.dart';
25 import '../../util/path_handler.dart'; 25 import '../../util/path_handler.dart';
26 import '../../util/stack_trace_mapper.dart'; 26 import '../../util/stack_trace_mapper.dart';
27 import '../../utils.dart'; 27 import '../../utils.dart';
28 import '../application_exception.dart'; 28 import '../configuration.dart';
29 import '../load_exception.dart'; 29 import '../load_exception.dart';
30 import 'browser_manager.dart'; 30 import 'browser_manager.dart';
31 import 'compiler_pool.dart'; 31 import 'compiler_pool.dart';
32 import 'polymer.dart'; 32 import 'polymer.dart';
33 33
34 /// A server that serves JS-compiled tests to browsers. 34 /// A server that serves JS-compiled tests to browsers.
35 /// 35 ///
36 /// A test suite may be loaded for a given file using [loadSuite]. 36 /// A test suite may be loaded for a given file using [loadSuite].
37 class BrowserServer { 37 class BrowserServer {
38 /// Starts the server. 38 /// Starts the server.
39 /// 39 ///
40 /// [root] is the root directory that the server should serve. It defaults to 40 /// [root] is the root directory that the server should serve. It defaults to
41 /// the working directory. 41 /// the working directory.
42 /// 42 static Future<BrowserServer> start(Configuration config, {String root})
43 /// If [packageRoot] is passed, it's used for all package imports when 43 async {
44 /// compiling tests to JS. Otherwise, the package root is inferred from 44 var server = new BrowserServer._(root, config);
45 /// [root].
46 ///
47 /// If [pubServeUrl] is passed, tests will be loaded from the `pub serve`
48 /// instance at that URL rather than from the filesystem.
49 ///
50 /// If [color] is true, console colors will be used when compiling Dart.
51 ///
52 /// If [jsTrace] is true, raw JavaScript stack traces will be used for tests
53 /// that are compiled to JavaScript.
54 ///
55 /// If the package root doesn't exist, throws an [ApplicationException].
56 static Future<BrowserServer> start({String root, String packageRoot,
57 Uri pubServeUrl, bool color: false, bool jsTrace: false}) async {
58 var server = new BrowserServer._(
59 root, packageRoot, pubServeUrl, color, jsTrace);
60 await server._load(); 45 await server._load();
61 return server; 46 return server;
62 } 47 }
63 48
64 /// The underlying HTTP server. 49 /// The underlying HTTP server.
65 HttpServer _server; 50 HttpServer _server;
66 51
67 /// A randomly-generated secret. 52 /// A randomly-generated secret.
68 /// 53 ///
69 /// This is used to ensure that other users on the same system can't snoop 54 /// This is used to ensure that other users on the same system can't snoop
70 /// on data being served through this server. 55 /// on data being served through this server.
71 final _secret = randomBase64(24, urlSafe: true); 56 final _secret = randomBase64(24, urlSafe: true);
72 57
73 /// The URL for this server. 58 /// The URL for this server.
74 Uri get url => baseUrlForAddress(_server.address, _server.port) 59 Uri get url => baseUrlForAddress(_server.address, _server.port)
75 .resolve(_secret + "/"); 60 .resolve(_secret + "/");
76 61
62 /// The test runner configuration.
63 Configuration _config;
64
77 /// A [OneOffHandler] for servicing WebSocket connections for 65 /// A [OneOffHandler] for servicing WebSocket connections for
78 /// [BrowserManager]s. 66 /// [BrowserManager]s.
79 /// 67 ///
80 /// This is one-off because each [BrowserManager] can only connect to a single 68 /// This is one-off because each [BrowserManager] can only connect to a single
81 /// WebSocket, 69 /// WebSocket,
82 final _webSocketHandler = new OneOffHandler(); 70 final _webSocketHandler = new OneOffHandler();
83 71
84 /// A [PathHandler] used to serve compiled JS. 72 /// A [PathHandler] used to serve compiled JS.
85 final _jsHandler = new PathHandler(); 73 final _jsHandler = new PathHandler();
86 74
87 /// The [CompilerPool] managing active instances of `dart2js`. 75 /// The [CompilerPool] managing active instances of `dart2js`.
88 /// 76 ///
89 /// This is `null` if tests are loaded from `pub serve`. 77 /// This is `null` if tests are loaded from `pub serve`.
90 final CompilerPool _compilers; 78 final CompilerPool _compilers;
91 79
92 /// The temporary directory in which compiled JS is emitted. 80 /// The temporary directory in which compiled JS is emitted.
93 final String _compiledDir; 81 final String _compiledDir;
94 82
95 /// The root directory served statically by this server. 83 /// The root directory served statically by this server.
96 final String _root; 84 final String _root;
97 85
98 /// The package root.
99 final String _packageRoot;
100
101 final bool _jsTrace;
102
103 /// The URL for the `pub serve` instance to use to load tests.
104 ///
105 /// This is `null` if tests should be compiled manually.
106 final Uri _pubServeUrl;
107
108 /// The pool of active `pub serve` compilations. 86 /// The pool of active `pub serve` compilations.
109 /// 87 ///
110 /// Pub itself ensures that only one compilation runs at a time; we just use 88 /// Pub itself ensures that only one compilation runs at a time; we just use
111 /// this pool to make sure that the output is nice and linear. 89 /// this pool to make sure that the output is nice and linear.
112 final _pubServePool = new Pool(1); 90 final _pubServePool = new Pool(1);
113 91
114 /// The HTTP client to use when caching JS files in `pub serve`. 92 /// The HTTP client to use when caching JS files in `pub serve`.
115 final HttpClient _http; 93 final HttpClient _http;
116 94
117 /// Whether [close] has been called. 95 /// Whether [close] has been called.
(...skipping 12 matching lines...) Expand all
130 108
131 /// A map from test suite paths to Futures that will complete once those 109 /// A map from test suite paths to Futures that will complete once those
132 /// suites are finished compiling. 110 /// suites are finished compiling.
133 /// 111 ///
134 /// This is used to make sure that a given test suite is only compiled once 112 /// This is used to make sure that a given test suite is only compiled once
135 /// per run, rather than once per browser per run. 113 /// per run, rather than once per browser per run.
136 final _compileFutures = new Map<String, Future>(); 114 final _compileFutures = new Map<String, Future>();
137 115
138 final _mappers = new Map<String, StackTraceMapper>(); 116 final _mappers = new Map<String, StackTraceMapper>();
139 117
140 BrowserServer._(String root, String packageRoot, Uri pubServeUrl, bool color, 118 BrowserServer._(String root, Configuration config)
141 this._jsTrace)
142 : _root = root == null ? p.current : root, 119 : _root = root == null ? p.current : root,
143 _packageRoot = packageRootFor(root, packageRoot), 120 _config = config,
144 _pubServeUrl = pubServeUrl, 121 _compiledDir = config.pubServeUrl == null ? createTempDir() : null,
145 _compiledDir = pubServeUrl == null ? createTempDir() : null, 122 _http = config.pubServeUrl == null ? null : new HttpClient(),
146 _http = pubServeUrl == null ? null : new HttpClient(), 123 _compilers = new CompilerPool(color: config.color);
147 _compilers = new CompilerPool(color: color);
148 124
149 /// Starts the underlying server. 125 /// Starts the underlying server.
150 Future _load() async { 126 Future _load() async {
151 var cascade = new shelf.Cascade() 127 var cascade = new shelf.Cascade()
152 .add(_webSocketHandler.handler); 128 .add(_webSocketHandler.handler);
153 129
154 if (_pubServeUrl == null) { 130 if (_config.pubServeUrl == null) {
155 cascade = cascade 131 cascade = cascade
156 .add(_createPackagesHandler()) 132 .add(_createPackagesHandler())
157 .add(_jsHandler.handler) 133 .add(_jsHandler.handler)
158 .add(createStaticHandler(_root)) 134 .add(createStaticHandler(_root))
159 .add(_wrapperHandler); 135 .add(_wrapperHandler);
160 } 136 }
161 137
162 var pipeline = new shelf.Pipeline() 138 var pipeline = new shelf.Pipeline()
163 .addMiddleware(nestingMiddleware(_secret)) 139 .addMiddleware(nestingMiddleware(_secret))
164 .addHandler(cascade.handler); 140 .addHandler(cascade.handler);
165 141
166 _server = await HttpMultiServer.loopback(0); 142 _server = await HttpMultiServer.loopback(0);
167 shelf_io.serveRequests(_server, pipeline); 143 shelf_io.serveRequests(_server, pipeline);
168 } 144 }
169 145
170 /// Returns a handler that serves the contents of the "packages/" directory 146 /// Returns a handler that serves the contents of the "packages/" directory
171 /// for any URL that contains "packages/". 147 /// for any URL that contains "packages/".
172 /// 148 ///
173 /// This is a factory so it can wrap a static handler. 149 /// This is a factory so it can wrap a static handler.
174 shelf.Handler _createPackagesHandler() { 150 shelf.Handler _createPackagesHandler() {
175 var staticHandler = 151 var staticHandler =
176 createStaticHandler(_packageRoot, serveFilesOutsidePath: true); 152 createStaticHandler(_config.packageRoot, serveFilesOutsidePath: true);
177 153
178 return (request) { 154 return (request) {
179 var segments = p.url.split(shelfUrl(request).path); 155 var segments = p.url.split(shelfUrl(request).path);
180 156
181 for (var i = 0; i < segments.length; i++) { 157 for (var i = 0; i < segments.length; i++) {
182 if (segments[i] != "packages") continue; 158 if (segments[i] != "packages") continue;
183 return staticHandler( 159 return staticHandler(
184 shelfChange(request, path: p.url.joinAll(segments.take(i + 1)))); 160 shelfChange(request, path: p.url.joinAll(segments.take(i + 1))));
185 } 161 }
186 162
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 if (new File(htmlPath).existsSync() && 219 if (new File(htmlPath).existsSync() &&
244 !new File(htmlPath).readAsStringSync() 220 !new File(htmlPath).readAsStringSync()
245 .contains('packages/test/dart.js')) { 221 .contains('packages/test/dart.js')) {
246 throw new LoadException( 222 throw new LoadException(
247 path, 223 path,
248 '"${htmlPath}" must contain <script src="packages/test/dart.js">' 224 '"${htmlPath}" must contain <script src="packages/test/dart.js">'
249 '</script>.'); 225 '</script>.');
250 } 226 }
251 227
252 var suiteUrl; 228 var suiteUrl;
253 if (_pubServeUrl != null) { 229 if (_config.pubServeUrl != null) {
254 var suitePrefix = p.withoutExtension( 230 var suitePrefix = p.withoutExtension(
255 p.relative(path, from: p.join(_root, 'test'))); 231 p.relative(path, from: p.join(_root, 'test')));
256 232
257 var jsUrl; 233 var jsUrl;
258 // Polymer generates a bootstrap entrypoint that wraps the entrypoint we 234 // Polymer generates a bootstrap entrypoint that wraps the entrypoint we
259 // see on disk, and modifies the HTML file to point to the bootstrap 235 // see on disk, and modifies the HTML file to point to the bootstrap
260 // instead. To make sure we get the right source maps and wait for the 236 // instead. To make sure we get the right source maps and wait for the
261 // right file to compile, we have some Polymer-specific logic here to load 237 // right file to compile, we have some Polymer-specific logic here to load
262 // the boostrap instead of the unwrapped file. 238 // the boostrap instead of the unwrapped file.
263 if (isPolymerEntrypoint(path)) { 239 if (isPolymerEntrypoint(path)) {
264 jsUrl = _pubServeUrl.resolve( 240 jsUrl = _config.pubServeUrl.resolve(
265 "$suitePrefix.html.polymer.bootstrap.dart.browser_test.dart.js"); 241 "$suitePrefix.html.polymer.bootstrap.dart.browser_test.dart.js");
266 } else { 242 } else {
267 jsUrl = _pubServeUrl.resolve( 243 jsUrl = _config.pubServeUrl.resolve(
268 '$suitePrefix.dart.browser_test.dart.js'); 244 '$suitePrefix.dart.browser_test.dart.js');
269 } 245 }
270 246
271 await _pubServeSuite(path, jsUrl); 247 await _pubServeSuite(path, jsUrl);
272 suiteUrl = _pubServeUrl.resolveUri(p.toUri('$suitePrefix.html')); 248 suiteUrl = _config.pubServeUrl.resolveUri(p.toUri('$suitePrefix.html'));
273 } else { 249 } else {
274 if (browser.isJS) await _compileSuite(path); 250 if (browser.isJS) await _compileSuite(path);
275 if (_closed) return null; 251 if (_closed) return null;
276 suiteUrl = url.resolveUri(p.toUri( 252 suiteUrl = url.resolveUri(p.toUri(
277 p.withoutExtension(p.relative(path, from: _root)) + ".html")); 253 p.withoutExtension(p.relative(path, from: _root)) + ".html"));
278 } 254 }
279 255
280 if (_closed) return null; 256 if (_closed) return null;
281 257
282 // TODO(nweiz): Don't start the browser until all the suites are compiled. 258 // TODO(nweiz): Don't start the browser until all the suites are compiled.
(...skipping 29 matching lines...) Expand all
312 // We don't care about the response body, but we have to drain it or 288 // We don't care about the response body, but we have to drain it or
313 // else the process can't exit. 289 // else the process can't exit.
314 response.listen((_) {}); 290 response.listen((_) {});
315 291
316 throw new LoadException(path, 292 throw new LoadException(path,
317 "Error getting $mapUrl: ${response.statusCode} " 293 "Error getting $mapUrl: ${response.statusCode} "
318 "${response.reasonPhrase}\n" 294 "${response.reasonPhrase}\n"
319 'Make sure "pub serve" is serving the test/ directory.'); 295 'Make sure "pub serve" is serving the test/ directory.');
320 } 296 }
321 297
322 if (_jsTrace) { 298 if (_config.jsTrace) {
323 // Drain the response stream. 299 // Drain the response stream.
324 response.listen((_) {}); 300 response.listen((_) {});
325 return; 301 return;
326 } 302 }
327 303
328 _mappers[path] = new StackTraceMapper( 304 _mappers[path] = new StackTraceMapper(
329 await UTF8.decodeStream(response), 305 await UTF8.decodeStream(response),
330 mapUrl: mapUrl, 306 mapUrl: mapUrl,
331 packageRoot: _pubServeUrl.resolve('packages'), 307 packageRoot: _config.pubServeUrl.resolve('packages'),
332 sdkRoot: _pubServeUrl.resolve('packages/\$sdk')); 308 sdkRoot: _config.pubServeUrl.resolve('packages/\$sdk'));
333 } on IOException catch (error) { 309 } on IOException catch (error) {
334 var message = getErrorMessage(error); 310 var message = getErrorMessage(error);
335 if (error is SocketException) { 311 if (error is SocketException) {
336 message = "${error.osError.message} " 312 message = "${error.osError.message} "
337 "(errno ${error.osError.errorCode})"; 313 "(errno ${error.osError.errorCode})";
338 } 314 }
339 315
340 throw new LoadException(path, 316 throw new LoadException(path,
341 "Error getting $mapUrl: $message\n" 317 "Error getting $mapUrl: $message\n"
342 'Make sure "pub serve" is running.'); 318 'Make sure "pub serve" is running.');
343 } finally { 319 } finally {
344 timer.cancel(); 320 timer.cancel();
345 } 321 }
346 }); 322 });
347 } 323 }
348 324
349 /// Compile the test suite at [dartPath] to JavaScript. 325 /// Compile the test suite at [dartPath] to JavaScript.
350 /// 326 ///
351 /// Once the suite has been compiled, it's added to [_jsHandler] so it can be 327 /// Once the suite has been compiled, it's added to [_jsHandler] so it can be
352 /// served. 328 /// served.
353 Future _compileSuite(String dartPath) { 329 Future _compileSuite(String dartPath) {
354 return _compileFutures.putIfAbsent(dartPath, () async { 330 return _compileFutures.putIfAbsent(dartPath, () async {
355 var dir = new Directory(_compiledDir).createTempSync('test_').path; 331 var dir = new Directory(_compiledDir).createTempSync('test_').path;
356 var jsPath = p.join(dir, p.basename(dartPath) + ".js"); 332 var jsPath = p.join(dir, p.basename(dartPath) + ".js");
357 333
358 await _compilers.compile(dartPath, jsPath, packageRoot: _packageRoot); 334 await _compilers.compile(dartPath, jsPath,
335 packageRoot: _config.packageRoot);
359 if (_closed) return; 336 if (_closed) return;
360 337
361 var jsUrl = p.toUri(p.relative(dartPath, from: _root)).path + 338 var jsUrl = p.toUri(p.relative(dartPath, from: _root)).path +
362 '.browser_test.dart.js'; 339 '.browser_test.dart.js';
363 _jsHandler.add(jsUrl, (request) { 340 _jsHandler.add(jsUrl, (request) {
364 return new shelf.Response.ok(new File(jsPath).readAsStringSync(), 341 return new shelf.Response.ok(new File(jsPath).readAsStringSync(),
365 headers: {'Content-Type': 'application/javascript'}); 342 headers: {'Content-Type': 'application/javascript'});
366 }); 343 });
367 344
368 var mapUrl = p.toUri(p.relative(dartPath, from: _root)).path + 345 var mapUrl = p.toUri(p.relative(dartPath, from: _root)).path +
369 '.browser_test.dart.js.map'; 346 '.browser_test.dart.js.map';
370 _jsHandler.add(mapUrl, (request) { 347 _jsHandler.add(mapUrl, (request) {
371 return new shelf.Response.ok( 348 return new shelf.Response.ok(
372 new File(jsPath + '.map').readAsStringSync(), 349 new File(jsPath + '.map').readAsStringSync(),
373 headers: {'Content-Type': 'application/json'}); 350 headers: {'Content-Type': 'application/json'});
374 }); 351 });
375 352
376 if (_jsTrace) return; 353 if (_config.jsTrace) return;
377 var mapPath = jsPath + '.map'; 354 var mapPath = jsPath + '.map';
378 _mappers[dartPath] = new StackTraceMapper( 355 _mappers[dartPath] = new StackTraceMapper(
379 new File(mapPath).readAsStringSync(), 356 new File(mapPath).readAsStringSync(),
380 mapUrl: p.toUri(mapPath), 357 mapUrl: p.toUri(mapPath),
381 packageRoot: p.toUri(_packageRoot), 358 packageRoot: p.toUri(_config.packageRoot),
382 sdkRoot: p.toUri(sdkDir)); 359 sdkRoot: p.toUri(sdkDir));
383 }); 360 });
384 } 361 }
385 362
386 /// Returns the [BrowserManager] for [platform], which should be a browser. 363 /// Returns the [BrowserManager] for [platform], which should be a browser.
387 /// 364 ///
388 /// If no browser manager is running yet, starts one. 365 /// If no browser manager is running yet, starts one.
389 Future<BrowserManager> _browserManagerFor(TestPlatform platform) { 366 Future<BrowserManager> _browserManagerFor(TestPlatform platform) {
390 var manager = _browserManagers[platform]; 367 var manager = _browserManagers[platform];
391 if (manager != null) return Result.release(manager); 368 if (manager != null) return Result.release(manager);
392 369
393 var completer = new Completer.sync(); 370 var completer = new Completer.sync();
394 var path = _webSocketHandler.create(webSocketHandler(completer.complete)); 371 var path = _webSocketHandler.create(webSocketHandler(completer.complete));
395 var webSocketUrl = url.replace(scheme: 'ws').resolve(path); 372 var webSocketUrl = url.replace(scheme: 'ws').resolve(path);
396 var hostUrl = (_pubServeUrl == null ? url : _pubServeUrl) 373 var hostUrl = (_config.pubServeUrl == null ? url : _config.pubServeUrl)
397 .resolve('packages/test/src/runner/browser/static/index.html') 374 .resolve('packages/test/src/runner/browser/static/index.html')
398 .replace(queryParameters: {'managerUrl': webSocketUrl.toString()}); 375 .replace(queryParameters: {'managerUrl': webSocketUrl.toString()});
399 376
400 var future = BrowserManager.start(platform, hostUrl, completer.future); 377 var future = BrowserManager.start(platform, hostUrl, completer.future);
401 378
402 // Capture errors and release them later to avoid Zone issues. This call to 379 // Capture errors and release them later to avoid Zone issues. This call to
403 // [_browserManagerFor] is running in a different [LoadSuite] than future 380 // [_browserManagerFor] is running in a different [LoadSuite] than future
404 // calls, which means they're also running in different error zones so 381 // calls, which means they're also running in different error zones so
405 // errors can't be freely passed between them. Storing the error or value as 382 // errors can't be freely passed between them. Storing the error or value as
406 // an explicit [Result] fixes that. 383 // an explicit [Result] fixes that.
(...skipping 13 matching lines...) Expand all
420 if (result.isError) return; 397 if (result.isError) return;
421 398
422 await result.asValue.value.close(); 399 await result.asValue.value.close();
423 }).toList(); 400 }).toList();
424 401
425 futures.add(_server.close()); 402 futures.add(_server.close());
426 futures.add(_compilers.close()); 403 futures.add(_compilers.close());
427 404
428 await Future.wait(futures); 405 await Future.wait(futures);
429 406
430 if (_pubServeUrl == null) { 407 if (_config.pubServeUrl == null) {
431 new Directory(_compiledDir).deleteSync(recursive: true); 408 new Directory(_compiledDir).deleteSync(recursive: true);
432 } else { 409 } else {
433 _http.close(); 410 _http.close();
434 } 411 }
435 }); 412 });
436 } 413 }
437 } 414 }
OLDNEW
« no previous file with comments | « lib/src/runner.dart ('k') | lib/src/runner/configuration.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698