| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analysis_server/protocol/protocol.dart'; | 7 import 'package:analysis_server/protocol/protocol.dart'; |
| 8 import 'package:analysis_server/protocol/protocol_constants.dart'; |
| 8 import 'package:analysis_server/protocol/protocol_generated.dart'; | 9 import 'package:analysis_server/protocol/protocol_generated.dart'; |
| 9 import 'package:analysis_server/src/analysis_server.dart'; | 10 import 'package:analysis_server/src/analysis_server.dart'; |
| 10 import 'package:analysis_server/src/constants.dart'; | |
| 11 import 'package:analysis_server/src/plugin/server_plugin.dart'; | 11 import 'package:analysis_server/src/plugin/server_plugin.dart'; |
| 12 import 'package:analysis_server/src/socket_server.dart'; | 12 import 'package:analysis_server/src/socket_server.dart'; |
| 13 import 'package:analyzer/file_system/physical_file_system.dart'; | 13 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 14 import 'package:analyzer/instrumentation/instrumentation.dart'; | 14 import 'package:analyzer/instrumentation/instrumentation.dart'; |
| 15 import 'package:analyzer/src/dart/sdk/sdk.dart'; | 15 import 'package:analyzer/src/dart/sdk/sdk.dart'; |
| 16 import 'package:analyzer/src/generated/sdk.dart'; | 16 import 'package:analyzer/src/generated/sdk.dart'; |
| 17 import 'package:plugin/manager.dart'; | 17 import 'package:plugin/manager.dart'; |
| 18 import 'package:test/test.dart'; | 18 import 'package:test/test.dart'; |
| 19 | 19 |
| 20 import 'mocks.dart'; | 20 import 'mocks.dart'; |
| 21 | 21 |
| 22 main() { | 22 main() { |
| 23 group('SocketServer', () { | 23 group('SocketServer', () { |
| 24 test('createAnalysisServer_successful', | 24 test('createAnalysisServer_successful', |
| 25 SocketServerTest.createAnalysisServer_successful); | 25 SocketServerTest.createAnalysisServer_successful); |
| 26 test('createAnalysisServer_alreadyStarted', | 26 test('createAnalysisServer_alreadyStarted', |
| 27 SocketServerTest.createAnalysisServer_alreadyStarted); | 27 SocketServerTest.createAnalysisServer_alreadyStarted); |
| 28 test('requestHandler_exception', SocketServerTest.requestHandler_exception); | 28 test('requestHandler_exception', SocketServerTest.requestHandler_exception); |
| 29 test('requestHandler_futureException', | 29 test('requestHandler_futureException', |
| 30 SocketServerTest.requestHandler_futureException); | 30 SocketServerTest.requestHandler_futureException); |
| 31 }); | 31 }); |
| 32 } | 32 } |
| 33 | 33 |
| 34 class SocketServerTest { | 34 class SocketServerTest { |
| 35 static void createAnalysisServer_alreadyStarted() { | 35 static void createAnalysisServer_alreadyStarted() { |
| 36 SocketServer server = _createSocketServer(); | 36 SocketServer server = _createSocketServer(); |
| 37 MockServerChannel channel1 = new MockServerChannel(); | 37 MockServerChannel channel1 = new MockServerChannel(); |
| 38 MockServerChannel channel2 = new MockServerChannel(); | 38 MockServerChannel channel2 = new MockServerChannel(); |
| 39 server.createAnalysisServer(channel1); | 39 server.createAnalysisServer(channel1); |
| 40 expect(channel1.notificationsReceived[0].event, SERVER_CONNECTED); | 40 expect( |
| 41 channel1.notificationsReceived[0].event, SERVER_NOTIFICATION_CONNECTED); |
| 41 server.createAnalysisServer(channel2); | 42 server.createAnalysisServer(channel2); |
| 42 channel1.expectMsgCount(notificationCount: 1); | 43 channel1.expectMsgCount(notificationCount: 1); |
| 43 channel2.expectMsgCount(responseCount: 1); | 44 channel2.expectMsgCount(responseCount: 1); |
| 44 expect(channel2.responsesReceived[0].id, equals('')); | 45 expect(channel2.responsesReceived[0].id, equals('')); |
| 45 expect(channel2.responsesReceived[0].error, isNotNull); | 46 expect(channel2.responsesReceived[0].error, isNotNull); |
| 46 expect(channel2.responsesReceived[0].error.code, | 47 expect(channel2.responsesReceived[0].error.code, |
| 47 equals(RequestErrorCode.SERVER_ALREADY_STARTED)); | 48 equals(RequestErrorCode.SERVER_ALREADY_STARTED)); |
| 48 channel2 | 49 channel2 |
| 49 .sendRequest(new ServerShutdownParams().toRequest('0')) | 50 .sendRequest(new ServerShutdownParams().toRequest('0')) |
| 50 .then((Response response) { | 51 .then((Response response) { |
| 51 expect(response.id, equals('0')); | 52 expect(response.id, equals('0')); |
| 52 expect(response.error, isNotNull); | 53 expect(response.error, isNotNull); |
| 53 expect( | 54 expect( |
| 54 response.error.code, equals(RequestErrorCode.SERVER_ALREADY_STARTED)); | 55 response.error.code, equals(RequestErrorCode.SERVER_ALREADY_STARTED)); |
| 55 channel2.expectMsgCount(responseCount: 2); | 56 channel2.expectMsgCount(responseCount: 2); |
| 56 }); | 57 }); |
| 57 } | 58 } |
| 58 | 59 |
| 59 static Future createAnalysisServer_successful() { | 60 static Future createAnalysisServer_successful() { |
| 60 SocketServer server = _createSocketServer(); | 61 SocketServer server = _createSocketServer(); |
| 61 MockServerChannel channel = new MockServerChannel(); | 62 MockServerChannel channel = new MockServerChannel(); |
| 62 server.createAnalysisServer(channel); | 63 server.createAnalysisServer(channel); |
| 63 channel.expectMsgCount(notificationCount: 1); | 64 channel.expectMsgCount(notificationCount: 1); |
| 64 expect(channel.notificationsReceived[0].event, SERVER_CONNECTED); | 65 expect( |
| 66 channel.notificationsReceived[0].event, SERVER_NOTIFICATION_CONNECTED); |
| 65 return channel | 67 return channel |
| 66 .sendRequest(new ServerShutdownParams().toRequest('0')) | 68 .sendRequest(new ServerShutdownParams().toRequest('0')) |
| 67 .then((Response response) { | 69 .then((Response response) { |
| 68 expect(response.id, equals('0')); | 70 expect(response.id, equals('0')); |
| 69 expect(response.error, isNull); | 71 expect(response.error, isNull); |
| 70 channel.expectMsgCount(responseCount: 1, notificationCount: 1); | 72 channel.expectMsgCount(responseCount: 1, notificationCount: 1); |
| 71 }); | 73 }); |
| 72 } | 74 } |
| 73 | 75 |
| 74 static Future requestHandler_exception() { | 76 static Future requestHandler_exception() { |
| 75 SocketServer server = _createSocketServer(); | 77 SocketServer server = _createSocketServer(); |
| 76 MockServerChannel channel = new MockServerChannel(); | 78 MockServerChannel channel = new MockServerChannel(); |
| 77 server.createAnalysisServer(channel); | 79 server.createAnalysisServer(channel); |
| 78 channel.expectMsgCount(notificationCount: 1); | 80 channel.expectMsgCount(notificationCount: 1); |
| 79 expect(channel.notificationsReceived[0].event, SERVER_CONNECTED); | 81 expect( |
| 82 channel.notificationsReceived[0].event, SERVER_NOTIFICATION_CONNECTED); |
| 80 _MockRequestHandler handler = new _MockRequestHandler(false); | 83 _MockRequestHandler handler = new _MockRequestHandler(false); |
| 81 server.analysisServer.handlers = [handler]; | 84 server.analysisServer.handlers = [handler]; |
| 82 var request = new ServerGetVersionParams().toRequest('0'); | 85 var request = new ServerGetVersionParams().toRequest('0'); |
| 83 return channel.sendRequest(request).then((Response response) { | 86 return channel.sendRequest(request).then((Response response) { |
| 84 expect(response.id, equals('0')); | 87 expect(response.id, equals('0')); |
| 85 expect(response.error, isNotNull); | 88 expect(response.error, isNotNull); |
| 86 expect(response.error.code, equals(RequestErrorCode.SERVER_ERROR)); | 89 expect(response.error.code, equals(RequestErrorCode.SERVER_ERROR)); |
| 87 expect(response.error.message, equals('mock request exception')); | 90 expect(response.error.message, equals('mock request exception')); |
| 88 expect(response.error.stackTrace, isNotNull); | 91 expect(response.error.stackTrace, isNotNull); |
| 89 expect(response.error.stackTrace, isNotEmpty); | 92 expect(response.error.stackTrace, isNotEmpty); |
| 90 channel.expectMsgCount(responseCount: 1, notificationCount: 1); | 93 channel.expectMsgCount(responseCount: 1, notificationCount: 1); |
| 91 }); | 94 }); |
| 92 } | 95 } |
| 93 | 96 |
| 94 static Future requestHandler_futureException() { | 97 static Future requestHandler_futureException() { |
| 95 SocketServer server = _createSocketServer(); | 98 SocketServer server = _createSocketServer(); |
| 96 MockServerChannel channel = new MockServerChannel(); | 99 MockServerChannel channel = new MockServerChannel(); |
| 97 server.createAnalysisServer(channel); | 100 server.createAnalysisServer(channel); |
| 98 _MockRequestHandler handler = new _MockRequestHandler(true); | 101 _MockRequestHandler handler = new _MockRequestHandler(true); |
| 99 server.analysisServer.handlers = [handler]; | 102 server.analysisServer.handlers = [handler]; |
| 100 var request = new ServerGetVersionParams().toRequest('0'); | 103 var request = new ServerGetVersionParams().toRequest('0'); |
| 101 return channel.sendRequest(request).then((Response response) { | 104 return channel.sendRequest(request).then((Response response) { |
| 102 expect(response.id, equals('0')); | 105 expect(response.id, equals('0')); |
| 103 expect(response.error, isNull); | 106 expect(response.error, isNull); |
| 104 channel.expectMsgCount(responseCount: 1, notificationCount: 2); | 107 channel.expectMsgCount(responseCount: 1, notificationCount: 2); |
| 105 expect(channel.notificationsReceived[1].event, SERVER_ERROR); | 108 expect(channel.notificationsReceived[1].event, SERVER_NOTIFICATION_ERROR); |
| 106 }); | 109 }); |
| 107 } | 110 } |
| 108 | 111 |
| 109 static SocketServer _createSocketServer() { | 112 static SocketServer _createSocketServer() { |
| 110 PhysicalResourceProvider resourceProvider = | 113 PhysicalResourceProvider resourceProvider = |
| 111 PhysicalResourceProvider.INSTANCE; | 114 PhysicalResourceProvider.INSTANCE; |
| 112 ServerPlugin serverPlugin = new ServerPlugin(); | 115 ServerPlugin serverPlugin = new ServerPlugin(); |
| 113 ExtensionManager manager = new ExtensionManager(); | 116 ExtensionManager manager = new ExtensionManager(); |
| 114 manager.processPlugins([serverPlugin]); | 117 manager.processPlugins([serverPlugin]); |
| 115 return new SocketServer( | 118 return new SocketServer( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 136 new Future(throwException); | 139 new Future(throwException); |
| 137 return new Response(request.id); | 140 return new Response(request.id); |
| 138 } | 141 } |
| 139 throw 'mock request exception'; | 142 throw 'mock request exception'; |
| 140 } | 143 } |
| 141 | 144 |
| 142 void throwException() { | 145 void throwException() { |
| 143 throw 'mock future exception'; | 146 throw 'mock future exception'; |
| 144 } | 147 } |
| 145 } | 148 } |
| OLD | NEW |