# Path: misc/client.py
# def readline(sock):
# def readexact(sock, size):
#     def __init__(self, sock):
#     def read_status_line(self):
#     def read_headers(self):
#     def encoding(self):
#     def read_body(self):
#     def json(self):
#     def __init__(self, addr):
#     def maybe_connect(self):
#     def putline(self, line=None):
#     def putclose(self, data):
#     def putrequest(self, method, path, query_string=None):
#     def request(self, method, path, query_string=None, headers=None,
#                 body=None):
#     def putheader(self, name, value):
#     def endheaders(self, body=None):
#     def getresponse(self):
#     def close(self):
# def chunked_encoder(data):
# class Response:
# class Connection:


from functools import partial
from misc import client
import time
import pytest
import integration_tests.common




pytestmark = pytest.mark.needs_build


@pytest.fixture(scope='function', params=[2, 3, 4])
def get_connections_and_wait(request):
    server, process = integration_tests.common.start_server([
        'integration_tests/reaper.py', '1', str(request.param)], path='.test',
        return_process=True)

    def connection_num():
        return len(
            set(c.fd for c in process.connections()) |
            set(c.fd for p in process.children() for c in p.connections()))

    yield connection_num, partial(time.sleep, request.param)

    server.terminate()
    assert server.wait() == 0


def test_empty(get_connections_and_wait):
    get_connections, wait = get_connections_and_wait

# Path: misc/client.py
# def readline(sock):
# def readexact(sock, size):
#     def __init__(self, sock):
#     def read_status_line(self):
#     def read_headers(self):
#     def encoding(self):
#     def read_body(self):
#     def json(self):
#     def __init__(self, addr):
#     def maybe_connect(self):
#     def putline(self, line=None):
#     def putclose(self, data):
#     def putrequest(self, method, path, query_string=None):
#     def request(self, method, path, query_string=None, headers=None,
#                 body=None):
#     def putheader(self, name, value):
#     def endheaders(self, body=None):
#     def getresponse(self):
#     def close(self):
# def chunked_encoder(data):
# class Response:
# class Connection:


# Path: integration_tests/test_reaper.py
from functools import partial
from misc import client
import time
import pytest
import integration_tests.common





pytestmark = pytest.mark.needs_build


@pytest.fixture(scope='function', params=[2, 3, 4])
def get_connections_and_wait(request):
    server, process = integration_tests.common.start_server([
        'integration_tests/reaper.py', '1', str(request.param)], path='.test',
        return_process=True)

    def connection_num():
        return len(
            set(c.fd for c in process.connections()) |
            set(c.fd for p in process.children() for c in p.connections()))

    yield connection_num, partial(time.sleep, request.param)

    server.terminate()
    assert server.wait() == 0


def test_empty(get_connections_and_wait):
    get_connections, wait = get_connections_and_wait

    conn = client.Connection('localhost:8080')