def test_Client_clean_thread_no_thread(safe_tmpdir):
    """
    The client will ignore an attempt to reset an API call if there's no such
    call "in flight".
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    cl.finish_api_call = mock.MagicMock()
    cl.api_threads = {'a': 'b'}
    cl.clean_thread('foo')
    assert len(cl.api_threads) == 1
def test_Client_clean_thread_no_thread(homedir, config, mocker):
    """
    The client will ignore an attempt to reset an API call if there's no such
    call "in flight".
    Using the `config` fixture to ensure the config is written to disk.
    """
    mock_gui = mocker.MagicMock()
    mock_session = mocker.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, homedir)
    cl.finish_api_call = mocker.MagicMock()
    cl.api_threads = {'a': 'b'}
    cl.clean_thread('foo')
    assert len(cl.api_threads) == 1
def test_Client_completed_api_call_with_current_object(safe_tmpdir):
    """
    Ensure that cleanup is performed if an API call returns in the expected
    time.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    mock_thread = mock.MagicMock()
    mock_runner = mock.MagicMock()
    mock_runner.result = 'result'
    mock_runner.current_object = 'current_object'
    mock_timer = mock.MagicMock()
    cl.api_threads = {
        'thread_uuid': {
            'thread': mock_thread,
            'runner': mock_runner,
            'timer': mock_timer,
        }
    }
    cl.clean_thread = mock.MagicMock()
    mock_user_callback = mock.MagicMock()
    cl.completed_api_call('thread_uuid', mock_user_callback)
    cl.clean_thread.assert_called_once_with('thread_uuid')
    mock_user_callback.assert_called_once_with('result',
                                               current_object='current_object')
    mock_timer.stop.assert_called_once_with()
def test_Client_timeout_cleanup_with_current_object(homedir, config, mocker):
    """
    Ensure that cleanup is performed if an API call times out.
    Using the `config` fixture to ensure the config is written to disk.
    """
    mock_gui = mocker.MagicMock()
    mock_session = mocker.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, homedir)
    mock_thread = mocker.MagicMock()
    mock_runner = mocker.MagicMock()
    mock_runner.current_object = 'current_object'
    mock_timer = mocker.MagicMock()
    cl.api_threads = {
        'thread_uuid': {
            'thread': mock_thread,
            'runner': mock_runner,
            'timer': mock_timer,
        }
    }
    cl.clean_thread = mocker.MagicMock()
    mock_user_callback = mocker.MagicMock()
    cl.timeout_cleanup('thread_uuid', mock_user_callback)
    assert mock_runner.i_timed_out is True
    cl.clean_thread.assert_called_once_with('thread_uuid')
    mock_user_callback.assert_called_once_with(current_object='current_object')
def test_Client_completed_api_call_without_current_object(
        homedir, config, mocker):
    """
    Ensure that cleanup is performed if an API call returns in the expected
    time.
    Using the `config` fixture to ensure the config is written to disk.
    """
    mock_gui = mocker.MagicMock()
    mock_session = mocker.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, homedir)
    mock_thread = mocker.MagicMock()
    mock_runner = mocker.MagicMock()
    mock_runner.result = 'result'
    mock_runner.current_object = None
    mock_timer = mocker.MagicMock()
    cl.api_threads = {
        'thread_uuid': {
            'thread': mock_thread,
            'runner': mock_runner,
            'timer': mock_timer,
        }
    }
    cl.clean_thread = mocker.MagicMock()
    mock_user_callback = mocker.MagicMock()
    cl.completed_api_call('thread_uuid', mock_user_callback)
    cl.clean_thread.assert_called_once_with('thread_uuid')
    mock_user_callback.assert_called_once_with('result')
    mock_timer.stop.assert_called_once_with()
def test_Client_clean_thread(safe_tmpdir):
    """
    Cleaning up an existing thread disconnects the timer and removes it from
    the api_threads collection.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    mock_timer = mock.MagicMock()
    cl.api_threads = {
        'foo': {
            'timer': mock_timer,
        }
    }
    cl.clean_thread('foo')
    assert mock_timer.disconnect.call_count == 1
    assert 'foo' not in cl.api_threads
def test_Client_clean_thread(homedir, config, mocker):
    """
    Cleaning up an existing thread disconnects the timer and removes it from
    the api_threads collection.
    Using the `config` fixture to ensure the config is written to disk.
    """
    mock_gui = mocker.MagicMock()
    mock_session = mocker.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, homedir)
    mock_timer = mocker.MagicMock()
    cl.api_threads = {
        'foo': {
            'timer': mock_timer,
        }
    }
    cl.clean_thread('foo')
    assert mock_timer.disconnect.call_count == 1
    assert 'foo' not in cl.api_threads
def test_Client_timeout_cleanup_with_current_object(safe_tmpdir):
    """
    Ensure that cleanup is performed if an API call times out.
    """
    mock_gui = mock.MagicMock()
    mock_session = mock.MagicMock()
    cl = Client('http://localhost', mock_gui, mock_session, str(safe_tmpdir))
    mock_thread = mock.MagicMock()
    mock_runner = mock.MagicMock()
    mock_runner.current_object = 'current_object'
    mock_timer = mock.MagicMock()
    cl.api_threads = {
        'thread_uuid': {
            'thread': mock_thread,
            'runner': mock_runner,
            'timer': mock_timer,
        }
    }
    cl.clean_thread = mock.MagicMock()
    mock_user_callback = mock.MagicMock()
    cl.timeout_cleanup('thread_uuid', mock_user_callback)
    assert mock_runner.i_timed_out is True
    cl.clean_thread.assert_called_once_with('thread_uuid')
    mock_user_callback.assert_called_once_with(current_object='current_object')