PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/net/test_udp.py

https://bitbucket.org/prologic/circuits/
Python | 102 lines | 70 code | 31 blank | 1 comment | 9 complexity | 99e7c7fcdaf9c369d86fcd03fad49823 MD5 | raw file
  1#!/usr/bin/env python
  2
  3import pytest
  4
  5import socket
  6import select
  7
  8from circuits import Manager
  9from circuits.net.events import close, write
 10from circuits.core.pollers import Select, Poll, EPoll, KQueue
 11from circuits.net.sockets import UDPServer, UDPClient, UDP6Server, UDP6Client
 12
 13from .client import Client
 14from .server import Server
 15
 16
 17def wait_host(server):
 18    def checker(obj, attr):
 19        return all((getattr(obj, a) for a in attr))
 20    assert pytest.wait_for(server, ("host", "port"), checker)
 21
 22
 23def _pytest_generate_tests(metafunc, ipv6):
 24    metafunc.addcall(funcargs={"Poller": Select, "ipv6": ipv6})
 25
 26    if hasattr(select, "poll"):
 27        metafunc.addcall(funcargs={"Poller": Poll, "ipv6": ipv6})
 28
 29    if hasattr(select, "epoll"):
 30        metafunc.addcall(funcargs={"Poller": EPoll, "ipv6": ipv6})
 31
 32    if hasattr(select, "kqueue"):
 33        metafunc.addcall(funcargs={"Poller": KQueue, "ipv6": ipv6})
 34
 35
 36def pytest_generate_tests(metafunc):
 37    _pytest_generate_tests(metafunc, ipv6=False)
 38    if socket.has_ipv6:
 39        _pytest_generate_tests(metafunc, ipv6=True)
 40
 41
 42def test_basic(Poller, ipv6):
 43    m = Manager() + Poller()
 44
 45    if ipv6:
 46        udp_server = UDP6Server(("::1", 0))
 47        udp_client = UDP6Client(("::1", 0), channel="client")
 48    else:
 49        udp_server = UDPServer(0)
 50        udp_client = UDPClient(0, channel="client")
 51    server = Server() + udp_server
 52    client = Client() + udp_client
 53
 54    server.register(m)
 55    client.register(m)
 56
 57    m.start()
 58
 59    try:
 60        assert pytest.wait_for(server, "ready")
 61        assert pytest.wait_for(client, "ready")
 62        wait_host(server)
 63
 64        client.fire(write((server.host, server.port), b"foo"))
 65        assert pytest.wait_for(server, "data", b"foo")
 66
 67        client.fire(close())
 68        assert pytest.wait_for(client, "closed")
 69
 70        server.fire(close())
 71        assert pytest.wait_for(server, "closed")
 72    finally:
 73        m.stop()
 74
 75
 76def test_close(Poller, ipv6):
 77    m = Manager() + Poller()
 78    server = Server() + UDPServer(0)
 79    server.register(m)
 80    m.start()
 81
 82    try:
 83        assert pytest.wait_for(server, "ready")
 84        wait_host(server)
 85
 86        host, port = server.host, server.port
 87
 88        server.fire(close())
 89        assert pytest.wait_for(server, "disconnected")
 90
 91        server.unregister()
 92
 93        def test(obj, attr):
 94            return attr not in obj.components
 95        assert pytest.wait_for(m, server, value=test)
 96
 97        server = Server() + UDPServer((host, port))
 98        server.register(m)
 99
100        assert pytest.wait_for(server, "ready", timeout=30.0)
101    finally:
102        m.stop()