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

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

Issue 221173007: Properly handle web socket requests to half-initialized servers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 8 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
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 // Use the existing one if already connected. 286 // Use the existing one if already connected.
287 if (_webSocket != null) return new Future.value(); 287 if (_webSocket != null) return new Future.value();
288 288
289 // Server should already be running. 289 // Server should already be running.
290 expect(_pubServer, isNotNull); 290 expect(_pubServer, isNotNull);
291 expect(_adminPort, isNotNull); 291 expect(_adminPort, isNotNull);
292 292
293 return WebSocket.connect("ws://127.0.0.1:$_adminPort").then((socket) { 293 return WebSocket.connect("ws://127.0.0.1:$_adminPort").then((socket) {
294 _webSocket = socket; 294 _webSocket = socket;
295 // TODO(rnystrom): Works around #13913. 295 // TODO(rnystrom): Works around #13913.
296 _webSocketBroadcastStream = _webSocket.asBroadcastStream(); 296 _webSocketBroadcastStream = _webSocket.map(JSON.decode).asBroadcastStream();
297 }); 297 });
298 } 298 }
299 299
300 /// Sends a JSON RPC 2.0 request to the running pub serve's web socket 300 /// Sends a JSON RPC 2.0 request to the running pub serve's web socket
301 /// connection.
302 ///
303 /// This calls a method named [method] with the given [params]. [params] may
304 /// contain Futures, in which case this will wait until they've completed before
305 /// sending the request.
306 ///
307 /// This schedules the request, but doesn't block the schedule on the response.
308 /// It returns the response as a [Future].
309 Future<Map> webSocketRequest(String method, Map params) {
310 var completer = new Completer();
311 schedule(() {
312 return Future.wait([
313 _ensureWebSocket(),
314 awaitObject(params),
315 ]).then((results) {
316 var resolvedParams = results[1];
317 chainToCompleter(
318 currentSchedule.wrapFuture(_jsonRpcRequest(method, resolvedParams)),
319 completer);
320 });
321 }, "send $method with $params to web socket");
322 return completer.future;
323 }
324
325 /// Sends a JSON RPC 2.0 request to the running pub serve's web socket
301 /// connection, waits for a reply, then verifies the result. 326 /// connection, waits for a reply, then verifies the result.
302 /// 327 ///
303 /// This calls a method named [method] with the given [params]. [params] may 328 /// This calls a method named [method] with the given [params]. [params] may
304 /// contain Futures, in which case this will wait until they've completed before 329 /// contain Futures, in which case this will wait until they've completed before
305 /// sending the request. 330 /// sending the request.
306 /// 331 ///
307 /// The result is validated using [result], which may be a [Matcher] or a [Map] 332 /// The result is validated using [result], which may be a [Matcher] or a [Map]
308 /// containing [Matcher]s and [Future]s. This will wait until any futures are 333 /// containing [Matcher]s and [Future]s. This will wait until any futures are
309 /// completed before sending the request. 334 /// completed before sending the request.
310 /// 335 ///
311 /// Returns a [Future] that completes to the call's result. 336 /// Returns a [Future] that completes to the call's result.
312 Future<Map> expectWebSocketResult(String method, Map params, result) { 337 Future<Map> expectWebSocketResult(String method, Map params, result) {
313 return schedule(() { 338 return schedule(() {
314 return Future.wait([ 339 return Future.wait([
315 _ensureWebSocket(), 340 webSocketRequest(method, params),
316 awaitObject(params),
317 awaitObject(result) 341 awaitObject(result)
318 ]).then((results) { 342 ]).then((results) {
319 var resolvedParams = results[1]; 343 var response = results[0];
320 var resolvedResult = results[2]; 344 var resolvedResult = results[1];
321 345 expect(response["result"], resolvedResult);
322 return _jsonRpcRequest(method, resolvedParams).then((response) { 346 return response["result"];
323 expect(response["result"], resolvedResult);
324 return response["result"];
325 });
326 }); 347 });
327 }, "send $method with $params to web socket and expect $result"); 348 }, "send $method with $params to web socket and expect $result");
328 } 349 }
329 350
330 /// Sends a JSON RPC 2.0 request to the running pub serve's web socket 351 /// Sends a JSON RPC 2.0 request to the running pub serve's web socket
331 /// connection, waits for a reply, then verifies the error response. 352 /// connection, waits for a reply, then verifies the error response.
332 /// 353 ///
333 /// This calls a method named [method] with the given [params]. [params] may 354 /// This calls a method named [method] with the given [params]. [params] may
334 /// contain Futures, in which case this will wait until they've completed before 355 /// contain Futures, in which case this will wait until they've completed before
335 /// sending the request. 356 /// sending the request.
336 /// 357 ///
337 /// The error response is validated using [errorCode] and [errorMessage]. Both 358 /// The error response is validated using [errorCode] and [errorMessage]. Both
338 /// of these must be provided. The error code is checked against [errorCode] and 359 /// of these must be provided. The error code is checked against [errorCode] and
339 /// the error message is checked against [errorMessage]. Either of these may be 360 /// the error message is checked against [errorMessage]. Either of these may be
340 /// matchers. 361 /// matchers.
341 /// 362 ///
342 /// Returns a [Future] that completes to the error's [data] field. 363 /// Returns a [Future] that completes to the error's [data] field.
343 Future expectWebSocketError(String method, Map params, errorCode, 364 Future expectWebSocketError(String method, Map params, errorCode,
344 errorMessage) { 365 errorMessage) {
345 return schedule(() { 366 return schedule(() {
346 return Future.wait([ 367 return webSocketRequest(method, params).then((response) {
347 _ensureWebSocket(),
348 awaitObject(params)
349 ]).then((results) {
350 var resolvedParams = results[1];
351 return _jsonRpcRequest(method, resolvedParams);
352 }).then((response) {
353 expect(response["error"]["code"], errorCode); 368 expect(response["error"]["code"], errorCode);
354 expect(response["error"]["message"], errorMessage); 369 expect(response["error"]["message"], errorMessage);
355
356 return response["error"]["data"]; 370 return response["error"]["data"];
357 }); 371 });
358 }, "send $method with $params to web socket and expect error $errorCode"); 372 }, "send $method with $params to web socket and expect error $errorCode");
359 } 373 }
360 374
361 /// Validates that [root] was not bound to a port when pub serve started. 375 /// Validates that [root] was not bound to a port when pub serve started.
362 Future expectNotServed(String root) { 376 Future expectNotServed(String root) {
363 return schedule(() { 377 return schedule(() {
364 expect(_ports.containsKey(root), isFalse); 378 expect(_ports.containsKey(root), isFalse);
365 }); 379 });
366 } 380 }
367 381
368 /// The next id to use for a JSON-RPC 2.0 request. 382 /// The next id to use for a JSON-RPC 2.0 request.
369 var _rpcId = 0; 383 var _rpcId = 0;
370 384
371 /// Sends a JSON-RPC 2.0 request calling [method] with [params]. 385 /// Sends a JSON-RPC 2.0 request calling [method] with [params].
372 /// 386 ///
373 /// Returns the response object. 387 /// Returns the response object.
374 Future<Map> _jsonRpcRequest(String method, Map params) { 388 Future<Map> _jsonRpcRequest(String method, Map params) {
375 var id = _rpcId++; 389 var id = _rpcId++;
376 _webSocket.add(JSON.encode({ 390 _webSocket.add(JSON.encode({
377 "jsonrpc": "2.0", 391 "jsonrpc": "2.0",
378 "method": method, 392 "method": method,
379 "params": params, 393 "params": params,
380 "id": id 394 "id": id
381 })); 395 }));
382 396
383 return _webSocketBroadcastStream.first.then((value) { 397 return _webSocketBroadcastStream
384 value = JSON.decode(value); 398 .firstWhere((response) => response["id"] == id).then((value) {
385 currentSchedule.addDebugInfo( 399 currentSchedule.addDebugInfo(
386 "Web Socket request $method with params $params\n" 400 "Web Socket request $method with params $params\n"
387 "Result: $value"); 401 "Result: $value");
388 402
389 expect(value["id"], equals(id)); 403 expect(value["id"], equals(id));
390 return value; 404 return value;
391 }); 405 });
392 } 406 }
393 407
394 /// Returns a [Future] that completes to a URL string for the server serving 408 /// Returns a [Future] that completes to a URL string for the server serving
(...skipping 19 matching lines...) Expand all
414 /// included. Unlike [getServerUrl], this should only be called after the ports 428 /// included. Unlike [getServerUrl], this should only be called after the ports
415 /// are known. 429 /// are known.
416 String _getServerUrlSync([String root, String path]) { 430 String _getServerUrlSync([String root, String path]) {
417 if (root == null) root = 'web'; 431 if (root == null) root = 'web';
418 expect(_ports, contains(root)); 432 expect(_ports, contains(root));
419 var url = "http://127.0.0.1:${_ports[root]}"; 433 var url = "http://127.0.0.1:${_ports[root]}";
420 if (path != null) url = "$url/$path"; 434 if (path != null) url = "$url/$path";
421 return url; 435 return url;
422 } 436 }
423 437
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698