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 'id': 1, |
| 44 'method': '_registerService', |
| 45 'params': {'service': serviceName, 'alias': serviceAlias} |
| 46 }); |
| 47 |
| 48 // Avoid flaky test. |
| 49 // We cannot assume the order in which two messages will arrive |
| 50 // from two different sockets |
| 51 final message = |
| 52 (await Future.wait([client.first, serviceEvents.first])).first; |
| 53 |
| 54 expect(message['id'], equals(1), |
| 55 reason: 'Should answer with the same id'); |
| 56 expect(message['result'], isNotEmpty); |
| 57 expect(message['result']['type'], equals('Success')); |
| 58 |
| 59 expect(vm.services, isNotEmpty); |
| 60 expect(vm.services.length, equals(1)); |
| 61 expect(vm.services.first.service, equals(serviceName)); |
| 62 expect(vm.services.first.method, isNotEmpty); |
| 63 expect(vm.services.first.alias, equals(serviceAlias)); |
| 64 } |
| 65 |
| 66 { |
| 67 // Registering second service |
| 68 socket.add({ |
| 69 'jsonrpc': '2.0', |
| 70 'id': 1, |
| 71 'method': '_registerService', |
| 72 'params': {'service': serviceName + '2', 'alias': serviceAlias + '2'} |
| 73 }); |
| 74 |
| 75 // Avoid flaky test. |
| 76 // We cannot assume the order in which two messages will arrive |
| 77 // from two different sockets |
| 78 final message = |
| 79 (await Future.wait([client.first, serviceEvents.first])).first; |
| 80 |
| 81 expect(message['id'], equals(1), |
| 82 reason: 'Should answer with the same id'); |
| 83 expect(message['result'], isNotEmpty); |
| 84 expect(message['result']['type'], equals('Success')); |
| 85 |
| 86 expect(vm.services, isNotEmpty); |
| 87 expect(vm.services.length, equals(2)); |
| 88 expect(vm.services[1].service, equals(serviceName + '2')); |
| 89 expect(vm.services[1].method, isNotEmpty); |
| 90 expect(vm.services[1].alias, equals(serviceAlias + '2')); |
| 91 } |
| 92 |
| 93 { |
| 94 // Double registering first service |
| 95 socket.add({ |
| 96 'jsonrpc': '2.0', |
| 97 'id': 1, |
| 98 'method': '_registerService', |
| 99 'params': {'service': serviceName, 'alias': serviceAlias} |
| 100 }); |
| 101 |
| 102 final message = await client.first; |
| 103 |
| 104 expect(message['id'], equals(1), |
| 105 reason: 'Should answer with the same id'); |
| 106 expect(message['error'], isNotEmpty); |
| 107 expect(message['error']['code'], equals(kServiceAlreadyRegistered)); |
| 108 expect( |
| 109 message['error']['message'], equals(kServiceAlreadyRegistered_Msg)); |
| 110 } |
| 111 |
| 112 // Avoid flaky test. |
| 113 // We cannot assume the order in which two messages will arrive |
| 114 // from two different sockets |
| 115 await Future.wait([socket.close(), serviceEvents.take(2).last]); |
| 116 |
| 117 expect(vm.services, isEmpty, |
| 118 reason: 'Should unregister services when client disconnects'); |
| 119 }, |
| 120 ]; |
| 121 |
| 122 main(args) => runIsolateTests(args, tests); |
OLD | NEW |