/tests/net/test_unix.py
Python | 68 lines | 46 code | 21 blank | 1 comment | 5 complexity | cf9944b0af282cf4b4f83d394443c836 MD5 | raw file
1#!/usr/bin/env python 2 3import pytest 4 5import os 6import sys 7import select 8 9if sys.platform in ("win32", "cygwin"): 10 pytest.skip("Test Not Applicable on Windows") 11 12from circuits import Manager 13from circuits.net.sockets import close, connect, write 14from circuits.net.sockets import UNIXServer, UNIXClient 15from circuits.core.pollers import Select, Poll, EPoll, KQueue 16 17from .client import Client 18from .server import Server 19 20 21def pytest_generate_tests(metafunc): 22 metafunc.addcall(funcargs={"Poller": Select}) 23 24 if hasattr(select, "poll"): 25 metafunc.addcall(funcargs={"Poller": Poll}) 26 27 if hasattr(select, "epoll"): 28 metafunc.addcall(funcargs={"Poller": EPoll}) 29 30 if hasattr(select, "kqueue"): 31 metafunc.addcall(funcargs={"Poller": KQueue}) 32 33 34def test_unix(tmpdir, Poller): 35 m = Manager() + Poller() 36 37 sockpath = tmpdir.ensure("test.sock") 38 filename = str(sockpath) 39 40 server = Server() + UNIXServer(filename) 41 client = Client() + UNIXClient() 42 43 server.register(m) 44 client.register(m) 45 46 m.start() 47 48 try: 49 assert pytest.wait_for(server, "ready") 50 assert pytest.wait_for(client, "ready") 51 52 client.fire(connect(filename)) 53 assert pytest.wait_for(client, "connected") 54 assert pytest.wait_for(server, "connected") 55 assert pytest.wait_for(client, "data", b"Ready") 56 57 client.fire(write(b"foo")) 58 assert pytest.wait_for(server, "data", b"foo") 59 60 client.fire(close()) 61 assert pytest.wait_for(client, "disconnected") 62 assert pytest.wait_for(server, "disconnected") 63 64 server.fire(close()) 65 assert pytest.wait_for(server, "closed") 66 finally: 67 m.stop() 68 os.remove(filename)