import asyncio
import gc
import logging
import socket

from json import loads

import pytest

from aiologstash import create_tcp_handler

asyncio.set_event_loop(None)


logging.getLogger().setLevel(logging.DEBUG)


def unused_port():
    """Return a port that is unused on the current host."""
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(('127.0.0.1', 0))
        return s.getsockname()[1]


@pytest.fixture
def event_loop(request):
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    yield loop

    loop.close()
    gc.collect()
    asyncio.set_event_loop(None)
    return

    if not loop.is_closed():
        loop.call_soon(loop.stop)
        loop.run_forever()
        loop.close()

    gc.collect()
    asyncio.set_event_loop(None)


@pytest.fixture
def loop(event_loop):
    return event_loop


class FakeTcpServer:
    def __init__(self, loop):
        self.loop = loop
        self.data = bytearray()
        self.server = None
        self.futs = set()

    async def start(self):
        self.server = await asyncio.start_server(self.on_connect,
                                                 host='127.0.0.1',
                                                 loop=self.loop)

    @property
    def port(self):
        return self.server.sockets[0].getsockname()[1]

    @property
    def jsons(self):
        s = self.data.decode('utf8')
        return [loads(i) for i in s.split('\n') if i]

    async def close(self):
        if self.server is None:
            return
        self.server.close()
        await self.server.wait_closed()
        self.server = None

    async def on_connect(self, reader, writer):
        while True:
            data = await reader.read(1024)
            self.data.extend(data)
            for fut in self.futs:
                if not fut.done():
                    fut.set_result(None)

    async def wait(self):
        fut = self.loop.create_future()
        self.futs.add(fut)
        await fut
        self.futs.remove(fut)


@pytest.fixture
def make_tcp_handler(loop):
    servers = []
    handlers = []

    async def go(*args, level=logging.DEBUG, **kwargs):
        server = FakeTcpServer(loop)
        await server.start()
        servers.append(server)
        handler = await create_tcp_handler('127.0.0.1', server.port, **kwargs)
        handlers.append(handler)
        return handler, server

    yield go

    async def finalize():
        for handler in handlers:
            handler.close()
            await handler.wait_closed()

        for server in servers:
            await server.close()
    loop.run_until_complete(finalize())


@pytest.fixture
def setup_logger(make_tcp_handler):
    async def go(*args, **kwargs):
        handler, server = await make_tcp_handler(*args, **kwargs)
        logger = logging.getLogger('aiologstash_test')
        logger.addHandler(handler)
        return logger, server
    yield go

@@ -46,16 +46,27 @@
 t socket
+%0Aimport sys
 %0A%0Afrom j
@@ -219,16 +219,312 @@
 EBUG)%0A%0A%0A
+if sys.version_info %3E= (3, 5, 2):%0A    def create_future(loop):%0A        return loop.create_future()%0Aelse:%0A    def create_future(loop):  # pragma: no cover%0A        %22%22%22Compatibility wrapper for the loop.create_future() call introduced in%0A        3.5.2.%22%22%22%0A        return asyncio.Future(loop=loop)%0A%0A%0A
 def unus
@@ -2240,18 +2240,8 @@
 t = 
-self.loop.
 crea
@@ -2250,16 +2250,25 @@
 _future(
+self.loop
 )%0A      

Fix tests on python 3.5.1