Exemple #1
0
def test_agent_address():
    """
    Test basic AgentAddress operations: initialization, equivalence and
    basic methods.
    """
    address = AgentAddress('ipc', 'addr', 'PUSH', 'server', 'pickle')
    # Equivalence
    assert address == AgentAddress('ipc', 'addr', 'PUSH', 'server', 'pickle')
    assert not address == 'foo'
    assert address != 'foo'
    # Basic methods
    assert address.twin() == AgentAddress('ipc', 'addr', 'PULL', 'client',
                                          'pickle')
Exemple #2
0
def test_agentchannel_uuid():
    """
    An AgentChannel's unique identifier must never contain the
    `osbrain.TOPIC_SEPARATOR` to avoid errors when using this identifier as
    topic in PUB-SUB communication patterns.
    """
    sender = AgentAddress('ipc', 'addr0', 'PUB', 'server', 'pickle')
    receiver = AgentAddress('ipc', 'addr0', 'PULL', 'server', 'pickle')
    for _ in range(1000):
        channel = AgentChannel('SYNC_PUB', sender=sender, receiver=receiver)
        identifier = channel.uuid
        assert isinstance(identifier, bytes)
        assert TOPIC_SEPARATOR not in identifier
Exemple #3
0
def test_agent_address():
    """
    Test basic AgentAddress operations: initialization, equivalence and
    basic methods.
    """
    address = AgentAddress('ipc', 'addr', 'PUSH', 'server', 'pickle')
    # Equivalence
    assert address == AgentAddress('ipc', 'addr', 'PUSH', 'server', 'pickle')
    assert not address == 'foo'
    assert address != 'foo'
    # Basic methods
    assert address.twin() == AgentAddress('ipc', 'addr', 'PULL', 'client',
                                          'pickle')
Exemple #4
0
def test_agent_address_explicit_serializer():
    """
    Test basic AgentAddress operations: initialization, equivalence and
    basic methods when an explicit serializer is used.
    """
    address = AgentAddress('ipc', 'addr', 'PUSH', 'server', 'raw')
    # Equivalence
    assert address == AgentAddress('ipc', 'addr', 'PUSH', 'server', 'raw')
    assert not address == 'foo'
    assert address != 'foo'
    # Basic methods
    assert address.twin() == AgentAddress('ipc', 'addr', 'PULL', 'client',
                                          'raw')
    assert address.twin() != AgentAddress('ipc', 'addr', 'PULL', 'client',
                                          'pickle')
Exemple #5
0
def test_agentchannel_async_rep():
    """
    Test basic ASYNC_REP AgentChannel operations: initialization, equivalence
    and basic methods.
    """
    receiver = AgentAddress('ipc', 'addr0', 'PULL', 'server', 'pickle')
    channel = AgentChannel('ASYNC_REP', receiver=receiver, sender=None)
    # Equivalence
    assert channel == AgentChannel('ASYNC_REP', receiver=receiver, sender=None)
    assert not channel == 'foo'
    assert channel != 'foo'
    # Basic methods
    assert channel.twin() == AgentChannel('ASYNC_REQ', sender=receiver.twin(),
                                          receiver=None)
    # Other attributes
    assert hasattr(channel, 'uuid')
    assert channel.transport == 'ipc'
    assert channel.serializer == 'pickle'
Exemple #6
0
def test_agentchannel_async_rep():
    """
    Test basic ASYNC_REP AgentChannel operations: initialization, equivalence
    and basic methods.
    """
    receiver = AgentAddress('ipc', 'addr0', 'PULL', 'server', 'pickle')
    channel = AgentChannel('ASYNC_REP', receiver=receiver, sender=None)
    # Equivalence
    assert channel == AgentChannel('ASYNC_REP', receiver=receiver, sender=None)
    assert not channel == 'foo'
    assert channel != 'foo'
    # Basic methods
    assert channel.twin() == AgentChannel(
        'ASYNC_REQ', sender=receiver.twin(), receiver=None
    )
    # Other attributes
    assert hasattr(channel, 'uuid')
    assert channel.transport == 'ipc'
    assert channel.serializer == 'pickle'
Exemple #7
0
def test_agentchannel_sync_pub():
    """
    Test basic SYNC_PUB AgentChannel operations: initialization, equivalence
    and basic methods.
    """
    sender = AgentAddress('ipc', 'addr0', 'PUB', 'server', 'pickle')
    receiver = AgentAddress('ipc', 'addr0', 'PULL', 'server', 'pickle')
    channel = AgentChannel('SYNC_PUB', sender=sender, receiver=receiver)
    # Equivalence
    assert channel == AgentChannel('SYNC_PUB', sender=sender,
                                   receiver=receiver)
    assert not channel == 'foo'
    assert channel != 'foo'
    # Basic methods
    assert channel.twin() == AgentChannel('SYNC_SUB', sender=receiver.twin(),
                                          receiver=sender.twin())
    # Other attributes
    assert hasattr(channel, 'uuid')
    assert channel.transport == 'ipc'
    assert channel.serializer == 'pickle'
Exemple #8
0
def test_agent_address_explicit_serializer():
    """
    Test basic AgentAddress operations: initialization, equivalence and
    basic methods when an explicit serializer is used.
    """
    address = AgentAddress('ipc', 'addr', 'PUSH', 'server', 'raw')
    # Equivalence
    assert address == AgentAddress('ipc', 'addr', 'PUSH', 'server', 'raw')
    assert not address == 'foo'
    assert address != 'foo'
    # Basic methods
    assert address.twin() == AgentAddress('ipc', 'addr', 'PULL', 'client',
                                          'raw')
    assert address.twin() != AgentAddress('ipc', 'addr', 'PULL', 'client',
                                          'pickle')
Exemple #9
0
def test_agentchannel_sync_pub():
    """
    Test basic SYNC_PUB AgentChannel operations: initialization, equivalence
    and basic methods.
    """
    sender = AgentAddress('ipc', 'addr0', 'PUB', 'server', 'pickle')
    receiver = AgentAddress('ipc', 'addr0', 'PULL', 'server', 'pickle')
    channel = AgentChannel('SYNC_PUB', sender=sender, receiver=receiver)
    # Equivalence
    assert channel == AgentChannel(
        'SYNC_PUB', sender=sender, receiver=receiver
    )
    assert not channel == 'foo'
    assert channel != 'foo'
    # Basic methods
    assert channel.twin() == AgentChannel(
        'SYNC_SUB', sender=receiver.twin(), receiver=sender.twin()
    )
    # Other attributes
    assert hasattr(channel, 'uuid')
    assert channel.transport == 'ipc'
    assert channel.serializer == 'pickle'
Exemple #10
0
def test_agent_address_to_host_port():
    """
    An agent address should be convertible to host+port if TCP is used.
    """
    address = AgentAddress('tcp', '127.0.0.1:1234', 'PUSH', 'server', 'pickle')
    assert address_to_host_port(address) == ('127.0.0.1', 1234)
Exemple #11
0
def test_invalid_address_to_host_port():
    """
    Test conversion of a wrong type to its corresponding host+port tuple.
    This conversion should cause an exception raising.
    """
    with pytest.raises(ValueError):
        address_to_host_port(1.234)


@pytest.mark.parametrize(
    ('address', 'host', 'port'),
    [
        (None, None, None),
        (
            AgentAddress('tcp', '127.0.0.1:123', 'PUSH', 'server', 'pickle'),
            '127.0.0.1',
            123,
        ),
        (SocketAddress('127.0.0.1', 123), '127.0.0.1', 123),
        ('127.0.0.1:123', '127.0.0.1', 123),
        ('127.0.0.1', '127.0.0.1', None),
        (
            namedtuple('Foo', ['host', 'port'])('127.0.0.1', 123),
            '127.0.0.1',
            123,
        ),
    ],
)
def test_valid_address_to_host_port(address, host, port):
    """