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

Side by Side Diff: sdk/lib/_internal/pub/test/serve/utils.dart

Issue 164773003: Change ScheduledProcess's output streams over to be ScheduledStreams. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
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'; 8 import 'dart:convert';
9 import 'dart:io'; 9 import 'dart:io';
10 10
11 import 'package:http/http.dart' as http; 11 import 'package:http/http.dart' as http;
12 import 'package:path/path.dart' as p; 12 import 'package:path/path.dart' as p;
13 import 'package:scheduled_test/scheduled_process.dart'; 13 import 'package:scheduled_test/scheduled_process.dart';
14 import 'package:scheduled_test/scheduled_stream.dart';
14 import 'package:scheduled_test/scheduled_test.dart'; 15 import 'package:scheduled_test/scheduled_test.dart';
15 16
16 import '../test_pub.dart'; 17 import '../test_pub.dart';
17 18
18 /// The pub process running "pub serve". 19 /// The pub process running "pub serve".
19 ScheduledProcess _pubServer; 20 ScheduledProcess _pubServer;
20 21
21 /// The ephemeral port assigned to the running server. 22 /// The ephemeral port assigned to the running server.
22 int _port; 23 int _port;
23 24
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 // 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
109 // timeout to cope with that. 110 // timeout to cope with that.
110 currentSchedule.timeout *= 1.5; 111 currentSchedule.timeout *= 1.5;
111 112
112 return startPub(args: pubArgs); 113 return startPub(args: pubArgs);
113 } 114 }
114 115
115 /// Schedules starting the "pub serve" process and records its port number for 116 /// Schedules starting the "pub serve" process and records its port number for
116 /// future requests. 117 /// future requests.
117 /// 118 ///
118 /// If [shouldGetFirst] is `true`, validates that pub get is run first. In that 119 /// If [shouldGetFirst] is `true`, validates that pub get is run first.
119 /// case, you can also pass [numDownloads] to specify how many packages should
120 /// be downloaded during the get.
121 /// 120 ///
122 /// Returns the `pub serve` process. 121 /// Returns the `pub serve` process.
123 ScheduledProcess pubServe({bool shouldGetFirst: false, 122 ScheduledProcess pubServe({bool shouldGetFirst: false, Iterable<String> args}) {
124 Iterable<String> args, int numDownloads: 0}) {
125 _pubServer = startPubServe(args); 123 _pubServer = startPubServe(args);
126 124
127 currentSchedule.onComplete.schedule(() { 125 currentSchedule.onComplete.schedule(() {
128 if (_webSocket != null) { 126 if (_webSocket != null) {
129 _webSocket.close(); 127 _webSocket.close();
130 _webSocket = null; 128 _webSocket = null;
131 _webSocketBroadcastStream = null; 129 _webSocketBroadcastStream = null;
132 } 130 }
133 }); 131 });
134 132
135 if (shouldGetFirst) { 133 if (shouldGetFirst) {
136 expect(_pubServer.nextLine(), 134 _pubServer.stdout.expect(consumeThrough("Got dependencies!"));
137 completion(anyOf(
138 startsWith("Your pubspec has changed"),
139 startsWith("You don't have a lockfile"),
140 startsWith("You are missing some dependencies"))));
141 expect(_pubServer.nextLine(),
142 completion(startsWith("Resolving dependencies...")));
143
144 for (var i = 0; i < numDownloads; i++) {
145 expect(_pubServer.nextLine(),
146 completion(startsWith("Downloading")));
147 }
148
149 expect(_pubServer.nextLine(),
150 completion(equals("Got dependencies!")));
151 } 135 }
152 136
153 expect(_pubServer.nextLine().then(_parsePort), completes); 137 expect(schedule(() => _pubServer.stdout.next()).then(_parsePort), completes);
154 return _pubServer; 138 return _pubServer;
155 } 139 }
156 140
157 /// Parses the port number from the "Serving blah on 127.0.0.1:1234" line 141 /// Parses the port number from the "Serving blah on 127.0.0.1:1234" line
158 /// printed by pub serve. 142 /// printed by pub serve.
159 void _parsePort(String line) { 143 void _parsePort(String line) {
160 var match = new RegExp(r"127\.0\.0\.1:(\d+)").firstMatch(line); 144 var match = new RegExp(r"127\.0\.0\.1:(\d+)").firstMatch(line);
161 assert(match != null); 145 assert(match != null);
162 _port = int.parse(match[1]); 146 _port = int.parse(match[1]);
163 } 147 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 expect(response.statusCode, equals(405)); 200 expect(response.statusCode, equals(405));
217 }); 201 });
218 }, "request $urlPath"); 202 }, "request $urlPath");
219 } 203 }
220 204
221 /// Reads lines from pub serve's stdout until it prints the build success 205 /// Reads lines from pub serve's stdout until it prints the build success
222 /// message. 206 /// message.
223 /// 207 ///
224 /// The schedule will not proceed until the output is found. If not found, it 208 /// The schedule will not proceed until the output is found. If not found, it
225 /// will eventually time out. 209 /// will eventually time out.
226 void waitForBuildSuccess() { 210 void waitForBuildSuccess() =>
227 nextLine() { 211 _pubServer.stdout.expect(consumeThrough(contains("successfully")));
228 return _pubServer.nextLine().then((line) {
229 if (line.contains("successfully")) return null;
230
231 // This line wasn't it, so ignore it and keep trying.
232 return nextLine();
233 });
234 }
235
236 schedule(nextLine);
237 }
238 212
239 /// Schedules opening a web socket connection to the currently running pub 213 /// Schedules opening a web socket connection to the currently running pub
240 /// serve. 214 /// serve.
241 Future _ensureWebSocket() { 215 Future _ensureWebSocket() {
242 // Use the existing one if already connected. 216 // Use the existing one if already connected.
243 if (_webSocket != null) return new Future.value(); 217 if (_webSocket != null) return new Future.value();
244 218
245 // Server should already be running. 219 // Server should already be running.
246 assert(_pubServer != null); 220 assert(_pubServer != null);
247 assert(_port != null); 221 assert(_port != null);
(...skipping 13 matching lines...) Expand all
261 /// socket. It omitted, request is JSON encoded to a string first. 235 /// socket. It omitted, request is JSON encoded to a string first.
262 void webSocketShouldReply(request, expectation, {bool encodeRequest: true}) { 236 void webSocketShouldReply(request, expectation, {bool encodeRequest: true}) {
263 schedule(() => _ensureWebSocket().then((_) { 237 schedule(() => _ensureWebSocket().then((_) {
264 if (encodeRequest) request = JSON.encode(request); 238 if (encodeRequest) request = JSON.encode(request);
265 _webSocket.add(request); 239 _webSocket.add(request);
266 return _webSocketBroadcastStream.first.then((value) { 240 return _webSocketBroadcastStream.first.then((value) {
267 expect(JSON.decode(value), expectation); 241 expect(JSON.decode(value), expectation);
268 }); 242 });
269 }), "send $request to web socket and expect reply that $expectation"); 243 }), "send $request to web socket and expect reply that $expectation");
270 } 244 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698