| 1 |
import os |
| 2 |
import warnings |
| 3 |
|
| 4 |
import eventlet |
| 5 |
from eventlet import convenience, debug |
| 6 |
from eventlet.green import socket |
| 7 |
import tests |
| 8 |
import tests.mock |
| 9 |
|
| 10 |
|
| 11 |
certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt') |
| 12 |
private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key') |
| 13 |
|
| 14 |
|
| 15 |
class TestServe(tests.LimitedTestCase): |
| 16 |
def setUp(self): |
| 17 |
super().setUp() |
| 18 |
debug.hub_exceptions(False) |
| 19 |
|
| 20 |
def tearDown(self): |
| 21 |
super().tearDown() |
| 22 |
debug.hub_exceptions(True) |
| 23 |
|
| 24 |
def test_exiting_server(self): |
| 25 |
# tests that the server closes the client sock on handle() exit |
| 26 |
def closer(sock, addr): |
| 27 |
pass |
| 28 |
|
| 29 |
l = eventlet.listen(('localhost', 0)) |
| 30 |
gt = eventlet.spawn(eventlet.serve, l, closer) |
| 31 |
client = eventlet.connect(('localhost', l.getsockname()[1])) |
| 32 |
client.sendall(b'a') |
| 33 |
self.assertFalse(client.recv(100)) |
| 34 |
gt.kill() |
| 35 |
|
| 36 |
def test_excepting_server(self): |
| 37 |
# tests that the server closes the client sock on handle() exception |
| 38 |
def crasher(sock, addr): |
| 39 |
sock.recv(1024) |
| 40 |
0 // 0 |
| 41 |
|
| 42 |
l = eventlet.listen(('localhost', 0)) |
| 43 |
gt = eventlet.spawn(eventlet.serve, l, crasher) |
| 44 |
client = eventlet.connect(('localhost', l.getsockname()[1])) |
| 45 |
client.sendall(b'a') |
| 46 |
self.assertRaises(ZeroDivisionError, gt.wait) |
| 47 |
self.assertFalse(client.recv(100)) |
| 48 |
|
| 49 |
def test_excepting_server_already_closed(self): |
| 50 |
# same as above but with explicit clsoe before crash |
| 51 |
def crasher(sock, addr): |
| 52 |
sock.recv(1024) |
| 53 |
sock.close() |
| 54 |
0 // 0 |
| 55 |
|
| 56 |
l = eventlet.listen(('localhost', 0)) |
| 57 |
gt = eventlet.spawn(eventlet.serve, l, crasher) |
| 58 |
client = eventlet.connect(('localhost', l.getsockname()[1])) |
| 59 |
client.sendall(b'a') |
| 60 |
self.assertRaises(ZeroDivisionError, gt.wait) |
| 61 |
self.assertFalse(client.recv(100)) |
| 62 |
|
| 63 |
def test_called_for_each_connection(self): |
| 64 |
hits = [0] |
| 65 |
|
| 66 |
def counter(sock, addr): |
| 67 |
hits[0] += 1 |
| 68 |
l = eventlet.listen(('localhost', 0)) |
| 69 |
gt = eventlet.spawn(eventlet.serve, l, counter) |
| 70 |
for i in range(100): |
| 71 |
client = eventlet.connect(('localhost', l.getsockname()[1])) |
| 72 |
self.assertFalse(client.recv(100)) |
| 73 |
gt.kill() |
| 74 |
self.assertEqual(100, hits[0]) |
| 75 |
|
| 76 |
def test_blocking(self): |
| 77 |
l = eventlet.listen(('localhost', 0)) |
| 78 |
x = eventlet.with_timeout( |
| 79 |
0.01, |
| 80 |
eventlet.serve, l, lambda c, a: None, |
| 81 |
timeout_value="timeout") |
| 82 |
self.assertEqual(x, "timeout") |
| 83 |
|
| 84 |
def test_raising_stopserve(self): |
| 85 |
def stopit(conn, addr): |
| 86 |
raise eventlet.StopServe() |
| 87 |
l = eventlet.listen(('localhost', 0)) |
| 88 |
# connect to trigger a call to stopit |
| 89 |
gt = eventlet.spawn(eventlet.connect, ('localhost', l.getsockname()[1])) |
| 90 |
eventlet.serve(l, stopit) |
| 91 |
gt.wait() |
| 92 |
|
| 93 |
def test_concurrency(self): |
| 94 |
evt = eventlet.Event() |
| 95 |
|
| 96 |
def waiter(sock, addr): |
| 97 |
sock.sendall(b'hi') |
| 98 |
evt.wait() |
| 99 |
l = eventlet.listen(('localhost', 0)) |
| 100 |
eventlet.spawn(eventlet.serve, l, waiter, 5) |
| 101 |
|
| 102 |
def test_client(): |
| 103 |
c = eventlet.connect(('localhost', l.getsockname()[1])) |
| 104 |
# verify the client is connected by getting data |
| 105 |
self.assertEqual(b'hi', c.recv(2)) |
| 106 |
return c |
| 107 |
[test_client() for i in range(5)] |
| 108 |
# very next client should not get anything |
| 109 |
x = eventlet.with_timeout( |
| 110 |
0.01, |
| 111 |
test_client, |
| 112 |
timeout_value="timed out") |
| 113 |
self.assertEqual(x, "timed out") |
| 114 |
|
| 115 |
@tests.skip_if_no_ssl |
| 116 |
def test_wrap_ssl(self): |
| 117 |
server = eventlet.wrap_ssl( |
| 118 |
eventlet.listen(('localhost', 0)), |
| 119 |
certfile=certificate_file, keyfile=private_key_file, |
| 120 |
server_side=True) |
| 121 |
port = server.getsockname()[1] |
| 122 |
|
| 123 |
def handle(sock, addr): |
| 124 |
sock.sendall(sock.recv(1024)) |
| 125 |
raise eventlet.StopServe() |
| 126 |
|
| 127 |
eventlet.spawn(eventlet.serve, server, handle) |
| 128 |
client = eventlet.wrap_ssl(eventlet.connect(('localhost', port))) |
| 129 |
client.sendall(b"echo") |
| 130 |
self.assertEqual(b"echo", client.recv(1024)) |
| 131 |
|
| 132 |
|
| 133 |
def test_socket_reuse(): |
| 134 |
# pick a free port with bind to 0 - without SO_REUSEPORT |
| 135 |
# then close it and try to bind to same port with SO_REUSEPORT |
| 136 |
# loop helps in case something else used the chosen port before second bind |
| 137 |
addr = None |
| 138 |
errors = [] |
| 139 |
for _ in range(5): |
| 140 |
lsock1 = eventlet.listen(('localhost', 0)) |
| 141 |
addr = lsock1.getsockname() |
| 142 |
lsock1.close() |
| 143 |
try: |
| 144 |
lsock1 = eventlet.listen(addr) |
| 145 |
except OSError as e: |
| 146 |
errors.append(e) |
| 147 |
continue |
| 148 |
break |
| 149 |
else: |
| 150 |
assert False, errors |
| 151 |
|
| 152 |
if hasattr(socket, 'SO_REUSEPORT'): |
| 153 |
lsock2 = eventlet.listen(addr) |
| 154 |
else: |
| 155 |
try: |
| 156 |
lsock2 = eventlet.listen(addr) |
| 157 |
assert lsock2 |
| 158 |
lsock2.close() |
| 159 |
except OSError: |
| 160 |
pass |
| 161 |
|
| 162 |
lsock1.close() |
| 163 |
|
| 164 |
|
| 165 |
def test_reuse_random_port_warning(): |
| 166 |
with warnings.catch_warnings(record=True) as w: |
| 167 |
eventlet.listen(('localhost', 0), reuse_port=True).close() |
| 168 |
assert len(w) == 1 |
| 169 |
assert issubclass(w[0].category, convenience.ReuseRandomPortWarning) |
| 170 |
|
| 171 |
|
| 172 |
@tests.skip_unless(hasattr(socket, 'SO_REUSEPORT')) |
| 173 |
def test_reuseport_oserror(): |
| 174 |
# https://github.com/eventlet/eventlet/issues/380 |
| 175 |
# https://github.com/eventlet/eventlet/issues/418 |
| 176 |
err22 = OSError(22, 'Invalid argument') |
| 177 |
|
| 178 |
sock1 = eventlet.listen(('localhost', 0)) |
| 179 |
addr = sock1.getsockname() |
| 180 |
sock1.close() |
| 181 |
|
| 182 |
original_socket_init = socket.socket.__init__ |
| 183 |
|
| 184 |
def patched(self, *a, **kw): |
| 185 |
original_socket_init(self, *a, **kw) |
| 186 |
self.setsockopt = tests.mock.Mock(side_effect=err22) |
| 187 |
|
| 188 |
with warnings.catch_warnings(record=True) as w: |
| 189 |
try: |
| 190 |
socket.socket.__init__ = patched |
| 191 |
eventlet.listen(addr, reuse_addr=False, reuse_port=True).close() |
| 192 |
finally: |
| 193 |
socket.socket.__init__ = original_socket_init |
| 194 |
assert len(w) == 1 |
| 195 |
assert issubclass(w[0].category, convenience.ReusePortUnavailableWarning) |