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 parallel invocation of service which succeeds |
| 67 { |
| 68 final results = new List<Future<Map>>.generate(repetition, (iteration) { |
| 69 final end = iteration.toString(); |
| 70 return vm.invokeRpcRaw( |
| 71 vm.services.first.method, {paramKey + end: paramValue + end}); |
| 72 }); |
| 73 final requests = await (client.take(repetition).toList()); |
| 74 |
| 75 final completions = requests.map((final request) { |
| 76 final iteration = requests.indexOf(request); |
| 77 final end = iteration.toString(); |
| 78 |
| 79 // check requests while they arrive |
| 80 expect(request, contains('id')); |
| 81 expect(request['id'], isNotNull); |
| 82 expect(request['method'], equals(successServiceName)); |
| 83 expect(request['params'], isNotNull); |
| 84 expect(request['params'][paramKey + end], equals(paramValue + end)); |
| 85 |
| 86 // answer later |
| 87 return () => socket.add({ |
| 88 'jsonrpc': '2.0', |
| 89 'id': request['id'], |
| 90 'result': {resultKey + end: resultValue + end} |
| 91 }); |
| 92 }).toList(); |
| 93 // random order |
| 94 completions.shuffle(); |
| 95 // answer out of order |
| 96 completions.forEach((complete) => complete()); |
| 97 |
| 98 final responses = await Future.wait(results); |
| 99 responses.forEach((final response) { |
| 100 final iteration = responses.indexOf(response); |
| 101 final end = iteration.toString(); |
| 102 |
| 103 expect(response, isNotNull); |
| 104 expect(response[resultKey + end], equals(resultValue + end)); |
| 105 }); |
| 106 } |
| 107 |
| 108 // Testing parallel invocation of service which fails |
| 109 { |
| 110 final results = new List<Future<Map>>.generate(repetition, (iteration) { |
| 111 final end = iteration.toString(); |
| 112 return vm.invokeRpcRaw( |
| 113 vm.services[1].method, {paramKey + end: paramValue + end}); |
| 114 }); |
| 115 final requests = await (client.take(repetition).toList()); |
| 116 |
| 117 final completions = requests.map((final request) { |
| 118 final iteration = requests.indexOf(request); |
| 119 final end = iteration.toString(); |
| 120 |
| 121 // check requests while they arrive |
| 122 expect(request, contains('id')); |
| 123 expect(request['id'], isNotNull); |
| 124 expect(request['method'], equals(errorServiceName)); |
| 125 expect(request['params'], isNotNull); |
| 126 expect(request['params'][paramKey + end], equals(paramValue + end)); |
| 127 |
| 128 // answer later |
| 129 return () => socket.add({ |
| 130 'jsonrpc': '2.0', |
| 131 'id': request['id'], |
| 132 'error': { |
| 133 'code': errorCode + iteration, |
| 134 'data': {errorKey + end: errorValue + end} |
| 135 } |
| 136 }); |
| 137 }).toList(); |
| 138 // random order |
| 139 completions.shuffle(); |
| 140 // answer out of order |
| 141 completions.forEach((complete) => complete()); |
| 142 |
| 143 final errors = await Future.wait(results.map((future) { |
| 144 return future.then((_) { |
| 145 expect(false, isTrue, reason: 'shouldn\'t get here'); |
| 146 }).catchError((e) => e); |
| 147 })); |
| 148 errors.forEach((final ServerRpcException error) { |
| 149 final iteration = errors.indexOf(error); |
| 150 final end = iteration.toString(); |
| 151 |
| 152 expect(error, isNotNull); |
| 153 expect(error.code, equals(errorCode + iteration)); |
| 154 expect(error.data, isNotNull); |
| 155 expect(error.data[errorKey + end], equals(errorValue + end)); |
| 156 }); |
| 157 } |
| 158 |
| 159 await socket.close(); |
| 160 }, |
| 161 ]; |
| 162 |
| 163 main(args) => runIsolateTests(args, tests); |
OLD | NEW |