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

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

Issue 310053004: Add an exitOnClose function to the "pub serve" WebSocket API. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 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 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 expect(_pubServer, isNotNull); 321 expect(_pubServer, isNotNull);
322 expect(_adminPort, isNotNull); 322 expect(_adminPort, isNotNull);
323 323
324 return WebSocket.connect("ws://127.0.0.1:$_adminPort").then((socket) { 324 return WebSocket.connect("ws://127.0.0.1:$_adminPort").then((socket) {
325 _webSocket = socket; 325 _webSocket = socket;
326 // TODO(rnystrom): Works around #13913. 326 // TODO(rnystrom): Works around #13913.
327 _webSocketBroadcastStream = _webSocket.map(JSON.decode).asBroadcastStream(); 327 _webSocketBroadcastStream = _webSocket.map(JSON.decode).asBroadcastStream();
328 }); 328 });
329 } 329 }
330 330
331 /// Schedules closing the web socket connection to the currently-running pub
332 /// serve.
333 Future closeWebSocket() {
334 schedule(() {
335 return _ensureWebSocket().then((_) => _webSocket.close())
336 .then((_) => _webSocket = null);
337 }, "closing web socket");
338 }
339
331 /// Sends a JSON RPC 2.0 request to the running pub serve's web socket 340 /// Sends a JSON RPC 2.0 request to the running pub serve's web socket
332 /// connection. 341 /// connection.
333 /// 342 ///
334 /// This calls a method named [method] with the given [params]. [params] may 343 /// This calls a method named [method] with the given [params] (or no
335 /// contain Futures, in which case this will wait until they've completed before 344 /// parameters, if it's not passed). [params] may contain Futures, in which case
336 /// sending the request. 345 /// this will wait until they've completed before sending the request.
337 /// 346 ///
338 /// This schedules the request, but doesn't block the schedule on the response. 347 /// This schedules the request, but doesn't block the schedule on the response.
339 /// It returns the response as a [Future]. 348 /// It returns the response as a [Future].
340 Future<Map> webSocketRequest(String method, Map params) { 349 Future<Map> webSocketRequest(String method, [Map params]) {
341 var completer = new Completer(); 350 var completer = new Completer();
342 schedule(() { 351 schedule(() {
343 return Future.wait([ 352 return Future.wait([
344 _ensureWebSocket(), 353 _ensureWebSocket(),
345 awaitObject(params), 354 awaitObject(params),
346 ]).then((results) { 355 ]).then((results) {
347 var resolvedParams = results[1]; 356 var resolvedParams = results[1];
348 chainToCompleter( 357 chainToCompleter(
349 currentSchedule.wrapFuture(_jsonRpcRequest(method, resolvedParams)), 358 currentSchedule.wrapFuture(_jsonRpcRequest(method, resolvedParams)),
350 completer); 359 completer);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 expect(_ports.containsKey(root), isFalse); 426 expect(_ports.containsKey(root), isFalse);
418 }); 427 });
419 } 428 }
420 429
421 /// The next id to use for a JSON-RPC 2.0 request. 430 /// The next id to use for a JSON-RPC 2.0 request.
422 var _rpcId = 0; 431 var _rpcId = 0;
423 432
424 /// Sends a JSON-RPC 2.0 request calling [method] with [params]. 433 /// Sends a JSON-RPC 2.0 request calling [method] with [params].
425 /// 434 ///
426 /// Returns the response object. 435 /// Returns the response object.
427 Future<Map> _jsonRpcRequest(String method, Map params) { 436 Future<Map> _jsonRpcRequest(String method, [Map params]) {
428 var id = _rpcId++; 437 var id = _rpcId++;
429 _webSocket.add(JSON.encode({ 438 var message = {
430 "jsonrpc": "2.0", 439 "jsonrpc": "2.0",
431 "method": method, 440 "method": method,
432 "params": params,
433 "id": id 441 "id": id
434 })); 442 };
443 if (params != null) message["params"] = params;
444 _webSocket.add(JSON.encode(message));
435 445
436 return _webSocketBroadcastStream 446 return _webSocketBroadcastStream
437 .firstWhere((response) => response["id"] == id).then((value) { 447 .firstWhere((response) => response["id"] == id).then((value) {
438 currentSchedule.addDebugInfo( 448 currentSchedule.addDebugInfo(
439 "Web Socket request $method with params $params\n" 449 "Web Socket request $method with params $params\n"
440 "Result: $value"); 450 "Result: $value");
441 451
442 expect(value["id"], equals(id)); 452 expect(value["id"], equals(id));
443 return value; 453 return value;
444 }); 454 });
(...skipping 22 matching lines...) Expand all
467 /// included. Unlike [getServerUrl], this should only be called after the ports 477 /// included. Unlike [getServerUrl], this should only be called after the ports
468 /// are known. 478 /// are known.
469 String _getServerUrlSync([String root, String path]) { 479 String _getServerUrlSync([String root, String path]) {
470 if (root == null) root = 'web'; 480 if (root == null) root = 'web';
471 expect(_ports, contains(root)); 481 expect(_ports, contains(root));
472 var url = "http://127.0.0.1:${_ports[root]}"; 482 var url = "http://127.0.0.1:${_ports[root]}";
473 if (path != null) url = "$url/$path"; 483 if (path != null) url = "$url/$path";
474 return url; 484 return url;
475 } 485 }
476 486
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698