Ejemplo n.º 1
0
def test_nameserver_proxy_shutdown_lazy_agents(delay, timeout):
    """
    Shutdown a name server proxy with agents that wait some time before
    shutting down.

    The name server should always wait for all agents to shut down when no
    timeout is set.
    """
    class Lazy(Agent):
        def shutdown(self):
            time.sleep(delay)
            super().shutdown()

    ns = run_nameserver()
    run_agent('a0', base=Lazy)
    run_agent('a1', base=Lazy)

    t0 = time.time()
    if timeout:
        with pytest.raises(TimeoutError) as error:
            ns.shutdown(timeout=delay - 1)
        assert 'not all agents were shutdown' in str(error.value)
        # Agent should die soon anyway
        assert agent_dies('a0', nsproxy=ns, timeout=2.)
        assert agent_dies('a1', nsproxy=ns, timeout=0.)
    ns.shutdown()
    assert time.time() - t0 > delay
    assert time.time() - t0 < delay + 2
Ejemplo n.º 2
0
def test_linger(nsproxy, linger, sleep_time, should_receive):
    '''
    Test linger works when closing the sockets of an agent.
    '''
    class AgentTest(Agent):
        def on_init(self):
            self.received = []

    osbrain.config['LINGER'] = linger

    puller = run_agent('puller', base=AgentTest)
    pusher = run_agent('pusher', base=AgentTest)

    address = puller.bind('PULL', alias='pull', handler=append_received,
                          transport='tcp')

    pusher.connect(address, alias='push')
    puller.shutdown()
    assert agent_dies('puller', nsproxy)

    pusher.send('push', 'foo')
    pusher.close_all()
    # After this timeout, depending on linger value, 'foo' will no longer be
    # on queue to be sent
    time.sleep(sleep_time)

    puller = run_agent('puller', base=AgentTest)
    puller.bind('PULL', alias='pull', handler=append_received,
                addr=address.address, transport='tcp')

    assert should_receive == wait_agent_attr(puller, data='foo', timeout=1)
Ejemplo n.º 3
0
def test_agent_dies(nsproxy):
    """
    The function `agent_dies` should return `False` if the agent does not die
    after a timeout.
    """
    run_agent('a0')
    assert not agent_dies('a0', nsproxy, timeout=0.5)
Ejemplo n.º 4
0
def test_multiple_yield(nsproxy):
    """
    A replier must only make use of yield once.
    """
    def yield_twice(agent, message):
        yield message
        yield 'Derp'

    logger = run_logger('logger')
    a0 = run_agent('a0')
    a1 = run_agent('a1')
    a0.set_logger(logger)
    sync_agent_logger(agent=a0, logger=logger)

    addr = a0.bind('REP', handler=yield_twice)
    a1.connect(addr, alias='request')

    response = a1.send_recv('request', 'Hello world!')
    # Response is received successfully
    assert response == 'Hello world!'
    # Replier should crash
    assert logger_received(logger,
                           log_name='log_history_error',
                           message='yielded more than once')
    assert agent_dies(a0, nsproxy)
Ejemplo n.º 5
0
def test_agent_dies(nsproxy):
    """
    The function `agent_dies` should return `False` if the agent does not die
    after a timeout.
    """
    run_agent('a0')
    assert not agent_dies('a0', nsproxy, timeout=0.5)
Ejemplo n.º 6
0
def test_agent_close_ipc_socket_agent_kill(nsproxy):
    """
    Check that the socket is closed and the socket file removed after the agent
    is killed.
    """
    agent = run_agent('name')
    address = agent.bind('PUSH')
    agent.oneway.kill()

    assert agent_dies('name', nsproxy)
    assert wait_condition(address.address.exists, negate=True)
Ejemplo n.º 7
0
def test_agent_close_ipc_socket_agent_kill(nsproxy):
    """
    Check that the socket is closed and the socket file removed after the agent
    is killed.
    """
    agent = run_agent('name')
    address = agent.bind('PUSH')
    agent.oneway.kill()

    assert agent_dies('name', nsproxy)
    assert wait_condition(address.address.exists, negate=True)
Ejemplo n.º 8
0
def test_linger(monkeypatch, nsproxy, linger, sleep_time, should_receive):
    """
    Test linger works when closing the sockets of an agent.
    """
    class AgentTest(Agent):
        def on_init(self):
            self.received = []

    monkeypatch.setitem(osbrain.config, 'LINGER', linger)

    puller = run_agent('puller', base=AgentTest)
    pusher = run_agent('pusher', base=AgentTest)

    address = puller.bind('PULL',
                          alias='pull',
                          handler=append_received,
                          transport='tcp')

    pusher.connect(address, alias='push')

    # Make sure connection is well established
    pusher.send('push', 'ping')
    assert wait_agent_attr(puller, data='ping', timeout=1)

    # Shutdown the puller and restart it without binding
    puller.shutdown()
    assert agent_dies('puller', nsproxy)
    puller = run_agent('puller', base=AgentTest)

    # Push a new message, which should block during linger period
    pusher.send('push', 'foo')
    pusher.close_all()

    # After this timeout, depending on linger value, 'foo' will no longer be
    # on queue to be sent
    time.sleep(sleep_time)

    # Bind to receive the message (if still in queue)
    puller.bind(
        'PULL',
        alias='pull',
        handler=append_received,
        addr=address.address,
        transport='tcp',
    )

    assert should_receive == wait_agent_attr(puller, data='foo', timeout=1)