# 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/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 agent_dies(agent, nsproxy, timeout=1.0):
#     """
#     Check if an agent dies within a given period.
# 
#     Parameters
#     ----------
#     agent : str
#         Name of the agent, as registered in the name server.
#     nsproxy : NSProxy
#         Proxy to the name server.
#     timeout : float
#         After this number of seconds the function will return `False`.
# 
#     Returns
#     -------
#     bool
#         Whether the agent died (was unregistered from the name server) within
#         the given period.
#     """
#     assert isinstance(agent, str), 'Wrong type for `agent`!'
#     t0 = time.time()
#     while True:
#         time.sleep(0.01)
#         if agent not in nsproxy.agents():
#             break
#         if timeout and time.time() - t0 > timeout:
#             return False
#     return True
# 
# Path: osbrain/helper.py
# def wait_condition(condition, *args, negate=False, timeout=3, **kwargs):
#     """
#     Wait for a condition to be true.
# 
#     The condition is passed as a callable and must evaluate to a boolean
#     result.
# 
#     Parameters
#     ----------
#     condition : Callable
#         A function that evaluates the desired condition.
#     timeout : float, default is 3
#         After this number of seconds the function will return `False`.
#     negate : bool, default is False
#         Negate the condition (wait for the condition to be false instead).
# 
#     Returns
#     -------
#     bool
#         Whether the specified condition was True.
#     """
#     t0 = time.time()
#     while True:
#         if condition(*args, **kwargs) != negate:
#             return True
#         if time.time() - t0 > timeout:
#             return False
#         time.sleep(0.01)
# 
# Path: osbrain/tests/common.py
# def append_received(agent, message, topic=None):
# def set_received(agent, message, topic=None):
# def echo_handler(agent, message):


import pytest
from osbrain import run_agent
from osbrain import run_nameserver
from osbrain.helper import agent_dies
from osbrain.helper import wait_condition
from .common import skip_windows_ipc
"""
Test file for everything IPC socket related
"""



pytestmark = skip_windows_ipc


def test_agent_close_ipc_socket_agent_shutdown(nsproxy):
    """
    Check that the socket is closed and the socket file removed after the agent
    is shut down.
    """

# 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/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 agent_dies(agent, nsproxy, timeout=1.0):
#     """
#     Check if an agent dies within a given period.
# 
#     Parameters
#     ----------
#     agent : str
#         Name of the agent, as registered in the name server.
#     nsproxy : NSProxy
#         Proxy to the name server.
#     timeout : float
#         After this number of seconds the function will return `False`.
# 
#     Returns
#     -------
#     bool
#         Whether the agent died (was unregistered from the name server) within
#         the given period.
#     """
#     assert isinstance(agent, str), 'Wrong type for `agent`!'
#     t0 = time.time()
#     while True:
#         time.sleep(0.01)
#         if agent not in nsproxy.agents():
#             break
#         if timeout and time.time() - t0 > timeout:
#             return False
#     return True
# 
# Path: osbrain/helper.py
# def wait_condition(condition, *args, negate=False, timeout=3, **kwargs):
#     """
#     Wait for a condition to be true.
# 
#     The condition is passed as a callable and must evaluate to a boolean
#     result.
# 
#     Parameters
#     ----------
#     condition : Callable
#         A function that evaluates the desired condition.
#     timeout : float, default is 3
#         After this number of seconds the function will return `False`.
#     negate : bool, default is False
#         Negate the condition (wait for the condition to be false instead).
# 
#     Returns
#     -------
#     bool
#         Whether the specified condition was True.
#     """
#     t0 = time.time()
#     while True:
#         if condition(*args, **kwargs) != negate:
#             return True
#         if time.time() - t0 > timeout:
#             return False
#         time.sleep(0.01)
# 
# Path: osbrain/tests/common.py
# def append_received(agent, message, topic=None):
# def set_received(agent, message, topic=None):
# def echo_handler(agent, message):


# Path: osbrain/tests/test_agent_ipc_sockets.py
import pytest
from osbrain import run_agent
from osbrain import run_nameserver
from osbrain.helper import agent_dies
from osbrain.helper import wait_condition
from .common import skip_windows_ipc

"""
Test file for everything IPC socket related
"""



pytestmark = skip_windows_ipc


def test_agent_close_ipc_socket_agent_shutdown(nsproxy):
    """
    Check that the socket is closed and the socket file removed after the agent
    is shut down.
    """

    agent = run_agent('name')