import errno

import pytest

from ..green import socket
from ..greenio import socket as green_socket

TIMEOUT = 0.01
BACKLOG = 10


class TestGreenSocket:
    def test_socket_init(self):
        sock = socket.socket()
        assert isinstance(sock, green_socket)

    def test_socket_close(self, gsock):
        gsock.close()

    def test_connect(self, gsock, pub_addr):
        gsock.connect(pub_addr)
        print(gsock.getpeername())
        assert gsock.getpeername()

    def test_connect_timeout(self, gsock, fail_addr):
        gsock.settimeout(TIMEOUT)

        with pytest.raises(socket.timeout):
            gsock.connect(fail_addr)

    def test_connect_ex_timeout(self, gsock, fail_addr):
        gsock.settimeout(TIMEOUT)

        e = gsock.connect_ex(fail_addr)

        if e not in {errno.EHOSTUNREACH, errno.ENETUNREACH}:
            assert e == errno.EAGAIN

    def test_accept_timeout(self, gsock):
        gsock.settimeout(TIMEOUT)
        gsock.bind(('', 0))
        gsock.listen(BACKLOG)

        with pytest.raises(socket.timeout):
            gsock.accept()

    def test_recv_timeout(self, gsock, pub_addr):
        gsock.settimeout(TIMEOUT)
        gsock.connect(pub_addr)

        with pytest.raises(socket.timeout) as exc_info:
            gsock.recv(8192)

        assert exc_info.value.args[0] == 'timed out'

    def test_send_to_closed_sock_raises(self, gsock):
        with pytest.raises(BrokenPipeError):
            gsock.send(b'hello')


class TestGreenModule:
    def test_create_connection(self, pub_addr):
        sock = socket.create_connection(pub_addr)

    def test_create_connection_timeout_error(self, fail_addr):
        with pytest.raises(OSError):
            sock = socket.create_connection(fail_addr, timeout=0.01)


@@ -106,16 +106,22 @@
 EOUT
+_SMALL
  = 0.01%0A
 BACK
@@ -116,16 +116,16 @@
  = 0.01%0A
-
 BACKLOG 
@@ -553,32 +553,38 @@
 ttimeout(TIMEOUT
+_SMALL
 )%0A%0A        with 
@@ -741,16 +741,22 @@
 (TIMEOUT
+_SMALL
 )%0A%0A     
@@ -956,32 +956,38 @@
 ttimeout(TIMEOUT
+_SMALL
 )%0A        gsock.
@@ -1171,58 +1171,64 @@
 ock.
-settimeout(TIMEOUT)%0A        gsock.connect(pub_addr
+connect(pub_addr)%0A        gsock.settimeout(TIMEOUT_SMALL
 )%0A%0A 

Fix test by setting timeout after connecting