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 const successServiceName = 'successService'; |
| 30 const errorServiceName = 'errorService'; |
| 31 const serviceAlias = 'serviceAlias'; |
| 32 const paramKey = 'pkey'; |
| 33 const paramValue = 'pvalue'; |
| 34 const resultKey = 'rkey'; |
| 35 const resultValue = 'rvalue'; |
| 36 const errorCode = 5000; |
| 37 const errorKey = 'ekey'; |
| 38 const errorValue = 'evalue'; |
| 39 const repetition = 5; |
| 40 |
| 41 socket.add({ |
| 42 'jsonrpc': '2.0', |
| 43 'id': 1, |
| 44 'method': '_registerService', |
| 45 'params': {'service': successServiceName, '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 await Future.wait([client.first, serviceEvents.first]); |
| 52 |
| 53 // Registering second service |
| 54 socket.add({ |
| 55 'jsonrpc': '2.0', |
| 56 'id': 1, |
| 57 'method': '_registerService', |
| 58 'params': {'service': errorServiceName, 'alias': serviceAlias} |
| 59 }); |
| 60 |
| 61 // Avoid flaky test. |
| 62 // We cannot assume the order in which two messages will arrive |
| 63 // from two different sockets |
| 64 await Future.wait([client.first, serviceEvents.first]); |
| 65 |
| 66 // Testing serial invocation of service which succeeds |
| 67 for (var iteration = 0; iteration < repetition; iteration++) { |
| 68 final end = iteration.toString(); |
| 69 final result = vm.invokeRpcRaw( |
| 70 vm.services.first.method, {paramKey + end: paramValue + end}); |
| 71 final request = await client.first; |
| 72 |
| 73 expect(request, contains('id')); |
| 74 expect(request['id'], isNotNull); |
| 75 expect(request['method'], equals(successServiceName)); |
| 76 expect(request['params'], isNotNull); |
| 77 expect(request['params'][paramKey + end], equals(paramValue + end)); |
| 78 |
| 79 socket.add({ |
| 80 'jsonrpc': '2.0', |
| 81 'id': request['id'], |
| 82 'result': {resultKey + end: resultValue + end} |
| 83 }); |
| 84 |
| 85 final response = await result; |
| 86 |
| 87 expect(response, isNotNull); |
| 88 expect(response[resultKey + end], equals(resultValue + end)); |
| 89 } |
| 90 |
| 91 // Testing serial invocation of service which fails |
| 92 for (var iteration = 0; iteration < repetition; iteration++) { |
| 93 final end = iteration.toString(); |
| 94 final result = vm.invokeRpcRaw( |
| 95 vm.services[1].method, {paramKey + end: paramValue + end}); |
| 96 final request = await client.first; |
| 97 |
| 98 expect(request, contains('id')); |
| 99 expect(request['id'], isNotNull); |
| 100 expect(request['method'], equals(errorServiceName)); |
| 101 expect(request['params'], isNotNull); |
| 102 expect(request['params'][paramKey + end], equals(paramValue + end)); |
| 103 |
| 104 socket.add({ |
| 105 'jsonrpc': '2.0', |
| 106 'id': request['id'], |
| 107 'error': { |
| 108 'code': errorCode + iteration, |
| 109 'data': {errorKey + end: errorValue + end} |
| 110 } |
| 111 }); |
| 112 |
| 113 try { |
| 114 final response = await result; |
| 115 expect(false, isTrue, reason: 'shouldn\'t get here'); |
| 116 } on ServerRpcException catch (e) { |
| 117 expect(e.code, equals(errorCode + iteration)); |
| 118 expect(e.data, isNotNull); |
| 119 expect(e.data[errorKey + end], equals(errorValue + end)); |
| 120 } |
| 121 } |
| 122 }, |
| 123 ]; |
| 124 |
| 125 main(args) => runIsolateTests(args, tests); |
OLD | NEW |