# 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/nameserver.py
# def run_nameserver(addr=None, base=NameServer):
#     """
#     Ease the name server creation process.
# 
#     This function will create a new nameserver, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     addr : SocketAddress, default is None
#         Name server address.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the name server.
#     """
#     if not addr:
#         addr = random_nameserver_process(base=base).addr
#     else:
#         NameServerProcess(addr, base=base).start()
#     return NSProxy(addr)
# 
# 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


import pytest
from osbrain import run_agent
from osbrain import run_logger
from osbrain import run_nameserver
from osbrain.helper import sync_agent_logger



@pytest.fixture(scope='function')
def nsproxy(request):
    ns = run_nameserver()
    yield ns
    ns.shutdown()


@pytest.fixture(scope='function')
def agent_logger(request):
    ns = run_nameserver()
    agent = run_agent('a0')

# 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/nameserver.py
# def run_nameserver(addr=None, base=NameServer):
#     """
#     Ease the name server creation process.
# 
#     This function will create a new nameserver, start the process and then run
#     its main loop through a proxy.
# 
#     Parameters
#     ----------
#     addr : SocketAddress, default is None
#         Name server address.
# 
#     Returns
#     -------
#     proxy
#         A proxy to the name server.
#     """
#     if not addr:
#         addr = random_nameserver_process(base=base).addr
#     else:
#         NameServerProcess(addr, base=base).start()
#     return NSProxy(addr)
# 
# 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/conftest.py
import pytest
from osbrain import run_agent
from osbrain import run_logger
from osbrain import run_nameserver
from osbrain.helper import sync_agent_logger




@pytest.fixture(scope='function')
def nsproxy(request):
    ns = run_nameserver()
    yield ns
    ns.shutdown()


@pytest.fixture(scope='function')
def agent_logger(request):
    ns = run_nameserver()
    agent = run_agent('a0')

    logger = run_logger('logger')