OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS d.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 pub_tests; | 5 library pub_tests; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 import 'dart:io'; |
8 | 10 |
9 import 'package:http/http.dart' as http; | 11 import 'package:http/http.dart' as http; |
10 import 'package:scheduled_test/scheduled_process.dart'; | 12 import 'package:scheduled_test/scheduled_process.dart'; |
11 import 'package:scheduled_test/scheduled_test.dart'; | 13 import 'package:scheduled_test/scheduled_test.dart'; |
12 | 14 |
13 import '../test_pub.dart'; | 15 import '../test_pub.dart'; |
14 | 16 |
15 /// The pub process running "pub serve". | 17 /// The pub process running "pub serve". |
16 ScheduledProcess _pubServer; | 18 ScheduledProcess _pubServer; |
17 | 19 |
18 /// The ephemeral port assigned to the running server. | 20 /// The ephemeral port assigned to the running server. |
19 int _port; | 21 int _port; |
20 | 22 |
| 23 /// The web socket connection to the running pub process, or `null` if no |
| 24 /// connection has been made. |
| 25 WebSocket _webSocket; |
| 26 Stream _webSocketBroadcastStream; |
| 27 |
21 /// The code for a transformer that renames ".txt" files to ".out" and adds a | 28 /// The code for a transformer that renames ".txt" files to ".out" and adds a |
22 /// ".out" suffix. | 29 /// ".out" suffix. |
23 const REWRITE_TRANSFORMER = """ | 30 const REWRITE_TRANSFORMER = """ |
24 import 'dart:async'; | 31 import 'dart:async'; |
25 | 32 |
26 import 'package:barback/barback.dart'; | 33 import 'package:barback/barback.dart'; |
27 | 34 |
28 class RewriteTransformer extends Transformer { | 35 class RewriteTransformer extends Transformer { |
29 RewriteTransformer.asPlugin(); | 36 RewriteTransformer.asPlugin(); |
30 | 37 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 if (!dart2js) args.add("--no-dart2js"); | 107 if (!dart2js) args.add("--no-dart2js"); |
101 | 108 |
102 // Dart2js can take a long time to compile dart code, so we increase the | 109 // Dart2js can take a long time to compile dart code, so we increase the |
103 // timeout to cope with that. | 110 // timeout to cope with that. |
104 if (dart2js) { | 111 if (dart2js) { |
105 currentSchedule.timeout = new Duration(seconds: 15); | 112 currentSchedule.timeout = new Duration(seconds: 15); |
106 } | 113 } |
107 | 114 |
108 _pubServer = startPub(args: args); | 115 _pubServer = startPub(args: args); |
109 | 116 |
| 117 currentSchedule.onComplete.schedule(() { |
| 118 if (_webSocket != null) { |
| 119 _webSocket.close(); |
| 120 _webSocket = null; |
| 121 _webSocketBroadcastStream = null; |
| 122 } |
| 123 }); |
| 124 |
110 if (shouldGetFirst) { | 125 if (shouldGetFirst) { |
111 expect(_pubServer.nextLine(), | 126 expect(_pubServer.nextLine(), |
112 completion(anyOf( | 127 completion(anyOf( |
113 startsWith("Your pubspec has changed"), | 128 startsWith("Your pubspec has changed"), |
114 startsWith("You don't have a lockfile")))); | 129 startsWith("You don't have a lockfile")))); |
115 expect(_pubServer.nextLine(), | 130 expect(_pubServer.nextLine(), |
116 completion(startsWith("Resolving dependencies..."))); | 131 completion(startsWith("Resolving dependencies..."))); |
117 expect(_pubServer.nextLine(), | 132 expect(_pubServer.nextLine(), |
118 completion(equals("Got dependencies!"))); | 133 completion(equals("Got dependencies!"))); |
119 } | 134 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 return _pubServer.nextLine().then((line) { | 191 return _pubServer.nextLine().then((line) { |
177 if (line.contains("successfully")) return; | 192 if (line.contains("successfully")) return; |
178 | 193 |
179 // This line wasn't it, so ignore it and keep trying. | 194 // This line wasn't it, so ignore it and keep trying. |
180 return nextLine(); | 195 return nextLine(); |
181 }); | 196 }); |
182 } | 197 } |
183 | 198 |
184 schedule(nextLine); | 199 schedule(nextLine); |
185 } | 200 } |
| 201 |
| 202 /// Schedules opening a web socket connection to the currently running pub |
| 203 /// serve. |
| 204 Future _ensureWebSocket() { |
| 205 // Use the existing one if already connected. |
| 206 if (_webSocket != null) return new Future.value(); |
| 207 |
| 208 // Server should already be running. |
| 209 assert(_pubServer != null); |
| 210 assert(_port != null); |
| 211 |
| 212 return WebSocket.connect("ws://127.0.0.1:$_port").then((socket) { |
| 213 _webSocket = socket; |
| 214 // TODO(rnystrom): Works around #13913. |
| 215 _webSocketBroadcastStream = _webSocket.asBroadcastStream(); |
| 216 }); |
| 217 } |
| 218 |
| 219 /// Sends [request] (an arbitrary JSON object) to the running pub serve's web |
| 220 /// socket connection, waits for a reply, then verifies that the reply matches |
| 221 /// [expectation]. |
| 222 /// |
| 223 /// If [encodeRequest] is `false`, then [request] will be sent as-is over the |
| 224 /// socket. It omitted, request is JSON encoded to a string first. |
| 225 void webSocketShouldReply(request, expectation, {bool encodeRequest: true}) { |
| 226 schedule(() => _ensureWebSocket().then((_) { |
| 227 if (encodeRequest) request = JSON.encode(request); |
| 228 _webSocket.add(request); |
| 229 return _webSocketBroadcastStream.first.then((value) { |
| 230 expect(JSON.decode(value), expectation); |
| 231 }); |
| 232 }), "send $request to web socket and expect reply that $expectation"); |
| 233 } |
OLD | NEW |