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 expect(vm.services, isEmpty, |
| 21 reason: 'No service should be registered at startup'); |
| 22 |
| 23 WebSocket _socket = |
| 24 await WebSocket.connect((vm as WebSocketVM).target.networkAddress); |
| 25 |
| 26 final socket = new StreamController(); |
| 27 |
| 28 // Avoid to manually encode and decode messages from the stream |
| 29 socket.stream.map(JSON.encode).pipe(_socket); |
| 30 final client = _socket.map(JSON.decode).asBroadcastStream(); |
| 31 |
| 32 // Note: keep this in sync with sdk/lib/vmservice.dart |
| 33 const kServiceAlreadyRegistered = 110; |
| 34 const kServiceAlreadyRegistered_Msg = 'Service already registered'; |
| 35 |
| 36 const serviceName = 'serviceName'; |
| 37 const serviceAlias = 'serviceAlias'; |
| 38 |
| 39 { |
| 40 // Registering first service |
| 41 socket.add({ |
| 42 'jsonrpc': '2.0', |
| 43 'method': '_registerService', |
| 44 'params': {'service': serviceName, 'alias': serviceAlias} |
| 45 }); |
| 46 |
| 47 await serviceEvents.first; |
| 48 |
| 49 expect(vm.services, isNotEmpty); |
| 50 expect(vm.services.length, equals(1)); |
| 51 expect(vm.services.first.service, equals(serviceName)); |
| 52 expect(vm.services.first.method, isNotEmpty); |
| 53 expect(vm.services.first.alias, equals(serviceAlias)); |
| 54 } |
| 55 |
| 56 { |
| 57 // Registering second service |
| 58 socket.add({ |
| 59 'jsonrpc': '2.0', |
| 60 'method': '_registerService', |
| 61 'params': {'service': serviceName + '2', 'alias': serviceAlias + '2'} |
| 62 }); |
| 63 |
| 64 await serviceEvents.first; |
| 65 |
| 66 expect(vm.services, isNotEmpty); |
| 67 expect(vm.services.length, equals(2)); |
| 68 expect(vm.services[1].service, equals(serviceName + '2')); |
| 69 expect(vm.services[1].method, isNotEmpty); |
| 70 expect(vm.services[1].alias, equals(serviceAlias + '2')); |
| 71 } |
| 72 |
| 73 { |
| 74 // Double registering first service |
| 75 socket.add({ |
| 76 'jsonrpc': '2.0', |
| 77 'id': 1, |
| 78 'method': '_registerService', |
| 79 'params': {'service': serviceName, 'alias': serviceAlias} |
| 80 }); |
| 81 |
| 82 final message = await client.first; |
| 83 |
| 84 expect(message['id'], equals(1), |
| 85 reason: 'Should answer with the same id'); |
| 86 expect(message['error'], isNotEmpty); |
| 87 expect(message['error']['code'], equals(kServiceAlreadyRegistered)); |
| 88 expect( |
| 89 message['error']['message'], equals(kServiceAlreadyRegistered_Msg)); |
| 90 } |
| 91 |
| 92 // Avoid flaky test. |
| 93 // We cannot assume the order in which two messages will arrive |
| 94 // from two different sockets |
| 95 await Future.wait([socket.close(), serviceEvents.take(2).last]); |
| 96 |
| 97 expect(vm.services, isEmpty, |
| 98 reason: 'Should unregister services when client disconnects'); |
| 99 }, |
| 100 ]; |
| 101 |
| 102 main(args) => runIsolateTests(args, tests); |
OLD | NEW |