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