Source

araldo / test / router_test.py

import pytest
from mock import Mock
import araldo.router as sut
from araldo.config import Config
from araldo.plugin_manager import PluginManager


@pytest.fixture
def conf1_name():
    return "testfiles/conf1.yaml"


@pytest.fixture
def config(conf1_name):
    return Config(conf1_name)


@pytest.fixture
def router_config(config):
    return config.settings()["routes"]


@pytest.fixture
def plugin_manager(config):
    return PluginManager(config)


@pytest.fixture
def router(router_config, plugin_manager):
    return sut.Router(router_config, plugin_manager)


@pytest.fixture
def test_instantiation(router):
    assert router._routes == {"redis_1": ["redis_2", "redis_3"]}


def test_existing_route(plugin_manager, router):
    redis_3 = Mock()
    outbound_entries = {"redis_3": redis_3}
    router._outbound = outbound_entries
    message = Mock()
    router.route("redis_1", message)
    assert redis_3.send.called


def test_nonexisting_route(plugin_manager, router):
    redis_3 = Mock()
    outbound_entries = {"redis_3": redis_3}
    router._outbound = outbound_entries
    message = Mock()
    router.route("redis_10", message)
    assert not redis_3.send.called


def test_recover_from_send_error(plugin_manager, router):
    def raise_send(message):
        raise Exception("test exception")

    redis_2 = Mock()
    redis_2.send.side_effect = raise_send
    redis_3 = Mock()
    outbound_entries = {
        "redis_2": redis_2,
        "redis_3": redis_3,
    }
    router._outbound = outbound_entries
    message = Mock()
    router.route("redis_1", message)
    assert redis_2.send.called
    assert redis_3.send.called