# Path: osbrain/agent.py
# def run_agent(
#     name='',
#     nsaddr=None,
#     addr=None,
#     base=Agent,
#     serializer=None,
#     transport=None,
#     safe=None,
#     attributes=None,
# ):
#     """
#     Ease the agent creation process.
# 
#     This function will create a new agent, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str, default is ''
#         Agent name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New agent address, if it is to be fixed.
#     transport : str, AgentAddressTransport, default is None
#         Transport protocol.
#     safe : bool, default is None
#         Use safe calls by default from the Proxy.
#     attributes : dict, default is None
#         A dictionary that defines initial attributes for the agent.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new agent.
#     """
#     if not nsaddr:
#         nsaddr = os.environ.get('OSBRAIN_NAMESERVER_ADDRESS')
#     agent = AgentProcess(
#         name=name,
#         nsaddr=nsaddr,
#         addr=addr,
#         base=base,
#         serializer=serializer,
#         transport=transport,
#         attributes=attributes,
#     )
#     agent_name = agent.start()
# 
#     proxy = Proxy(agent_name, nsaddr, safe=safe)
#     proxy.run()
#     proxy.wait_for_running()
# 
#     return proxy
# 
# Path: osbrain/logging.py
# def run_logger(name, nsaddr=None, addr=None, base=Logger):
#     """
#     Ease the logger creation process.
# 
#     This function will create a new logger, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str
#         Logger name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New logger address, if it is to be fixed.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new logger.
#     """
#     return run_agent(name, nsaddr, addr, base)
# 
# Path: osbrain/helper.py
# def logger_received(
#     logger, message, log_name='log_history_info', position=None, timeout=1.0
# ):
#     """
#     Check if a logger receives a message.
# 
#     Parameters
#     ----------
#     logger : Proxy
#         Proxy to the logger.
#     log_name : str, default is `'log_history_info'`
#         The name of the attribute to look for in the logger.
#     message : anything
#         Message to look for in the log. Can be a partial match. Regular
#         expressions are allowed.
#     position : int, default is None
#         Where to look for the message in the log. If not set, the message
#         will be searched for through all the log.
#     timeout : float
#         After this number of seconds the function will return `False`.
# 
#     Returns
#     -------
#     int
#         The number of times the logger received a message that matched the
#         expression. Can be higher than 1 if `position` is not set.
#     """
#     t0 = time.time()
#     while True:
#         time.sleep(0.01)
#         log_history = logger.get_attr(log_name)
#         if position is not None:
#             log_history = [log_history[position]]
#         matches = regex_count_in_list(message, log_history)
#         if matches:
#             return matches
#         if timeout and time.time() - t0 > timeout:
#             return 0
# 
# Path: osbrain/helper.py
# def sync_agent_logger(agent, logger):
#     """
#     Make sure and agent and a logger are synchronized.
# 
#     An agent is synchronized with its logger when we make sure the logger has
#     started receiving messages from the agent.
# 
#     Parameters
#     ----------
#     agent : Proxy
#         Proxy to the agent.
#     logger : Proxy
#         Proxy to the logger.
#     """
#     message = str(uuid4())
#     delay = 0.01
#     while (
#         not len(logger.get_attr('log_history_info'))
#         or message not in logger.get_attr('log_history_info')[-1]
#     ):
#         message = str(uuid4())
#         agent.log_info(message)
#         time.sleep(delay)
#         delay *= 2
# 
# Path: osbrain/tests/common.py
# def echo_handler(agent, message):
#     return message


import time
from osbrain import run_agent
from osbrain import run_logger
from osbrain.helper import logger_received
from osbrain.helper import sync_agent_logger
from .common import echo_handler
    assert time.time() - t0 < delay / 2.0
    assert response == 'Working!'
    assert a0.get_attr('delay') == delay
    # Sleep so that the replier has had time to update
    time.sleep(delay + 0.5)
    assert a0.get_attr('delay') == 'ok'


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

# Path: osbrain/agent.py
# def run_agent(
#     name='',
#     nsaddr=None,
#     addr=None,
#     base=Agent,
#     serializer=None,
#     transport=None,
#     safe=None,
#     attributes=None,
# ):
#     """
#     Ease the agent creation process.
# 
#     This function will create a new agent, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str, default is ''
#         Agent name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New agent address, if it is to be fixed.
#     transport : str, AgentAddressTransport, default is None
#         Transport protocol.
#     safe : bool, default is None
#         Use safe calls by default from the Proxy.
#     attributes : dict, default is None
#         A dictionary that defines initial attributes for the agent.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new agent.
#     """
#     if not nsaddr:
#         nsaddr = os.environ.get('OSBRAIN_NAMESERVER_ADDRESS')
#     agent = AgentProcess(
#         name=name,
#         nsaddr=nsaddr,
#         addr=addr,
#         base=base,
#         serializer=serializer,
#         transport=transport,
#         attributes=attributes,
#     )
#     agent_name = agent.start()
# 
#     proxy = Proxy(agent_name, nsaddr, safe=safe)
#     proxy.run()
#     proxy.wait_for_running()
# 
#     return proxy
# 
# Path: osbrain/logging.py
# def run_logger(name, nsaddr=None, addr=None, base=Logger):
#     """
#     Ease the logger creation process.
# 
#     This function will create a new logger, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     name : str
#         Logger name or alias.
#     nsaddr : SocketAddress, default is None
#         Name server address.
#     addr : SocketAddress, default is None
#         New logger address, if it is to be fixed.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the new logger.
#     """
#     return run_agent(name, nsaddr, addr, base)
# 
# Path: osbrain/helper.py
# def logger_received(
#     logger, message, log_name='log_history_info', position=None, timeout=1.0
# ):
#     """
#     Check if a logger receives a message.
# 
#     Parameters
#     ----------
#     logger : Proxy
#         Proxy to the logger.
#     log_name : str, default is `'log_history_info'`
#         The name of the attribute to look for in the logger.
#     message : anything
#         Message to look for in the log. Can be a partial match. Regular
#         expressions are allowed.
#     position : int, default is None
#         Where to look for the message in the log. If not set, the message
#         will be searched for through all the log.
#     timeout : float
#         After this number of seconds the function will return `False`.
# 
#     Returns
#     -------
#     int
#         The number of times the logger received a message that matched the
#         expression. Can be higher than 1 if `position` is not set.
#     """
#     t0 = time.time()
#     while True:
#         time.sleep(0.01)
#         log_history = logger.get_attr(log_name)
#         if position is not None:
#             log_history = [log_history[position]]
#         matches = regex_count_in_list(message, log_history)
#         if matches:
#             return matches
#         if timeout and time.time() - t0 > timeout:
#             return 0
# 
# Path: osbrain/helper.py
# def sync_agent_logger(agent, logger):
#     """
#     Make sure and agent and a logger are synchronized.
# 
#     An agent is synchronized with its logger when we make sure the logger has
#     started receiving messages from the agent.
# 
#     Parameters
#     ----------
#     agent : Proxy
#         Proxy to the agent.
#     logger : Proxy
#         Proxy to the logger.
#     """
#     message = str(uuid4())
#     delay = 0.01
#     while (
#         not len(logger.get_attr('log_history_info'))
#         or message not in logger.get_attr('log_history_info')[-1]
#     ):
#         message = str(uuid4())
#         agent.log_info(message)
#         time.sleep(delay)
#         delay *= 2
# 
# Path: osbrain/tests/common.py
# def echo_handler(agent, message):
#     return message


# Path: osbrain/tests/test_agent_req_rep.py
import time
from osbrain import run_agent
from osbrain import run_logger
from osbrain.helper import logger_received
from osbrain.helper import sync_agent_logger
from .common import echo_handler

    assert time.time() - t0 < delay / 2.0
    assert response == 'Working!'
    assert a0.get_attr('delay') == delay
    # Sleep so that the replier has had time to update
    time.sleep(delay + 0.5)
    assert a0.get_attr('delay') == 'ok'


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(