OLD | NEW |
---|---|
(Empty) | |
1 // 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.
| |
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; | |
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 | |
22 WebSocket socket = | |
23 await WebSocket.connect((vm as WebSocketVM).target.networkAddress); | |
24 | |
25 const serviceName = 'serviceName'; | |
26 const serviceAlias = 'serviceAlias'; | |
27 const paramKey = 'pkey'; | |
28 const paramValue = 'pvalue'; | |
29 const resultKey = 'rkey'; | |
30 const resultValue = 'rvalue'; | |
31 const errorCode = 5000; | |
32 const errorKey = 'ekey'; | |
33 const errorValue = 'evalue'; | |
34 const repetition = 5; | |
35 | |
36 socket.add(JSON.encode({ | |
37 'jsonrpc': '2.0', | |
38 'id': 1, | |
39 'method': '_registerService', | |
40 'params': {'service': serviceName, 'alias': serviceAlias} | |
41 })); | |
42 | |
43 final client = socket.asBroadcastStream(); | |
44 | |
45 { | |
46 final message = JSON.decode(await client.first); | |
47 expect(message['id'], equals(1)); | |
48 expect(message['result'], isNotEmpty); | |
49 expect(message['result']['type'], equals('Success')); | |
50 } | |
51 await serviceEvents.first; | |
52 expect(vm.services, isNotEmpty); | |
53 expect(vm.services.length, equals(1)); | |
54 expect(vm.services.first.service, equals(serviceName)); | |
55 expect(vm.services.first.method, isNotEmpty); | |
56 expect(vm.services.first.alias, equals(serviceAlias)); | |
57 for (var iteration = 0; iteration < repetition; iteration++) { | |
58 final end = iteration.toString(); | |
59 final result = vm.invokeRpcRaw( | |
60 vm.services.first.method, {paramKey + end: paramValue + end}); | |
61 final request = JSON.decode(await client.first); | |
62 expect(request, contains('id')); | |
63 expect(request['id'], isNotNull); | |
64 expect(request['method'], equals(serviceName)); | |
65 expect(request['params'], isNotNull); | |
66 expect(request['params'][paramKey + end], equals(paramValue + end)); | |
67 socket.add(JSON.encode({ | |
68 'jsonrpc': '2.0', | |
69 'id': request['id'], | |
70 'result': {resultKey + end: resultValue + end} | |
71 })); | |
72 | |
73 final response = await result; | |
74 expect(response, isNotNull); | |
75 expect(response[resultKey + end], equals(resultValue + end)); | |
76 } | |
77 | |
78 for (var iteration = 0; iteration < repetition; iteration++) { | |
79 final end = iteration.toString(); | |
80 final result = vm.invokeRpcRaw( | |
81 vm.services.first.method, {paramKey + end: paramValue + end}); | |
82 final request = JSON.decode(await client.first); | |
83 expect(request, contains('id')); | |
84 expect(request['id'], isNotNull); | |
85 expect(request['method'], equals(serviceName)); | |
86 expect(request['params'], isNotNull); | |
87 expect(request['params'][paramKey + end], equals(paramValue + end)); | |
88 socket.add(JSON.encode({ | |
89 'jsonrpc': '2.0', | |
90 'id': request['id'], | |
91 'error': { | |
92 'code': errorCode + iteration, | |
93 'data': {errorKey + end: errorValue + end} | |
94 } | |
95 })); | |
96 | |
97 try { | |
98 final response = await result; | |
99 expect(false, isTrue, reason: 'should\'t get here'); | |
100 } on ServerRpcException catch (e) { | |
101 expect(e.code, equals(errorCode + iteration)); | |
102 expect(e.data, isNotNull); | |
103 expect(e.data[errorKey + end], equals(errorValue + end)); | |
104 } | |
105 } | |
106 | |
107 { | |
108 final List<Future<Map>> results = <Future<Map>>[]; | |
109 for (var iteration = 0; iteration < repetition; iteration++) { | |
110 final end = iteration.toString(); | |
111 results.add(vm.invokeRpcRaw( | |
112 vm.services.first.method, {paramKey + end: paramValue + end})); | |
113 } | |
114 for (var iteration = 0; iteration < repetition; iteration++) { | |
115 final end = iteration.toString(); | |
116 final request = JSON.decode(await client.first); | |
117 expect(request, contains('id')); | |
118 expect(request['id'], isNotNull); | |
119 expect(request['method'], equals(serviceName)); | |
120 expect(request['params'], isNotNull); | |
121 expect(request['params'][paramKey + end], equals(paramValue + end)); | |
122 socket.add(JSON.encode({ | |
123 'jsonrpc': '2.0', | |
124 'id': request['id'], | |
125 'result': {resultKey + end: resultValue + end} | |
126 })); | |
127 } | |
128 for (var iteration = 0; iteration < repetition; iteration++) { | |
129 final end = iteration.toString(); | |
130 final response = await results[iteration]; | |
131 expect(response, isNotNull); | |
132 expect(response[resultKey + end], equals(resultValue + end)); | |
133 } | |
134 } | |
135 | |
136 { | |
137 final List<Future<Map>> results = <Future<Map>>[]; | |
138 for (var iteration = 0; iteration < repetition; iteration++) { | |
139 final end = iteration.toString(); | |
140 results.add(vm.invokeRpcRaw( | |
141 vm.services.first.method, {paramKey + end: paramValue + end})); | |
142 } | |
143 for (var iteration = 0; iteration < repetition; iteration++) { | |
144 final end = iteration.toString(); | |
145 final request = JSON.decode(await client.first); | |
146 expect(request, contains('id')); | |
147 expect(request['id'], isNotNull); | |
148 expect(request['method'], equals(serviceName)); | |
149 expect(request['params'], isNotNull); | |
150 expect(request['params'][paramKey + end], equals(paramValue + end)); | |
151 socket.add(JSON.encode({ | |
152 'jsonrpc': '2.0', | |
153 'id': request['id'], | |
154 'error': { | |
155 'code': errorCode + iteration, | |
156 'data': {errorKey + end: errorValue + end} | |
157 } | |
158 })); | |
159 } | |
160 for (var iteration = 0; iteration < repetition; iteration++) { | |
161 final end = iteration.toString(); | |
162 try { | |
163 final response = await results[iteration]; | |
164 expect(false, isTrue, reason: 'should\'t get here'); | |
165 } on ServerRpcException catch (e) { | |
166 expect(e.code, equals(errorCode + iteration)); | |
167 expect(e.data, isNotNull); | |
168 expect(e.data[errorKey + end], equals(errorValue + end)); | |
169 } | |
170 } | |
171 } | |
172 | |
173 expect(vm.services, isNotEmpty); | |
174 expect(vm.services.length, equals(1)); | |
175 await socket.close(); | |
176 await serviceEvents.first; | |
177 expect(vm.services, isEmpty); | |
178 }, | |
179 ]; | |
180 | |
181 main(args) => runIsolateTests(args, tests); | |
OLD | NEW |