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.browser_manager; | 5 library test.runner.browser.browser_manager; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 | 9 |
10 import 'package:http_parser/http_parser.dart'; | 10 import 'package:http_parser/http_parser.dart'; |
| 11 import 'package:pool/pool.dart'; |
11 | 12 |
12 import '../../backend/metadata.dart'; | 13 import '../../backend/metadata.dart'; |
13 import '../../backend/suite.dart'; | 14 import '../../backend/suite.dart'; |
14 import '../../backend/test_platform.dart'; | 15 import '../../backend/test_platform.dart'; |
15 import '../../util/multi_channel.dart'; | 16 import '../../util/multi_channel.dart'; |
16 import '../../util/remote_exception.dart'; | 17 import '../../util/remote_exception.dart'; |
17 import '../../utils.dart'; | 18 import '../../utils.dart'; |
18 import '../load_exception.dart'; | 19 import '../load_exception.dart'; |
19 import 'iframe_test.dart'; | 20 import 'iframe_test.dart'; |
20 | 21 |
21 /// A class that manages the connection to a single running browser. | 22 /// A class that manages the connection to a single running browser. |
22 /// | 23 /// |
23 /// This is in charge of telling the browser which test suites to load and | 24 /// This is in charge of telling the browser which test suites to load and |
24 /// converting its responses into [Suite] objects. | 25 /// converting its responses into [Suite] objects. |
25 class BrowserManager { | 26 class BrowserManager { |
26 /// The browser that this is managing. | 27 /// The browser that this is managing. |
27 final TestPlatform browser; | 28 final TestPlatform browser; |
28 | 29 |
29 /// The channel used to communicate with the browser. | 30 /// The channel used to communicate with the browser. |
30 /// | 31 /// |
31 /// This is connected to a page running `static/host.dart`. | 32 /// This is connected to a page running `static/host.dart`. |
32 final MultiChannel _channel; | 33 final MultiChannel _channel; |
33 | 34 |
| 35 /// A pool that ensures that limits the number of initial connections the |
| 36 /// manager will wait for at once. |
| 37 /// |
| 38 /// This isn't the *total* number of connections; any number of iframes may be |
| 39 /// loaded in the same browser. However, the browser can only load so many at |
| 40 /// once, and we want a timeout in case they fail so we only wait for so many |
| 41 /// at once. |
| 42 final _pool = new Pool(8); |
| 43 |
34 /// Creates a new BrowserManager that communicates with [browser] over | 44 /// Creates a new BrowserManager that communicates with [browser] over |
35 /// [webSocket]. | 45 /// [webSocket]. |
36 BrowserManager(this.browser, CompatibleWebSocket webSocket) | 46 BrowserManager(this.browser, CompatibleWebSocket webSocket) |
37 : _channel = new MultiChannel( | 47 : _channel = new MultiChannel( |
38 webSocket.map(JSON.decode), | 48 webSocket.map(JSON.decode), |
39 mapSink(webSocket, JSON.encode)); | 49 mapSink(webSocket, JSON.encode)); |
40 | 50 |
41 /// Tells the browser the load a test suite from the URL [url]. | 51 /// Tells the browser the load a test suite from the URL [url]. |
42 /// | 52 /// |
43 /// [url] should be an HTML page with a reference to the JS-compiled test | 53 /// [url] should be an HTML page with a reference to the JS-compiled test |
44 /// suite. [path] is the path of the original test suite file, which is used | 54 /// suite. [path] is the path of the original test suite file, which is used |
45 /// for reporting. [metadata] is the parsed metadata for the test suite. | 55 /// for reporting. [metadata] is the parsed metadata for the test suite. |
46 Future<Suite> loadSuite(String path, Uri url, Metadata metadata) { | 56 Future<Suite> loadSuite(String path, Uri url, Metadata metadata) { |
47 url = url.replace(fragment: Uri.encodeFull(JSON.encode({ | 57 url = url.replace(fragment: Uri.encodeFull(JSON.encode({ |
48 "metadata": metadata.serialize(), | 58 "metadata": metadata.serialize(), |
49 "browser": browser.identifier | 59 "browser": browser.identifier |
50 }))); | 60 }))); |
51 | 61 |
52 var suiteChannel = _channel.virtualChannel(); | |
53 _channel.sink.add({ | |
54 "command": "loadSuite", | |
55 "url": url.toString(), | |
56 "channel": suiteChannel.id | |
57 }); | |
58 | |
59 // Create a nested MultiChannel because the iframe will be using a channel | |
60 // wrapped within the host's channel. | |
61 suiteChannel = new MultiChannel(suiteChannel.stream, suiteChannel.sink); | |
62 | |
63 // The stream may close before emitting a value if the browser is killed | 62 // The stream may close before emitting a value if the browser is killed |
64 // prematurely (e.g. via Control-C). | 63 // prematurely (e.g. via Control-C). |
65 return maybeFirst(suiteChannel.stream) | 64 var suiteChannel = _channel.virtualChannel(); |
66 .timeout(new Duration(seconds: 7), onTimeout: () { | 65 return _pool.withResource(() { |
67 throw new LoadException( | 66 _channel.sink.add({ |
68 path, | 67 "command": "loadSuite", |
69 "Timed out waiting for the test suite to connect on " | 68 "url": url.toString(), |
70 "${browser.name}."); | 69 "channel": suiteChannel.id |
| 70 }); |
| 71 |
| 72 // Create a nested MultiChannel because the iframe will be using a channel |
| 73 // wrapped within the host's channel. |
| 74 suiteChannel = new MultiChannel(suiteChannel.stream, suiteChannel.sink); |
| 75 |
| 76 return maybeFirst(suiteChannel.stream) |
| 77 .timeout(new Duration(seconds: 15), onTimeout: () { |
| 78 throw new LoadException( |
| 79 path, |
| 80 "Timed out waiting for the test suite to connect on " |
| 81 "${browser.name}."); |
| 82 }); |
71 }).then((response) { | 83 }).then((response) { |
72 if (response == null) return null; | 84 if (response == null) return null; |
73 | 85 |
74 if (response["type"] == "loadException") { | 86 if (response["type"] == "loadException") { |
75 return new Future.error(new LoadException(path, response["message"])); | 87 return new Future.error(new LoadException(path, response["message"])); |
76 } else if (response["type"] == "error") { | 88 } else if (response["type"] == "error") { |
77 var asyncError = RemoteException.deserialize(response["error"]); | 89 var asyncError = RemoteException.deserialize(response["error"]); |
78 return new Future.error( | 90 return new Future.error( |
79 new LoadException(path, asyncError.error), | 91 new LoadException(path, asyncError.error), |
80 asyncError.stackTrace); | 92 asyncError.stackTrace); |
81 } | 93 } |
82 | 94 |
83 return new Suite(response["tests"].map((test) { | 95 return new Suite(response["tests"].map((test) { |
84 var testMetadata = new Metadata.deserialize(test['metadata']); | 96 var testMetadata = new Metadata.deserialize(test['metadata']); |
85 var testChannel = suiteChannel.virtualChannel(test['channel']); | 97 var testChannel = suiteChannel.virtualChannel(test['channel']); |
86 return new IframeTest(test['name'], testMetadata, testChannel); | 98 return new IframeTest(test['name'], testMetadata, testChannel); |
87 }), metadata: metadata, path: path); | 99 }), metadata: metadata, path: path); |
88 }); | 100 }); |
89 } | 101 } |
90 } | 102 } |
OLD | NEW |