OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.channel.byte_stream; | 5 library test.channel.byte_stream; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'package:analysis_server/src/channel/byte_stream_channel.dart'; | 11 import 'package:analysis_server/src/channel/byte_stream_channel.dart'; |
12 import 'package:analysis_server/src/protocol.dart'; | 12 import 'package:analysis_server/src/protocol.dart'; |
13 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
14 | 14 |
15 import '../mocks.dart'; | 15 import '../mocks.dart'; |
16 | 16 |
17 main() { | 17 main() { |
18 group('ByteStreamClientChannel', () { | 18 group('ByteStreamClientChannel', () { |
19 setUp(ByteStreamClientChannelTest.setUp); | 19 setUp(ByteStreamClientChannelTest.setUp); |
20 test('close', ByteStreamClientChannelTest.close); | 20 test('close', ByteStreamClientChannelTest.close); |
21 test('listen_notification', ByteStreamClientChannelTest.listen_notification)
; | 21 test( |
| 22 'listen_notification', |
| 23 ByteStreamClientChannelTest.listen_notification); |
22 test('listen_response', ByteStreamClientChannelTest.listen_response); | 24 test('listen_response', ByteStreamClientChannelTest.listen_response); |
23 test('sendRequest', ByteStreamClientChannelTest.sendRequest); | 25 test('sendRequest', ByteStreamClientChannelTest.sendRequest); |
24 }); | 26 }); |
25 group('ByteStreamServerChannel', () { | 27 group('ByteStreamServerChannel', () { |
26 setUp(ByteStreamServerChannelTest.setUp); | 28 setUp(ByteStreamServerChannelTest.setUp); |
27 test('closed', ByteStreamServerChannelTest.closed); | 29 test('closed', ByteStreamServerChannelTest.closed); |
28 test('listen_wellFormedRequest', | 30 test( |
| 31 'listen_wellFormedRequest', |
29 ByteStreamServerChannelTest.listen_wellFormedRequest); | 32 ByteStreamServerChannelTest.listen_wellFormedRequest); |
30 test('listen_invalidRequest', | 33 test( |
| 34 'listen_invalidRequest', |
31 ByteStreamServerChannelTest.listen_invalidRequest); | 35 ByteStreamServerChannelTest.listen_invalidRequest); |
32 test('listen_invalidJson', ByteStreamServerChannelTest.listen_invalidJson); | 36 test('listen_invalidJson', ByteStreamServerChannelTest.listen_invalidJson); |
33 test('listen_streamError', ByteStreamServerChannelTest.listen_streamError); | 37 test('listen_streamError', ByteStreamServerChannelTest.listen_streamError); |
34 test('listen_streamDone', ByteStreamServerChannelTest.listen_streamDone); | 38 test('listen_streamDone', ByteStreamServerChannelTest.listen_streamDone); |
35 test('sendNotification', ByteStreamServerChannelTest.sendNotification); | 39 test('sendNotification', ByteStreamServerChannelTest.sendNotification); |
36 test('sendResponse', ByteStreamServerChannelTest.sendResponse); | 40 test('sendResponse', ByteStreamServerChannelTest.sendResponse); |
37 }); | 41 }); |
38 } | 42 } |
39 | 43 |
40 class ByteStreamClientChannelTest { | 44 class ByteStreamClientChannelTest { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 inputSink.writeln('{"id":"72"}'); | 93 inputSink.writeln('{"id":"72"}'); |
90 return pumpEventQueue().then((_) { | 94 return pumpEventQueue().then((_) { |
91 expect(responses.length, equals(1)); | 95 expect(responses.length, equals(1)); |
92 expect(responses[0].id, equals('72')); | 96 expect(responses[0].id, equals('72')); |
93 }); | 97 }); |
94 } | 98 } |
95 | 99 |
96 static Future sendRequest() { | 100 static Future sendRequest() { |
97 int assertCount = 0; | 101 int assertCount = 0; |
98 Request request = new Request('72', 'foo.bar'); | 102 Request request = new Request('72', 'foo.bar'); |
99 outputLineStream.first | 103 outputLineStream.first.then((line) => JSON.decode(line)).then((json) { |
100 .then((line) => JSON.decode(line)) | 104 expect(json[Request.ID], equals('72')); |
101 .then((json) { | 105 expect(json[Request.METHOD], equals('foo.bar')); |
102 expect(json[Request.ID], equals('72')); | 106 inputSink.writeln('{"id":"73"}'); |
103 expect(json[Request.METHOD], equals('foo.bar')); | 107 inputSink.writeln('{"id":"72"}'); |
104 inputSink.writeln('{"id":"73"}'); | 108 assertCount++; |
105 inputSink.writeln('{"id":"72"}'); | 109 }); |
106 assertCount++; | 110 channel.sendRequest(request).then((Response response) { |
107 }); | 111 expect(response.id, equals('72')); |
108 channel.sendRequest(request) | 112 assertCount++; |
109 .then((Response response) { | 113 }); |
110 expect(response.id, equals('72')); | |
111 assertCount++; | |
112 }); | |
113 return pumpEventQueue().then((_) => expect(assertCount, equals(2))); | 114 return pumpEventQueue().then((_) => expect(assertCount, equals(2))); |
114 } | 115 } |
115 | 116 |
116 static void setUp() { | 117 static void setUp() { |
117 var inputStream = new StreamController<List<int>>(); | 118 var inputStream = new StreamController<List<int>>(); |
118 inputSink = new IOSink(inputStream); | 119 inputSink = new IOSink(inputStream); |
119 var outputStream = new StreamController<List<int>>(); | 120 var outputStream = new StreamController<List<int>>(); |
120 outputLineStream = outputStream.stream.transform((new Utf8Codec()).decoder | 121 outputLineStream = outputStream.stream.transform( |
121 ).transform(new LineSplitter()); | 122 (new Utf8Codec()).decoder).transform(new LineSplitter()); |
122 outputSink = new IOSink(outputStream); | 123 outputSink = new IOSink(outputStream); |
123 channel = new ByteStreamClientChannel(inputStream.stream, outputSink); | 124 channel = new ByteStreamClientChannel(inputStream.stream, outputSink); |
124 } | 125 } |
125 } | 126 } |
126 | 127 |
127 class ByteStreamServerChannelTest { | 128 class ByteStreamServerChannelTest { |
128 static ByteStreamServerChannel channel; | 129 static ByteStreamServerChannel channel; |
129 | 130 |
130 /** | 131 /** |
131 * Sink that may be used to deliver data to the channel, as though it's | 132 * Sink that may be used to deliver data to the channel, as though it's |
(...skipping 15 matching lines...) Expand all Loading... |
147 * Stream of errors received from the channel via [listen()]. | 148 * Stream of errors received from the channel via [listen()]. |
148 */ | 149 */ |
149 static Stream errorStream; | 150 static Stream errorStream; |
150 | 151 |
151 /** | 152 /** |
152 * Future which is completed when then [listen()] reports [onDone]. | 153 * Future which is completed when then [listen()] reports [onDone]. |
153 */ | 154 */ |
154 static Future doneFuture; | 155 static Future doneFuture; |
155 | 156 |
156 static Future closed() { | 157 static Future closed() { |
157 return inputSink.close().then((_) => channel.closed.timeout(new Duration( | 158 return inputSink.close().then( |
158 seconds: 1))); | 159 (_) => channel.closed.timeout(new Duration(seconds: 1))); |
159 } | 160 } |
160 | 161 |
161 static Future listen_invalidJson() { | 162 static Future listen_invalidJson() { |
162 inputSink.writeln('{"id":'); | 163 inputSink.writeln('{"id":'); |
163 return inputSink.flush().then((_) => outputLineStream.first.timeout( | 164 return inputSink.flush().then( |
164 new Duration(seconds: 1))).then((String response) { | 165 (_) => |
| 166 outputLineStream.first.timeout( |
| 167 new Duration(seconds: 1))).then((String response) { |
165 var jsonResponse = new JsonCodec().decode(response); | 168 var jsonResponse = new JsonCodec().decode(response); |
166 expect(jsonResponse, isMap); | 169 expect(jsonResponse, isMap); |
167 expect(jsonResponse, contains('error')); | 170 expect(jsonResponse, contains('error')); |
168 expect(jsonResponse['error'], isNotNull); | 171 expect(jsonResponse['error'], isNotNull); |
169 }); | 172 }); |
170 } | 173 } |
171 | 174 |
172 static Future listen_invalidRequest() { | 175 static Future listen_invalidRequest() { |
173 inputSink.writeln('{"id":"0"}'); | 176 inputSink.writeln('{"id":"0"}'); |
174 return inputSink.flush().then((_) => outputLineStream.first.timeout( | 177 return inputSink.flush().then( |
175 new Duration(seconds: 1))).then((String response) { | 178 (_) => |
| 179 outputLineStream.first.timeout( |
| 180 new Duration(seconds: 1))).then((String response) { |
176 var jsonResponse = new JsonCodec().decode(response); | 181 var jsonResponse = new JsonCodec().decode(response); |
177 expect(jsonResponse, isMap); | 182 expect(jsonResponse, isMap); |
178 expect(jsonResponse, contains('error')); | 183 expect(jsonResponse, contains('error')); |
179 expect(jsonResponse['error'], isNotNull); | 184 expect(jsonResponse['error'], isNotNull); |
180 }); | 185 }); |
181 } | 186 } |
182 | 187 |
183 static Future listen_streamDone() { | 188 static Future listen_streamDone() { |
184 return inputSink.close().then((_) => doneFuture.timeout(new Duration( | 189 return inputSink.close().then( |
185 seconds: 1))); | 190 (_) => doneFuture.timeout(new Duration(seconds: 1))); |
186 } | 191 } |
187 | 192 |
188 static Future listen_streamError() { | 193 static Future listen_streamError() { |
189 var error = new Error(); | 194 var error = new Error(); |
190 inputSink.addError(error); | 195 inputSink.addError(error); |
191 return inputSink.flush().then((_) => errorStream.first.timeout(new Duration( | 196 return inputSink.flush().then( |
192 seconds: 1))).then((var receivedError) { | 197 (_) => |
| 198 errorStream.first.timeout(new Duration(seconds: 1))).then((var recei
vedError) { |
193 expect(receivedError, same(error)); | 199 expect(receivedError, same(error)); |
194 }); | 200 }); |
195 } | 201 } |
196 | 202 |
197 static Future listen_wellFormedRequest() { | 203 static Future listen_wellFormedRequest() { |
198 inputSink.writeln('{"id":"0","method":"server.version"}'); | 204 inputSink.writeln('{"id":"0","method":"server.version"}'); |
199 return inputSink.flush().then((_) => requestStream.first.timeout( | 205 return inputSink.flush().then( |
200 new Duration(seconds: 1))).then((Request request) { | 206 (_) => |
| 207 requestStream.first.timeout(new Duration(seconds: 1))).then((Request
request) { |
201 expect(request.id, equals("0")); | 208 expect(request.id, equals("0")); |
202 expect(request.method, equals("server.version")); | 209 expect(request.method, equals("server.version")); |
203 }); | 210 }); |
204 } | 211 } |
205 | 212 |
206 static Future sendNotification() { | 213 static Future sendNotification() { |
207 channel.sendNotification(new Notification('foo')); | 214 channel.sendNotification(new Notification('foo')); |
208 return outputLineStream.first.timeout(new Duration(seconds: 1)).then((String | 215 return outputLineStream.first.timeout( |
209 notification) { | 216 new Duration(seconds: 1)).then((String notification) { |
210 var jsonNotification = new JsonCodec().decode(notification); | 217 var jsonNotification = new JsonCodec().decode(notification); |
211 expect(jsonNotification, isMap); | 218 expect(jsonNotification, isMap); |
212 expect(jsonNotification, contains('event')); | 219 expect(jsonNotification, contains('event')); |
213 expect(jsonNotification['event'], equals('foo')); | 220 expect(jsonNotification['event'], equals('foo')); |
214 }); | 221 }); |
215 } | 222 } |
216 | 223 |
217 static Future sendResponse() { | 224 static Future sendResponse() { |
218 channel.sendResponse(new Response('foo')); | 225 channel.sendResponse(new Response('foo')); |
219 return outputLineStream.first.timeout(new Duration(seconds: 1)).then((String | 226 return outputLineStream.first.timeout( |
220 response) { | 227 new Duration(seconds: 1)).then((String response) { |
221 var jsonResponse = new JsonCodec().decode(response); | 228 var jsonResponse = new JsonCodec().decode(response); |
222 expect(jsonResponse, isMap); | 229 expect(jsonResponse, isMap); |
223 expect(jsonResponse, contains('id')); | 230 expect(jsonResponse, contains('id')); |
224 expect(jsonResponse['id'], equals('foo')); | 231 expect(jsonResponse['id'], equals('foo')); |
225 }); | 232 }); |
226 } | 233 } |
227 | 234 |
228 static void setUp() { | 235 static void setUp() { |
229 StreamController<List<int>> inputStream = new StreamController<List<int>>(); | 236 StreamController<List<int>> inputStream = new StreamController<List<int>>(); |
230 inputSink = new IOSink(inputStream); | 237 inputSink = new IOSink(inputStream); |
231 StreamController<List<int>> outputStream = new StreamController<List<int>>( | 238 StreamController<List<int>> outputStream = |
232 ); | 239 new StreamController<List<int>>(); |
233 outputLineStream = outputStream.stream.transform((new Utf8Codec()).decoder | 240 outputLineStream = outputStream.stream.transform( |
234 ).transform(new LineSplitter()); | 241 (new Utf8Codec()).decoder).transform(new LineSplitter()); |
235 IOSink outputSink = new IOSink(outputStream); | 242 IOSink outputSink = new IOSink(outputStream); |
236 channel = new ByteStreamServerChannel(inputStream.stream, outputSink); | 243 channel = new ByteStreamServerChannel(inputStream.stream, outputSink); |
237 StreamController<Request> requestStreamController = | 244 StreamController<Request> requestStreamController = |
238 new StreamController<Request>(); | 245 new StreamController<Request>(); |
239 requestStream = requestStreamController.stream; | 246 requestStream = requestStreamController.stream; |
240 StreamController errorStreamController = new StreamController(); | 247 StreamController errorStreamController = new StreamController(); |
241 errorStream = errorStreamController.stream; | 248 errorStream = errorStreamController.stream; |
242 Completer doneCompleter = new Completer(); | 249 Completer doneCompleter = new Completer(); |
243 doneFuture = doneCompleter.future; | 250 doneFuture = doneCompleter.future; |
244 channel.listen((Request request) { | 251 channel.listen((Request request) { |
245 requestStreamController.add(request); | 252 requestStreamController.add(request); |
246 }, onError: (error) { | 253 }, onError: (error) { |
247 errorStreamController.add(error); | 254 errorStreamController.add(error); |
248 }, onDone: () { | 255 }, onDone: () { |
249 doneCompleter.complete(); | 256 doneCompleter.complete(); |
250 }); | 257 }); |
251 } | 258 } |
252 } | 259 } |
OLD | NEW |