Chromium Code Reviews| Index: sdk/lib/_internal/pub/test/serve/utils.dart |
| diff --git a/sdk/lib/_internal/pub/test/serve/utils.dart b/sdk/lib/_internal/pub/test/serve/utils.dart |
| index 93ffbff2ff5b3ead988d3da803b5a84d98570a61..2bd31f2394b7dd2bfbd60e3faccf29220aa28dbe 100644 |
| --- a/sdk/lib/_internal/pub/test/serve/utils.dart |
| +++ b/sdk/lib/_internal/pub/test/serve/utils.dart |
| @@ -5,6 +5,8 @@ |
| library pub_tests; |
| import 'dart:async'; |
| +import 'dart:convert'; |
| +import 'dart:io'; |
| import 'package:http/http.dart' as http; |
| import 'package:scheduled_test/scheduled_process.dart'; |
| @@ -18,6 +20,11 @@ ScheduledProcess _pubServer; |
| /// The ephemeral port assigned to the running server. |
| int _port; |
| +/// The web socket connection to the running pub process, or `null` if no |
| +/// connection has been made. |
| +WebSocket _webSocket; |
| +Stream _webSocketBroadcastStream; |
| + |
| /// The code for a transformer that renames ".txt" files to ".out" and adds a |
| /// ".out" suffix. |
| const REWRITE_TRANSFORMER = """ |
| @@ -107,6 +114,14 @@ ScheduledProcess startPubServe({bool shouldGetFirst: false, |
| _pubServer = startPub(args: args); |
| + currentSchedule.onComplete.schedule(() { |
| + if (_webSocket != null) { |
| + _webSocket.close(); |
| + _webSocket = null; |
| + _webSocketBroadcastStream = null; |
| + } |
| + }); |
| + |
| if (shouldGetFirst) { |
| expect(_pubServer.nextLine(), |
| completion(anyOf( |
| @@ -183,3 +198,33 @@ void waitForBuildSuccess() { |
| schedule(nextLine); |
| } |
| + |
| +/// Schedules opening a web socket connection to the currently running pub |
| +/// serve. |
| +Future _ensureWebSocket() { |
| + // Use the existing one if already connected. |
| + if (_webSocket != null) return new Future.value(); |
| + |
| + // Server should already be running. |
| + assert(_pubServer != null); |
| + assert(_port != null); |
| + |
| + return WebSocket.connect("ws://127.0.0.1:$_port").then((socket) { |
| + _webSocket = socket; |
| + // TODO(rnystrom): Works around #13913. |
| + _webSocketBroadcastStream = _webSocket.asBroadcastStream(); |
| + }); |
| +} |
| + |
| +/// Sends [request] (an arbitrary JSON object) to the running pub serve's web |
| +/// socket connection, waits for a reply, then verifies that the reply matches |
| +/// [expectation]. |
| +void webSocketShouldReply(request, expectation, {bool encodeRequest: true}) { |
|
nweiz
2013/10/09 19:16:37
Document the [encodeRequest] parameter.
Bob Nystrom
2013/10/11 20:50:10
Done.
|
| + schedule(() => _ensureWebSocket().then((_) { |
| + if (encodeRequest) request = JSON.encode(request); |
| + _webSocket.add(request); |
| + return _webSocketBroadcastStream.first.then((value) { |
| + expect(JSON.decode(value), expectation); |
| + }); |
| + }), "web socket should reply"); |
|
nweiz
2013/10/09 19:16:37
I'd prefer a more descriptive description here. At
Bob Nystrom
2013/10/11 20:50:10
Done.
|
| +} |