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