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

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

Powered by Google App Engine
This is Rietveld 408576698