Chromium Code Reviews| Index: runtime/observatory/tests/service/register_service_test.dart |
| diff --git a/runtime/observatory/tests/service/register_service_test.dart b/runtime/observatory/tests/service/register_service_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..00f518878ac49c63348defa4178ad6b0e56fa0ff |
| --- /dev/null |
| +++ b/runtime/observatory/tests/service/register_service_test.dart |
| @@ -0,0 +1,181 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
|
siva
2017/07/13 01:07:40
2017
cbernaschina
2017/07/13 04:10:15
Done.
|
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| +// VMOptions=--error_on_bad_type --error_on_bad_override |
| + |
| +import 'package:observatory/service_io.dart'; |
| +import 'package:unittest/unittest.dart'; |
| +import 'test_helper.dart'; |
| +import 'dart:io' show WebSocket; |
| +import 'dart:convert' show JSON; |
| +import 'dart:async' show Future; |
| + |
| +var tests = [ |
| + (Isolate isolate) async { |
| + VM vm = isolate.owner; |
| + |
| + final serviceEvents = |
| + (await vm.getEventStream('_Service')).asBroadcastStream(); |
| + |
| + expect(vm.services, isEmpty); |
| + |
| + WebSocket socket = |
| + await WebSocket.connect((vm as WebSocketVM).target.networkAddress); |
| + |
| + const serviceName = 'serviceName'; |
| + const serviceAlias = 'serviceAlias'; |
| + const paramKey = 'pkey'; |
| + const paramValue = 'pvalue'; |
| + const resultKey = 'rkey'; |
| + const resultValue = 'rvalue'; |
| + const errorCode = 5000; |
| + const errorKey = 'ekey'; |
| + const errorValue = 'evalue'; |
| + const repetition = 5; |
| + |
| + socket.add(JSON.encode({ |
| + 'jsonrpc': '2.0', |
| + 'id': 1, |
| + 'method': '_registerService', |
| + 'params': {'service': serviceName, 'alias': serviceAlias} |
| + })); |
| + |
| + final client = socket.asBroadcastStream(); |
| + |
| + { |
| + final message = JSON.decode(await client.first); |
| + expect(message['id'], equals(1)); |
| + expect(message['result'], isNotEmpty); |
| + expect(message['result']['type'], equals('Success')); |
| + } |
| + await serviceEvents.first; |
| + expect(vm.services, isNotEmpty); |
| + expect(vm.services.length, equals(1)); |
| + expect(vm.services.first.service, equals(serviceName)); |
| + expect(vm.services.first.method, isNotEmpty); |
| + expect(vm.services.first.alias, equals(serviceAlias)); |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + final result = vm.invokeRpcRaw( |
| + vm.services.first.method, {paramKey + end: paramValue + end}); |
| + final request = JSON.decode(await client.first); |
| + expect(request, contains('id')); |
| + expect(request['id'], isNotNull); |
| + expect(request['method'], equals(serviceName)); |
| + expect(request['params'], isNotNull); |
| + expect(request['params'][paramKey + end], equals(paramValue + end)); |
| + socket.add(JSON.encode({ |
| + 'jsonrpc': '2.0', |
| + 'id': request['id'], |
| + 'result': {resultKey + end: resultValue + end} |
| + })); |
| + |
| + final response = await result; |
| + expect(response, isNotNull); |
| + expect(response[resultKey + end], equals(resultValue + end)); |
| + } |
| + |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + final result = vm.invokeRpcRaw( |
| + vm.services.first.method, {paramKey + end: paramValue + end}); |
| + final request = JSON.decode(await client.first); |
| + expect(request, contains('id')); |
| + expect(request['id'], isNotNull); |
| + expect(request['method'], equals(serviceName)); |
| + expect(request['params'], isNotNull); |
| + expect(request['params'][paramKey + end], equals(paramValue + end)); |
| + socket.add(JSON.encode({ |
| + 'jsonrpc': '2.0', |
| + 'id': request['id'], |
| + 'error': { |
| + 'code': errorCode + iteration, |
| + 'data': {errorKey + end: errorValue + end} |
| + } |
| + })); |
| + |
| + try { |
| + final response = await result; |
| + expect(false, isTrue, reason: 'should\'t get here'); |
| + } on ServerRpcException catch (e) { |
| + expect(e.code, equals(errorCode + iteration)); |
| + expect(e.data, isNotNull); |
| + expect(e.data[errorKey + end], equals(errorValue + end)); |
| + } |
| + } |
| + |
| + { |
| + final List<Future<Map>> results = <Future<Map>>[]; |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + results.add(vm.invokeRpcRaw( |
| + vm.services.first.method, {paramKey + end: paramValue + end})); |
| + } |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + final request = JSON.decode(await client.first); |
| + expect(request, contains('id')); |
| + expect(request['id'], isNotNull); |
| + expect(request['method'], equals(serviceName)); |
| + expect(request['params'], isNotNull); |
| + expect(request['params'][paramKey + end], equals(paramValue + end)); |
| + socket.add(JSON.encode({ |
| + 'jsonrpc': '2.0', |
| + 'id': request['id'], |
| + 'result': {resultKey + end: resultValue + end} |
| + })); |
| + } |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + final response = await results[iteration]; |
| + expect(response, isNotNull); |
| + expect(response[resultKey + end], equals(resultValue + end)); |
| + } |
| + } |
| + |
| + { |
| + final List<Future<Map>> results = <Future<Map>>[]; |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + results.add(vm.invokeRpcRaw( |
| + vm.services.first.method, {paramKey + end: paramValue + end})); |
| + } |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + final request = JSON.decode(await client.first); |
| + expect(request, contains('id')); |
| + expect(request['id'], isNotNull); |
| + expect(request['method'], equals(serviceName)); |
| + expect(request['params'], isNotNull); |
| + expect(request['params'][paramKey + end], equals(paramValue + end)); |
| + socket.add(JSON.encode({ |
| + 'jsonrpc': '2.0', |
| + 'id': request['id'], |
| + 'error': { |
| + 'code': errorCode + iteration, |
| + 'data': {errorKey + end: errorValue + end} |
| + } |
| + })); |
| + } |
| + for (var iteration = 0; iteration < repetition; iteration++) { |
| + final end = iteration.toString(); |
| + try { |
| + final response = await results[iteration]; |
| + expect(false, isTrue, reason: 'should\'t get here'); |
| + } on ServerRpcException catch (e) { |
| + expect(e.code, equals(errorCode + iteration)); |
| + expect(e.data, isNotNull); |
| + expect(e.data[errorKey + end], equals(errorValue + end)); |
| + } |
| + } |
| + } |
| + |
| + expect(vm.services, isNotEmpty); |
| + expect(vm.services.length, equals(1)); |
| + await socket.close(); |
| + await serviceEvents.first; |
| + expect(vm.services, isEmpty); |
| + }, |
| +]; |
| + |
| +main(args) => runIsolateTests(args, tests); |