OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 // VMOptions=--error_on_bad_type --error_on_bad_override |
| 5 |
| 6 import 'package:observatory/service_io.dart'; |
| 7 import 'package:unittest/unittest.dart'; |
| 8 import 'test_helper.dart'; |
| 9 import 'dart:io' show WebSocket; |
| 10 import 'dart:convert' show JSON; |
| 11 import 'dart:async' show Future, StreamController; |
| 12 |
| 13 var tests = [ |
| 14 (Isolate isolate) async { |
| 15 VM vm = isolate.owner; |
| 16 |
| 17 final serviceEvents = |
| 18 (await vm.getEventStream('_Service')).asBroadcastStream(); |
| 19 |
| 20 WebSocket _socket = |
| 21 await WebSocket.connect((vm as WebSocketVM).target.networkAddress); |
| 22 |
| 23 final socket = new StreamController(); |
| 24 |
| 25 // Avoid to manually encode and decode messages from the stream |
| 26 socket.stream.map(JSON.encode).pipe(_socket); |
| 27 final client = _socket.map(JSON.decode).asBroadcastStream(); |
| 28 |
| 29 // Note: keep this in sync with sdk/lib/vmservice.dart |
| 30 const kServiceDisappeared = 111; |
| 31 const kServiceDisappeared_Msg = 'Service has disappeared'; |
| 32 |
| 33 const serviceName = 'disapearService'; |
| 34 const serviceAlias = 'serviceAlias'; |
| 35 const paramKey = 'pkey'; |
| 36 const paramValue = 'pvalue'; |
| 37 const repetition = 5; |
| 38 |
| 39 socket.add({ |
| 40 'jsonrpc': '2.0', |
| 41 'id': 1, |
| 42 'method': '_registerService', |
| 43 'params': {'service': serviceName, 'alias': serviceAlias} |
| 44 }); |
| 45 |
| 46 // Avoid flaky test. |
| 47 // We cannot assume the order in which two messages will arrive |
| 48 // from two different sockets |
| 49 await Future.wait([client.first, serviceEvents.first]); |
| 50 |
| 51 // Testing invocation of service which disappear |
| 52 { |
| 53 final results = new List<Future<Map>>.generate(repetition, (iteration) { |
| 54 final end = iteration.toString(); |
| 55 return vm.invokeRpcRaw( |
| 56 vm.services.first.method, {paramKey + end: paramValue + end}); |
| 57 }); |
| 58 final requests = await (client.take(repetition).toList()); |
| 59 |
| 60 requests.forEach((final request) { |
| 61 final iteration = requests.indexOf(request); |
| 62 final end = iteration.toString(); |
| 63 |
| 64 // check requests while they arrive |
| 65 expect(request, contains('id')); |
| 66 expect(request['id'], isNotNull); |
| 67 expect(request['method'], equals(serviceName)); |
| 68 expect(request['params'], isNotNull); |
| 69 expect(request['params'][paramKey + end], equals(paramValue + end)); |
| 70 }); |
| 71 |
| 72 await socket.close(); |
| 73 |
| 74 await Future.wait(results.map((future) { |
| 75 return future.then((_) { |
| 76 expect(false, isTrue, reason: 'shouldn\'t get here'); |
| 77 }).catchError((ServerRpcException error) { |
| 78 expect(error, isNotNull); |
| 79 expect(error.code, equals(kServiceDisappeared)); |
| 80 expect(error.message, equals(kServiceDisappeared_Msg)); |
| 81 }); |
| 82 })); |
| 83 } |
| 84 }, |
| 85 ]; |
| 86 |
| 87 main(args) => runIsolateTests(args, tests); |
OLD | NEW |