def register(self, service_provider: ServiceProvider) -> None: """ Called by the ServiceProvider to allow init and registration of service handler methods """ self._service_provider = service_provider self._logger = service_provider.logger self._server = service_provider.server self.operations_queue = OperationsQueue(service_provider) self.operations_queue.start() # Register request handlers self._server.set_request_handler(COMPLETION_REQUEST, self.handle_completion_request) self._server.set_request_handler(DEFINITION_REQUEST, self.handle_definition_request) self._server.set_request_handler( COMPLETION_RESOLVE_REQUEST, self.handle_completion_resolve_request) self._server.set_request_handler(DOCUMENT_FORMATTING_REQUEST, self.handle_doc_format_request) self._server.set_request_handler(DOCUMENT_RANGE_FORMATTING_REQUEST, self.handle_doc_range_format_request) self._server.set_notification_handler( LANGUAGE_FLAVOR_CHANGE_NOTIFICATION, self.handle_flavor_change) # Register internal service notification handlers self._connection_service.register_on_connect_callback(self.on_connect) self._service_provider.server.add_shutdown_handler( self._handle_shutdown)
def test_start_process_stop(self): operations_queue = OperationsQueue(self.mock_service_provider) operations_queue.start() self.assertTrue(operations_queue._operations_consumer.isAlive()) operations_queue.stop() operations_queue._operations_consumer.join(2) self.assertFalse(operations_queue._operations_consumer.isAlive())
def test_execute_operation_calls_timeout_if_not_connected(self): # Given an operation with a non-connected context task = mock.Mock() timeout_task = mock.Mock() operations_queue = OperationsQueue(self.mock_service_provider) operation = QueuedOperation(self.expected_context_key, task, timeout_task) operation.context = ConnectionContext(self.expected_context_key) # When I execute the operation operations_queue.execute_operation(operation) # Then I expect the timeout task to be called timeout_task.assert_called_once() # ... and I do not expect the regular task to be called task.assert_not_called()
def test_add_operation_succeeds_when_has_key(self): # Given I have a connection in the map operations_queue = OperationsQueue(self.mock_service_provider) operations_queue._context_map[ self.expected_context_key] = ConnectionContext( self.expected_context_key) # When I add an operation operations_queue.add_operation( QueuedOperation(self.expected_context_key, None, None)) # Then I expect the operation to be added successfully to the queue operation: QueuedOperation = operations_queue.queue.get_nowait() self.assertEqual(operation.key, self.expected_context_key) # ... and I expect the context to have been set automatically self.assertEqual( operation.context, operations_queue._context_map[self.expected_context_key])
def do_test(): # When I add context for 2 URIs with same connection details operations_queue = OperationsQueue(self.mock_service_provider) with mock.patch(COMPLETIONREFRESHER_PATH_PATH) as refresher_patch: refresher_patch.return_value = self.refresher_mock operations_queue.add_connection_context(self.connection_info) conn_info2 = ConnectionInfo('newuri', self.connection_info.details) operations_queue.add_connection_context(conn_info2) # Then I expect to only have connection connect_mock: mock.MagicMock = self.mock_connection_service.connect connect_mock.assert_called_once() connect_params: ConnectRequestParams = connect_mock.call_args[ 0][0] self.assertEqual(connect_params.owner_uri, self.expected_connection_uri)
def test_execute_operation_calls_task_and_timouttask_if_task_fails(self): # Given an operation where the task will fail (return false) context = ConnectionContext(self.expected_context_key) context.is_connected = True context.completer = mock.Mock() task = mock.MagicMock(return_value=False) timeout_task = mock.Mock() operations_queue = OperationsQueue(self.mock_service_provider) operation = QueuedOperation(self.expected_context_key, task, timeout_task) operation.context = context # When I execute the operation operations_queue.execute_operation(operation) # Then I expect the regular task to be called task.assert_called_once() actual_context = task.call_args[0][0] self.assertEqual(actual_context, context) # ... and I also expect the timeout task to be called timeout_task.assert_called_once()
def test_add_context_creates_new_context(self): # Given a connection will be created on a connect request connect_result = mock.MagicMock() connect_result.error_message = None self.mock_connection_service.get_connection = mock.Mock( return_value=mock.MagicMock()) self.mock_connection_service.connect = mock.MagicMock( return_value=connect_result) # When I add a connection context operations_queue = OperationsQueue(self.mock_service_provider) with mock.patch(COMPLETIONREFRESHER_PATH_PATH) as refresher_patch: refresher_patch.return_value = self.refresher_mock context: ConnectionContext = operations_queue.add_connection_context( self.connection_info) # Then I expect the context to be non-null self.assertIsNotNone(context) self.assertEqual(context.key, self.expected_context_key) self.assertFalse(context.intellisense_complete.is_set()) self.assertTrue( operations_queue.has_connection_context(self.connection_info))
def do_test(): # When I add context for 2 URIs with same connection details operations_queue = OperationsQueue(self.mock_service_provider) with mock.patch(COMPLETIONREFRESHER_PATH_PATH) as refresher_patch: refresher_patch.return_value = self.refresher_mock operations_queue.add_connection_context(self.connection_info) conn_info2 = ConnectionInfo('newuri', self.connection_info.details) operations_queue.add_connection_context(conn_info2, overwrite=True) # Then I expect to only have 1 connection # and I expect disconnect and reconnect to have been called connect_mock: mock.MagicMock = self.mock_connection_service.connect self.assertEqual(connect_mock.call_count, 2) self.assertEqual( connect_mock.call_args_list[0][0][0].owner_uri, self.expected_connection_uri) self.assertEqual( connect_mock.call_args_list[1][0][0].owner_uri, self.expected_connection_uri) disconnect_mock: mock.MagicMock = self.mock_connection_service.disconnect disconnect_mock.assert_called_once()
def test_init(self): operations_queue = OperationsQueue(self.mock_service_provider) self.assertFalse(operations_queue.stop_requested) self.assertTrue(operations_queue.queue.empty)
def test_execute_operation_ignores_none_param(self): operations_queue = OperationsQueue(self.mock_service_provider) try: operations_queue.execute_operation(None) except Exception as e: self.fail(e)
def test_add_operation_without_context_throws(self): operations_queue = OperationsQueue(self.mock_service_provider) with self.assertRaises(KeyError): operations_queue.add_operation( QueuedOperation('something', None, None))
def test_add_none_operation_throws(self): operations_queue = OperationsQueue(self.mock_service_provider) with self.assertRaises(ValueError): operations_queue.add_operation(None)