Example #1
0
def test_subscribe(monkeypatch):
    # Test subscription and message receipt
    mock_connection(monkeypatch, connection_success=True)

    # connection, exchange, queue, key, msg
    def on_message(self, exchange, queue, key, body):
        assert (key == "mykey")
        assert (body == "mybody")

    connection = AsyncConnection(connection_params)
    connection.connect()
    queue = {"exchange": "exchange", "name": "name", "key": "key"}
    connection.subscribe([queue, queue], on_message)
    connection.publish(queue, "key", "mybody")
    assert (consumed_message == "mybody")

    # connection, exchange, queue, key, msg
    def on_message_fail(self, exchange, queue, key, body):
        raise Exception

    connection.subscribe([queue, queue], on_message_fail)
    connection.publish(queue, "key", "mybody")
    assert (consumed_message == "mybody")

    connection.disconnect()
Example #2
0
def test_connect_success(monkeypatch):
    # Test if connect reports connection success
    mock_connection(monkeypatch, connection_success=True)

    connection = AsyncConnection(connection_params)
    assert (connection.connect() is True)
    connection.disconnect()
Example #3
0
def test_publish(monkeypatch):
    # Test publish message
    mock_connection(monkeypatch, connection_success=True)

    connection = AsyncConnection(connection_params)
    connection.connect()
    queue = {"exchange": "exchange", "name": "name", "key": "key"}
    connection.publish(queue, "key", "message")
    assert (published_message == json.dumps("message"))
    connection.disconnect()
Example #4
0
def test_connect_callback_success(monkeypatch):
    # Test if connect calls callback on success
    mock_connection(monkeypatch, connection_success=True)
    global on_connect_called

    assert (not on_connect_called)
    connection = AsyncConnection(connection_params)
    connection.connect(on_connect)
    assert (on_connect_called)
    connection.disconnect()
Example #5
0
def test_idempotent_disconnect():
    # Test disconnect repeated execution
    connection = AsyncConnection('')
    assert (connection.disconnect() is None)
    assert (connection.disconnect() is None)
Example #6
0
def test_disconnect():
    # Test if a disconnect can be called without an earlier connect
    connection = AsyncConnection('')
    assert (connection.disconnect() is None)