1 """Issue #143 - Socket timeouts in wsgi server not caught.
2 https://bitbucket.org/eventlet/eventlet/issue/143/
4 This file intentionally ignored by nose.
5 Caller process (tests.wsgi_test.TestWsgiConnTimeout) handles success / failure
8 Simulate server connection socket timeout without actually waiting.
9 Logs 'timed out' if server debug=True (similar to 'accepted' logging)
11 FAIL: if log (ie, _spawn_n_impl 'except:' catches timeout, logs TB)
12 NOTE: timeouts are NOT on server_sock, but on the conn sockets produced
13 by the socket.accept() call
15 server's socket.listen() sock - NaughtySocketAcceptWrap
19 server / client accept() conn - ExplodingConnectionWrap
23 connection makefile() file objects - ExplodingSocketFile <-- these raise
28 from eventlet.support import six
29 import tests.wsgi_test
32 # no standard tests in this file, ignore
36 TAG_BOOM = "=== ~* BOOM *~ ==="
41 class BufferLog(object):
44 output_buffer.append(s.rstrip())
48 # This test might make you wince
49 class NaughtySocketAcceptWrap(object):
50 # server's socket.accept(); patches resulting connection sockets
52 def __init__(self, sock):
54 self.sock._really_accept = self.sock.accept
55 self.sock.accept = self
59 self.sock.accept = self.sock._really_accept
60 del self.sock._really_accept
61 for conn_wrap in self.conn_reg:
65 output_buffer.append("ca-click")
66 for i in self.conn_reg:
70 output_buffer.append(self.__class__.__name__ + ".__call__")
71 conn, addr = self.sock._really_accept()
72 self.conn_reg.append(ExplodingConnectionWrap(conn))
76 class ExplodingConnectionWrap(object):
77 # new connection's socket.makefile
78 # eventlet *tends* to use socket.makefile, not raw socket methods.
79 # need to patch file operations
81 def __init__(self, conn):
83 self.conn._really_makefile = self.conn.makefile
84 self.conn.makefile = self
89 self.conn.makefile = self.conn._really_makefile
90 del self.conn._really_makefile
93 output_buffer.append("tick")
94 for i in self.file_reg:
97 def __call__(self, mode='r', bufsize=-1):
98 output_buffer.append(self.__class__.__name__ + ".__call__")
99 # file_obj = self.conn._really_makefile(*args, **kwargs)
100 file_obj = ExplodingSocketFile(self.conn._sock, mode, bufsize)
101 self.file_reg.append(file_obj)
105 class ExplodingSocketFile(eventlet.greenio._fileobject):
107 def __init__(self, sock, mode='rb', bufsize=-1, close=False):
108 args = [bufsize, close] if six.PY2 else []
109 super(self.__class__, self).__init__(sock, mode, *args)
113 output_buffer.append("beep")
118 output_buffer.append(TAG_BOOM)
119 raise socket.timeout("timed out")
121 def readline(self, *args, **kwargs):
122 output_buffer.append(self.__class__.__name__ + ".readline")
124 return super(self.__class__, self).readline(*args, **kwargs)
128 output_buffer[:] = []
130 server_sock = eventlet.listen(('localhost', 0))
131 server_addr = server_sock.getsockname()
132 sock_wrap = NaughtySocketAcceptWrap(server_sock)
135 eventlet.wsgi.server,
139 site=tests.wsgi_test.Site(),
145 sock1 = eventlet.connect(server_addr)
146 sock1.settimeout(0.1)
147 fd1 = sock1.makefile('rwb')
148 fd1.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
150 tests.wsgi_test.read_http(sock1)
152 # let the server socket ops catch up, set bomb
154 output_buffer.append("arming...")
157 # req #2 - old conn, post-arm - timeout
158 fd1.write(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
161 tests.wsgi_test.read_http(sock1)
162 assert False, 'Expected ConnectionClosed exception'
163 except tests.wsgi_test.ConnectionClosed:
169 # reset streams, then output trapped tracebacks
171 # check output asserts in tests.wsgi_test.TestHttpd
172 # test_143_server_connection_timeout_exception
174 return output_buffer[:]
178 output_normal = step(debug=False)
179 output_debug = step(debug=True)
181 assert "timed out" in output_debug[-1], repr(output_debug)
182 # if the BOOM check fails, it's because our timeout didn't happen
183 # (if eventlet stops using file.readline() to read HTTP headers,
185 assert TAG_BOOM == output_debug[-2], repr(output_debug)
186 assert TAG_BOOM == output_normal[-1], repr(output_normal)
187 assert "Traceback" not in output_debug, repr(output_debug)
188 assert "Traceback" not in output_normal, repr(output_normal)
191 if __name__ == '__main__':