| 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.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:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
| 12 import 'package:pool/pool.dart'; | 12 import 'package:pool/pool.dart'; |
| 13 import 'package:shelf/shelf.dart' as shelf; | 13 import 'package:shelf/shelf.dart' as shelf; |
| 14 import 'package:shelf/shelf_io.dart' as shelf_io; | 14 import 'package:shelf/shelf_io.dart' as shelf_io; |
| 15 import 'package:shelf_static/shelf_static.dart'; | 15 import 'package:shelf_static/shelf_static.dart'; |
| 16 import 'package:shelf_web_socket/shelf_web_socket.dart'; | 16 import 'package:shelf_web_socket/shelf_web_socket.dart'; |
| 17 | 17 |
| 18 import '../../backend/suite.dart'; | 18 import '../../backend/suite.dart'; |
| 19 import '../../backend/test_platform.dart'; |
| 19 import '../../util/io.dart'; | 20 import '../../util/io.dart'; |
| 20 import '../../util/one_off_handler.dart'; | 21 import '../../util/one_off_handler.dart'; |
| 21 import '../../utils.dart'; | 22 import '../../utils.dart'; |
| 22 import '../load_exception.dart'; | 23 import '../load_exception.dart'; |
| 24 import 'browser.dart'; |
| 23 import 'browser_manager.dart'; | 25 import 'browser_manager.dart'; |
| 24 import 'compiler_pool.dart'; | 26 import 'compiler_pool.dart'; |
| 25 import 'chrome.dart'; | 27 import 'chrome.dart'; |
| 28 import 'firefox.dart'; |
| 26 | 29 |
| 27 /// A server that serves JS-compiled tests to browsers. | 30 /// A server that serves JS-compiled tests to browsers. |
| 28 /// | 31 /// |
| 29 /// A test suite may be loaded for a given file using [loadSuite]. | 32 /// A test suite may be loaded for a given file using [loadSuite]. |
| 30 class BrowserServer { | 33 class BrowserServer { |
| 31 /// Starts the server. | 34 /// Starts the server. |
| 32 /// | 35 /// |
| 33 /// If [packageRoot] is passed, it's used for all package imports when | 36 /// If [packageRoot] is passed, it's used for all package imports when |
| 34 /// compiling tests to JS. Otherwise, the package root is inferred from the | 37 /// compiling tests to JS. Otherwise, the package root is inferred from the |
| 35 /// location of the source file. | 38 /// location of the source file. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 78 |
| 76 /// The pool of active `pub serve` compilations. | 79 /// The pool of active `pub serve` compilations. |
| 77 /// | 80 /// |
| 78 /// Pub itself ensures that only one compilation runs at a time; we just use | 81 /// Pub itself ensures that only one compilation runs at a time; we just use |
| 79 /// this pool to make sure that the output is nice and linear. | 82 /// this pool to make sure that the output is nice and linear. |
| 80 final _pubServePool = new Pool(1); | 83 final _pubServePool = new Pool(1); |
| 81 | 84 |
| 82 /// The HTTP client to use when caching JS files in `pub serve`. | 85 /// The HTTP client to use when caching JS files in `pub serve`. |
| 83 final HttpClient _http; | 86 final HttpClient _http; |
| 84 | 87 |
| 85 /// The browser in which test suites are loaded and run. | |
| 86 /// | |
| 87 /// This is `null` until a suite is loaded. | |
| 88 Chrome _browser; | |
| 89 | |
| 90 /// Whether [close] has been called. | 88 /// Whether [close] has been called. |
| 91 bool get _closed => _closeCompleter != null; | 89 bool get _closed => _closeCompleter != null; |
| 92 | 90 |
| 93 /// The completer for the [Future] returned by [close]. | 91 /// The completer for the [Future] returned by [close]. |
| 94 Completer _closeCompleter; | 92 Completer _closeCompleter; |
| 95 | 93 |
| 96 /// A future that will complete to the [BrowserManager] for [_browser]. | 94 /// All currently-running browsers. |
| 97 /// | 95 /// |
| 98 /// The first time this is called, it will start both the browser and the | 96 /// These are controlled by [_browserManager]s. |
| 99 /// browser manager. Any further calls will return the existing manager. | 97 final _browsers = new Map<TestPlatform, Browser>(); |
| 100 Future<BrowserManager> get _browserManager { | |
| 101 if (_browserManagerCompleter == null) { | |
| 102 _browserManagerCompleter = new Completer(); | |
| 103 var path = _webSocketHandler.create(webSocketHandler((webSocket) { | |
| 104 _browserManagerCompleter.complete(new BrowserManager(webSocket)); | |
| 105 })); | |
| 106 | 98 |
| 107 var webSocketUrl = url.replace(scheme: 'ws', path: '/$path'); | 99 /// A map from browser identifiers to futures that will complete to the |
| 108 | 100 /// [BrowserManager]s for those browsers. |
| 109 var hostUrl = url; | 101 /// |
| 110 if (_pubServeUrl != null) { | 102 /// This should only be accessed through [_browserManagerFor]. |
| 111 hostUrl = _pubServeUrl.resolve( | 103 final _browserManagers = new Map<TestPlatform, Future<BrowserManager>>(); |
| 112 '/packages/test/src/runner/browser/static/'); | |
| 113 } | |
| 114 | |
| 115 _browser = new Chrome(hostUrl.replace(queryParameters: { | |
| 116 'managerUrl': webSocketUrl.toString() | |
| 117 })); | |
| 118 | |
| 119 // TODO(nweiz): Gracefully handle the browser being killed before the | |
| 120 // tests complete. | |
| 121 _browser.onExit.catchError((error, stackTrace) { | |
| 122 if (_browserManagerCompleter.isCompleted) return; | |
| 123 _browserManagerCompleter.completeError(error, stackTrace); | |
| 124 }); | |
| 125 } | |
| 126 return _browserManagerCompleter.future; | |
| 127 } | |
| 128 Completer<BrowserManager> _browserManagerCompleter; | |
| 129 | 104 |
| 130 BrowserServer._(this._packageRoot, Uri pubServeUrl, bool color) | 105 BrowserServer._(this._packageRoot, Uri pubServeUrl, bool color) |
| 131 : _pubServeUrl = pubServeUrl, | 106 : _pubServeUrl = pubServeUrl, |
| 132 _compiledDir = pubServeUrl == null ? createTempDir() : null, | 107 _compiledDir = pubServeUrl == null ? createTempDir() : null, |
| 133 _http = pubServeUrl == null ? null : new HttpClient(), | 108 _http = pubServeUrl == null ? null : new HttpClient(), |
| 134 _compilers = new CompilerPool(color: color); | 109 _compilers = new CompilerPool(color: color); |
| 135 | 110 |
| 136 /// Starts the underlying server. | 111 /// Starts the underlying server. |
| 137 Future _load() { | 112 Future _load() { |
| 138 var cascade = new shelf.Cascade() | 113 var cascade = new shelf.Cascade() |
| 139 .add(_webSocketHandler.handler); | 114 .add(_webSocketHandler.handler); |
| 140 | 115 |
| 141 if (_pubServeUrl == null) { | 116 if (_pubServeUrl == null) { |
| 142 var staticPath = p.join(libDir(packageRoot: _packageRoot), | 117 var staticPath = p.join(libDir(packageRoot: _packageRoot), |
| 143 'src/runner/browser/static'); | 118 'src/runner/browser/static'); |
| 144 cascade = cascade | 119 cascade = cascade |
| 145 .add(createStaticHandler(staticPath, defaultDocument: 'index.html')) | 120 .add(createStaticHandler(staticPath, defaultDocument: 'index.html')) |
| 146 .add(createStaticHandler(_compiledDir, | 121 .add(createStaticHandler(_compiledDir, |
| 147 defaultDocument: 'index.html')); | 122 defaultDocument: 'index.html')); |
| 148 } | 123 } |
| 149 | 124 |
| 150 return shelf_io.serve(cascade.handler, 'localhost', 0).then((server) { | 125 return shelf_io.serve(cascade.handler, 'localhost', 0).then((server) { |
| 151 _server = server; | 126 _server = server; |
| 152 }); | 127 }); |
| 153 } | 128 } |
| 154 | 129 |
| 155 /// Loads the test suite at [path]. | 130 /// Loads the test suite at [path] on the browser [browser]. |
| 156 /// | 131 /// |
| 157 /// This will start a browser to load the suite if one isn't already running. | 132 /// This will start a browser to load the suite if one isn't already running. |
| 158 Future<Suite> loadSuite(String path) { | 133 /// Throws an [ArgumentError] if [browser] isn't a browser platform. |
| 134 Future<Suite> loadSuite(String path, TestPlatform browser) { |
| 135 if (!browser.isBrowser) { |
| 136 throw new ArgumentError("$browser is not a browser."); |
| 137 } |
| 138 |
| 159 return new Future.sync(() { | 139 return new Future.sync(() { |
| 160 if (_pubServeUrl != null) { | 140 if (_pubServeUrl != null) { |
| 161 var suitePrefix = p.withoutExtension(p.relative(path, from: 'test')) + | 141 var suitePrefix = p.withoutExtension(p.relative(path, from: 'test')) + |
| 162 '.browser_test'; | 142 '.browser_test'; |
| 163 var jsUrl = _pubServeUrl.resolve('$suitePrefix.dart.js'); | 143 var jsUrl = _pubServeUrl.resolve('$suitePrefix.dart.js'); |
| 164 return _pubServeSuite(path, jsUrl) | 144 return _pubServeSuite(path, jsUrl) |
| 165 .then((_) => _pubServeUrl.resolve('$suitePrefix.html')); | 145 .then((_) => _pubServeUrl.resolve('$suitePrefix.html')); |
| 166 } else { | 146 } else { |
| 167 return _compileSuite(path).then((dir) { | 147 return _compileSuite(path).then((dir) { |
| 168 if (_closed) return null; | 148 if (_closed) return null; |
| 169 | 149 |
| 170 // Add a trailing slash because at least on Chrome, the iframe's | 150 // Add a trailing slash because at least on Chrome, the iframe's |
| 171 // window.location.href will do so automatically, and if that differs | 151 // window.location.href will do so automatically, and if that differs |
| 172 // from the original URL communication will fail. | 152 // from the original URL communication will fail. |
| 173 return url.resolve( | 153 return url.resolve( |
| 174 "/" + p.toUri(p.relative(dir, from: _compiledDir)).path + "/"); | 154 "/" + p.toUri(p.relative(dir, from: _compiledDir)).path + "/"); |
| 175 }); | 155 }); |
| 176 } | 156 } |
| 177 }).then((suiteUrl) { | 157 }).then((suiteUrl) { |
| 178 if (_closed) return null; | 158 if (_closed) return null; |
| 179 | 159 |
| 180 // TODO(nweiz): Don't start the browser until all the suites are compiled. | 160 // TODO(nweiz): Don't start the browser until all the suites are compiled. |
| 181 return _browserManager.then((browserManager) { | 161 return _browserManagerFor(browser).then((browserManager) { |
| 182 if (_closed) return null; | 162 if (_closed) return null; |
| 183 return browserManager.loadSuite(path, suiteUrl); | 163 return browserManager.loadSuite(path, suiteUrl); |
| 184 }); | 164 }); |
| 185 }); | 165 }); |
| 186 } | 166 } |
| 187 | 167 |
| 188 /// Loads a test suite at [path] from the `pub serve` URL [jsUrl]. | 168 /// Loads a test suite at [path] from the `pub serve` URL [jsUrl]. |
| 189 /// | 169 /// |
| 190 /// This ensures that only one suite is loaded at a time, and that any errors | 170 /// This ensures that only one suite is loaded at a time, and that any errors |
| 191 /// are exposed as [LoadException]s. | 171 /// are exposed as [LoadException]s. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 <head> | 219 <head> |
| 240 <title>${HTML_ESCAPE.convert(dartPath)} Test</title> | 220 <title>${HTML_ESCAPE.convert(dartPath)} Test</title> |
| 241 <script src="${HTML_ESCAPE.convert(p.basename(jsPath))}"></script> | 221 <script src="${HTML_ESCAPE.convert(p.basename(jsPath))}"></script> |
| 242 </head> | 222 </head> |
| 243 </html> | 223 </html> |
| 244 '''); | 224 '''); |
| 245 return dir; | 225 return dir; |
| 246 }); | 226 }); |
| 247 } | 227 } |
| 248 | 228 |
| 229 /// Returns the [BrowserManager] for [platform], which should be a browser. |
| 230 /// |
| 231 /// If no browser manager is running yet, starts one. |
| 232 Future<BrowserManager> _browserManagerFor(TestPlatform platform) { |
| 233 var manager = _browserManagers[platform]; |
| 234 if (manager != null) return manager; |
| 235 |
| 236 var completer = new Completer(); |
| 237 _browserManagers[platform] = completer.future; |
| 238 var path = _webSocketHandler.create(webSocketHandler((webSocket) { |
| 239 completer.complete(new BrowserManager(webSocket)); |
| 240 })); |
| 241 |
| 242 var webSocketUrl = url.replace(scheme: 'ws', path: '/$path'); |
| 243 |
| 244 var hostUrl = url; |
| 245 if (_pubServeUrl != null) { |
| 246 hostUrl = _pubServeUrl.resolve( |
| 247 '/packages/test/src/runner/browser/static/'); |
| 248 } |
| 249 |
| 250 var browser = _newBrowser(hostUrl.replace(queryParameters: { |
| 251 'managerUrl': webSocketUrl.toString() |
| 252 }), platform); |
| 253 _browsers[platform] = browser; |
| 254 |
| 255 // TODO(nweiz): Gracefully handle the browser being killed before the |
| 256 // tests complete. |
| 257 browser.onExit.catchError((error, stackTrace) { |
| 258 if (completer.isCompleted) return; |
| 259 completer.completeError(error, stackTrace); |
| 260 }); |
| 261 |
| 262 return completer.future; |
| 263 } |
| 264 |
| 265 /// Starts the browser identified by [browser] and has it load [url]. |
| 266 Browser _newBrowser(Uri url, TestPlatform browser) { |
| 267 switch (browser) { |
| 268 case TestPlatform.chrome: return new Chrome(url); |
| 269 case TestPlatform.firefox: return new Firefox(url); |
| 270 default: |
| 271 throw new ArgumentError("$browser is not a browser."); |
| 272 } |
| 273 } |
| 274 |
| 249 /// Closes the server and releases all its resources. | 275 /// Closes the server and releases all its resources. |
| 250 /// | 276 /// |
| 251 /// Returns a [Future] that completes once the server is closed and its | 277 /// Returns a [Future] that completes once the server is closed and its |
| 252 /// resources have been fully released. | 278 /// resources have been fully released. |
| 253 Future close() { | 279 Future close() { |
| 254 if (_closeCompleter != null) return _closeCompleter.future; | 280 if (_closeCompleter != null) return _closeCompleter.future; |
| 255 _closeCompleter = new Completer(); | 281 _closeCompleter = new Completer(); |
| 256 | 282 |
| 257 return Future.wait([ | 283 return Future.wait([ |
| 258 _server.close(), | 284 _server.close(), |
| 259 _compilers.close() | 285 _compilers.close() |
| 260 ]).then((_) { | 286 ]).then((_) { |
| 261 if (_browserManagerCompleter == null) return null; | 287 if (_browserManagers.isEmpty) return null; |
| 262 return _browserManager.then((_) => _browser.close()); | 288 return Future.wait(_browserManagers.keys.map((platform) { |
| 289 return _browserManagers[platform] |
| 290 .then((_) => _browsers[platform].close()); |
| 291 })); |
| 263 }).then((_) { | 292 }).then((_) { |
| 264 if (_pubServeUrl == null) { | 293 if (_pubServeUrl == null) { |
| 265 new Directory(_compiledDir).deleteSync(recursive: true); | 294 new Directory(_compiledDir).deleteSync(recursive: true); |
| 266 } else { | 295 } else { |
| 267 _http.close(); | 296 _http.close(); |
| 268 } | 297 } |
| 269 | 298 |
| 270 _closeCompleter.complete(); | 299 _closeCompleter.complete(); |
| 271 }).catchError(_closeCompleter.completeError); | 300 }).catchError(_closeCompleter.completeError); |
| 272 } | 301 } |
| 273 } | 302 } |
| OLD | NEW |