def test_auto_reconnect(mock_time, monkeypatch):
    mock_connection(monkeypatch)
    connection = Connection(connection_params)
    connection.is_alive = MagicMock(side_effect=[False, False, True])

    connection.auto_reconnect()

    assert connection.is_alive.call_count == 3
    mock_time.assert_has_calls(
        [call.sleep(connection.SEC_SLEEP),
         call.sleep(connection.SEC_SLEEP)])
Exemple #2
0
def test_is_alive():
    open_mock = type('OpenMock', (object, ), {'is_open': True})
    close_mock = type('CloseMock', (object, ), {'is_open': False})
    connection = Connection({})
    connection._connection = None
    connection._channel = open_mock
    assert connection.is_alive() is False

    connection._connection = open_mock
    connection._channel = None
    assert connection.is_alive() is False

    connection._channel = open_mock
    assert connection.is_alive() is True

    connection._connection = close_mock
    assert connection.is_alive() is False

    connection._channel = close_mock
    assert connection.is_alive() is False

    connection._connection = open_mock
    assert connection.is_alive() is False