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; | 5 library test.channel; |
6 | 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/src/channel.dart'; |
7 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; |
8 import 'package:unittest/matcher.dart'; | 11 import 'package:unittest/matcher.dart'; |
9 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
10 import 'package:analysis_server/src/channel.dart'; | 13 |
11 import 'mocks.dart'; | 14 import 'mocks.dart'; |
12 | 15 |
13 main() { | 16 main() { |
14 group('Channel', () { | 17 group('WebSocketChannel', () { |
15 test('invalidJsonToClient', ChannelTest.invalidJsonToClient); | 18 setUp(WebSocketChannelTest.setUp); |
16 test('invalidJsonToServer', ChannelTest.invalidJsonToServer); | 19 test('close', WebSocketChannelTest.close); |
17 test('notification', ChannelTest.notification); | 20 test('invalidJsonToClient', WebSocketChannelTest.invalidJsonToClient); |
18 test('request', ChannelTest.request); | 21 test('invalidJsonToServer', WebSocketChannelTest.invalidJsonToServer); |
19 test('response', ChannelTest.response); | 22 test('notification', WebSocketChannelTest.notification); |
| 23 test('notificationAndResponse', WebSocketChannelTest.notificationAndResponse
); |
| 24 test('request', WebSocketChannelTest.request); |
| 25 test('requestResponse', WebSocketChannelTest.requestResponse); |
| 26 test('response', WebSocketChannelTest.response); |
20 }); | 27 }); |
21 } | 28 } |
22 | 29 |
23 class ChannelTest { | 30 class WebSocketChannelTest { |
| 31 static MockSocket socket; |
| 32 static WebSocketClientChannel client; |
| 33 static WebSocketServerChannel server; |
24 | 34 |
25 static void invalidJsonToClient() { | 35 static List requestsReceived; |
26 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); | 36 static List responsesReceived; |
27 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); | 37 static List notificationsReceived; |
28 var responsesReceived = new List(); | |
29 var notificationsReceived = new List(); | |
30 client.listen((Response response) => responsesReceived.add(response), | |
31 (Notification notification) => notificationsReceived.add(notification)); | |
32 | 38 |
33 mockSocket.addInvalid('"blat"'); | 39 static void setUp() { |
34 mockSocket.addInvalid('{foo:bar}'); | 40 socket = new MockSocket.pair(); |
| 41 client = new WebSocketClientChannel(socket); |
| 42 server = new WebSocketServerChannel(socket.twin); |
35 | 43 |
36 expect(responsesReceived.length, equals(0)); | 44 requestsReceived = []; |
37 expect(notificationsReceived.length, equals(0)); | 45 responsesReceived = []; |
38 expect(mockSocket.responseCount, equals(0)); | 46 notificationsReceived = []; |
| 47 |
| 48 // Allow multiple listeners on server side for testing. |
| 49 socket.twin.allowMultipleListeners(); |
| 50 |
| 51 server.listen(requestsReceived.add); |
| 52 client.responseStream.listen(responsesReceived.add); |
| 53 client.notificationStream.listen(notificationsReceived.add); |
39 } | 54 } |
40 | 55 |
41 static void invalidJsonToServer() { | 56 static Future close() { |
42 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); | 57 var timeout = new Duration(seconds: 1); |
43 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); | 58 var future = client.responseStream.drain().timeout(timeout); |
44 var received = new List(); | 59 client.close(); |
45 server.listen((Request request) => received.add(request)); | 60 return future; |
46 | |
47 mockSocket.addInvalid('"blat"'); | |
48 mockSocket.addInvalid('{foo:bar}'); | |
49 | |
50 expect(received.length, equals(0)); | |
51 expect(mockSocket.responseCount, equals(2)); | |
52 } | 61 } |
53 | 62 |
54 static void notification() { | 63 static Future invalidJsonToClient() { |
55 MockSocket mockSocket = new MockSocket(); | 64 socket.twin.add('{"foo":"bar"}'); |
56 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); | 65 server.sendResponse(new Response('myId')); |
57 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); | 66 return client.responseStream |
58 var responsesReceived = new List(); | 67 .first |
59 var notificationsReceived = new List(); | 68 .timeout(new Duration(seconds: 1)) |
60 client.listen((Response response) => responsesReceived.add(response), | 69 .then((Response response) { |
61 (Notification notification) => notificationsReceived.add(notification)); | 70 expect(response.id, equals('myId')); |
| 71 expectMsgCount(responseCount: 1); |
| 72 }); |
| 73 } |
62 | 74 |
| 75 static Future invalidJsonToServer() { |
| 76 socket.add('"blat"'); |
| 77 return client.responseStream |
| 78 .first |
| 79 .timeout(new Duration(seconds: 1)) |
| 80 .then((Response response) { |
| 81 expect(response.id, equals('')); |
| 82 expect(response.error, isNotNull); |
| 83 expectMsgCount(responseCount: 1); |
| 84 }); |
| 85 } |
| 86 |
| 87 static Future notification() { |
63 server.sendNotification(new Notification('myEvent')); | 88 server.sendNotification(new Notification('myEvent')); |
| 89 return client.notificationStream |
| 90 .first |
| 91 .timeout(new Duration(seconds: 1)) |
| 92 .then((Notification notification) { |
| 93 expect(notification.event, equals('myEvent')); |
| 94 expectMsgCount(notificationCount: 1); |
64 | 95 |
65 expect(responsesReceived.length, equals(0)); | 96 expect(notificationsReceived.first, equals(notification)); |
66 expect(notificationsReceived.length, equals(1)); | 97 }); |
67 expect(notificationsReceived.first.runtimeType, equals(Notification)); | 98 } |
68 Notification actual = notificationsReceived.first; | 99 |
69 expect(actual.event, equals('myEvent')); | 100 static Future notificationAndResponse() { |
| 101 server |
| 102 ..sendNotification(new Notification('myEvent')) |
| 103 ..sendResponse(new Response('myId')); |
| 104 return Future |
| 105 .wait([ |
| 106 client.notificationStream.first, |
| 107 client.responseStream.first]) |
| 108 .timeout(new Duration(seconds: 1)) |
| 109 .then((_) => expectMsgCount(responseCount: 1, notificationCount: 1)); |
70 } | 110 } |
71 | 111 |
72 static void request() { | 112 static void request() { |
73 MockSocket mockSocket = new MockSocket(); | 113 client.sendRequest(new Request('myId', 'myMth')); |
74 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); | 114 server.listen((Request request) { |
75 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); | 115 expect(request.id, equals('myId')); |
76 var requestsReceived = new List(); | 116 expect(request.method, equals('myMth')); |
77 server.listen((Request request) => requestsReceived.add(request)); | 117 expectMsgCount(requestCount: 1); |
78 | 118 }); |
79 client.sendRequest(new Request('myId', 'aMethod')); | |
80 | |
81 expect(requestsReceived.length, equals(1)); | |
82 expect(requestsReceived.first.runtimeType, equals(Request)); | |
83 Request actual = requestsReceived.first; | |
84 expect(actual.id, equals('myId')); | |
85 expect(actual.method, equals('aMethod')); | |
86 } | 119 } |
87 | 120 |
88 static void response() { | 121 static Future requestResponse() { |
89 MockSocket mockSocket = new MockSocket(); | 122 // Simulate server sending a response by echoing the request. |
90 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); | 123 server.listen((Request request) => |
91 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); | 124 server.sendResponse(new Response(request.id))); |
92 var responsesReceived = new List(); | 125 return client.sendRequest(new Request('myId', 'myMth')) |
93 var notificationsReceived = new List(); | 126 .timeout(new Duration(seconds: 1)) |
94 client.listen((Response response) => responsesReceived.add(response), | 127 .then((Response response) { |
95 (Notification notification) => notificationsReceived.add(notification)); | 128 expect(response.id, equals('myId')); |
| 129 expectMsgCount(requestCount: 1, responseCount: 1); |
96 | 130 |
| 131 expect(requestsReceived.first is Request, isTrue); |
| 132 Request request = requestsReceived.first; |
| 133 expect(request.id, equals('myId')); |
| 134 expect(request.method, equals('myMth')); |
| 135 expect(responsesReceived.first, equals(response)); |
| 136 }); |
| 137 } |
| 138 |
| 139 static Future response() { |
97 server.sendResponse(new Response('myId')); | 140 server.sendResponse(new Response('myId')); |
| 141 return client.responseStream |
| 142 .first |
| 143 .timeout(new Duration(seconds: 1)) |
| 144 .then((Response response) { |
| 145 expect(response.id, equals('myId')); |
| 146 expectMsgCount(responseCount: 1); |
| 147 }); |
| 148 } |
98 | 149 |
99 expect(responsesReceived.length, equals(1)); | 150 static void expectMsgCount({requestCount: 0, |
100 expect(notificationsReceived.length, equals(0)); | 151 responseCount: 0, |
101 expect(responsesReceived.first.runtimeType, equals(Response)); | 152 notificationCount: 0}) { |
102 Response actual = responsesReceived.first; | 153 expect(requestsReceived, hasLength(requestCount)); |
103 expect(actual.id, equals('myId')); | 154 expect(responsesReceived, hasLength(responseCount)); |
| 155 expect(notificationsReceived, hasLength(notificationCount)); |
104 } | 156 } |
105 } | 157 } |
OLD | NEW |