Exemplo n.º 1
0
def test_add_to_file_cache(lock):
    """Test trollmoves.client.add_to_file_cache()."""
    from trollmoves.client import add_to_file_cache, file_cache

    # Clear file cache, the other tests have added stuff in it
    file_cache.clear()

    # Mock the lock context manager
    lock_cm = MagicMock()
    lock.__enter__ = lock_cm

    # Add a file to cache
    add_to_file_cache(MSG_FILE1)
    lock_cm.assert_called_once()
    assert len(file_cache) == 1
    assert MSG_FILE1.data['uid'] in file_cache

    # Add the same file again
    add_to_file_cache(MSG_FILE1)
    assert len(lock_cm.mock_calls) == 2
    # The file should be there only once
    assert len(file_cache) == 1
    assert MSG_FILE1.data['uid'] in file_cache

    # Add another file
    add_to_file_cache(MSG_FILE2)
    assert len(lock_cm.mock_calls) == 3
    assert len(file_cache) == 2
    assert MSG_FILE2.data['uid'] in file_cache
Exemplo n.º 2
0
def test_request_push(send_ack, send_request, terminate_transfers,
                      ongoing_transfers, add_to_ongoing):
    """Test trollmoves.client.request_push()."""
    from trollmoves.client import request_push, file_cache
    from tempfile import gettempdir

    # Clear file cache, the other tests have added stuff in it
    file_cache.clear()

    ongoing_transfers[UID_FILE2].pop.return_value = MSG_FILE2
    send_request.return_value = [MSG_FILE2, 'localhost']
    publisher = MagicMock()
    kwargs = {'transfer_req_timeout': 1.0, 'req_timeout': 1.0}

    request_push(MSG_FILE2, gettempdir(), 'login', publisher=publisher,
                 **kwargs)

    send_request.assert_called_once()
    send_ack.assert_not_called()
    # The file should be added to ongoing transfers
    add_to_ongoing.assert_called_once()
    # And removed
    ongoing_transfers[UID_FILE2].pop.assert_called_once()
    # The transferred file should be in the cache
    assert MSG_FILE2.data['uid'] in file_cache
    assert len(file_cache) == 1

    # Request the same file again. Now the transfer should not be
    # started again, and `send_ack()` should be called.
    request_push(MSG_FILE2, gettempdir(), 'login', publisher=publisher,
                 **kwargs)

    send_ack.assert_called_once()
    send_request.assert_called_once()