def test_provide_disconnected_connection_second_time_with_matching_selector_and_matching_checksum( ): mock_service_config = mock.MagicMock() mock_service_config.selector = mock.sentinel.CONFIG_SELECTOR mock_service_config_provider = mock.MagicMock() mock_service_config_provider.provide_for = mock.MagicMock( return_value=mock_service_config) mock_service_config_provider.checksum = mock.MagicMock( return_value=mock.sentinel.CONFIG_CHECKSUM) mock_connection = mock.MagicMock() mock_connection.service_config.checksum = mock.sentinel.CONFIG_CHECKSUM mock_provide_backend_connection = mock.MagicMock( return_value=mock_connection) frontend = Frontend( mock.sentinel.LISTENERS, service_config_provider=mock_service_config_provider, provide_backend_connection=mock_provide_backend_connection) connection1 = frontend.get_connection(mock.sentinel.FILE_NAME1) assert mock_connection.connect.called mock_connection.connect.reset_mock() mock_connection.state = State.Disconnected connection2 = frontend.get_connection(mock.sentinel.FILE_NAME2) assert connection1 is connection2 assert mock_connection.connect.called assert not mock_connection.disconnect.called
def test_frontend_starts_stops_backend(): configure_test_logger() frontend = Frontend([MyListener()]) connection = frontend.get_connection('../scripts/localfile.mydsl') started = datetime.datetime.now() cycles = 0 while connection.state is not State.Disconnected: connection.run(datetime.timedelta(seconds=0.1)) cycles += 1 ended = datetime.datetime.now() _logger.info('Frontend executed %d cycles in %.2f seconds.' % (cycles, (ended - started).total_seconds()))
def test_provide_connection_first_time(): mock_service_config = mock.MagicMock() mock_service_config.selector = mock.sentinel.CONFIG_SELECTOR mock_service_config_provider = mock.MagicMock() mock_service_config_provider.provide_for = mock.MagicMock( return_value=mock_service_config) mock_connection = mock.MagicMock() mock_provide_backend_connection = mock.MagicMock( return_value=mock_connection) frontend = Frontend( mock.sentinel.LISTENERS, service_config_provider=mock_service_config_provider, provide_backend_connection=mock_provide_backend_connection) connection = frontend.get_connection(mock.sentinel.FILE_NAME) mock_service_config_provider.provide_for.assert_called_once_with( mock.sentinel.FILE_NAME) mock_provide_backend_connection.assert_called_once_with( frontend, mock_service_config, mock.sentinel.LISTENERS) assert mock_connection.connect.call_count == 1 assert not mock_connection.disconnect.called assert connection is mock_connection
def test_provide_connection_second_time_with_other_selector(): mock_service_config = mock.MagicMock() mock_service_config.selector = mock.sentinel.CONFIG_SELECTOR mock_service_config_provider = mock.MagicMock() mock_service_config_provider.provide_for = mock.MagicMock( return_value=mock_service_config) mock_connection = mock.MagicMock() mock_provide_backend_connection = mock.MagicMock( return_value=mock_connection) frontend = Frontend( mock.sentinel.LISTENERS, service_config_provider=mock_service_config_provider, provide_backend_connection=mock_provide_backend_connection) frontend.get_connection(mock.sentinel.FILE_NAME1) assert mock_connection.connect.called mock_connection.connect.reset_mock() # now change the selector of the provided service configuration: mock_service_config.selector = mock.sentinel.CONFIG_SELECTOR2 frontend.get_connection(mock.sentinel.FILE_NAME2) assert mock_provide_backend_connection.call_count == 2
def test_provide_connection_second_time_with_matching_selector_and_new_checksum( ): mock_service_config = mock.MagicMock() mock_service_config.selector = mock.sentinel.CONFIG_SELECTOR mock_service_config_provider = mock.MagicMock() mock_service_config_provider.provide_for = mock.MagicMock( return_value=mock_service_config) mock_service_config_provider.checksum = mock.MagicMock( return_value=mock.sentinel.CONFIG_CHECKSUM) mock_connection = mock.MagicMock() mock_connection.service_config.checksum = mock.sentinel.CONFIG_CHECKSUM mock_provide_backend_connection = mock.MagicMock( return_value=mock_connection) frontend = Frontend( mock.sentinel.LISTENERS, service_config_provider=mock_service_config_provider, provide_backend_connection=mock_provide_backend_connection) connection1 = frontend.get_connection(mock.sentinel.FILE_NAME1) assert mock_connection.connect.called mock_connection.reset_mock() # now change the checksum of the provided service configuration: mock_service_config2 = mock.MagicMock() mock_service_config2.selector = mock.sentinel.CONFIG_SELECTOR mock_service_config_provider.provide_for.return_value = mock_service_config2 mock_service_config_provider.checksum.return_value = mock.sentinel.CONFIG_CHECKSUM2 connection2 = frontend.get_connection(mock.sentinel.FILE_NAME2) # new connection: assert connection1 is connection2 assert mock_connection.reconnect.called mock_connection.reconnect.assert_called_once_with(mock_service_config2) assert mock_provide_backend_connection.call_count == 1
def test_provide_connection_unhandled_file(): frontend = Frontend() assert not frontend.get_connection('some.unknown')