| 1 |
"""This test checks that socket instances (not GreenSockets but underlying sockets) |
| 2 |
are not leaked by the hub. |
| 3 |
""" |
| 4 |
import gc |
| 5 |
import pprint |
| 6 |
import sys |
| 7 |
import weakref |
| 8 |
|
| 9 |
import eventlet |
| 10 |
from eventlet.green import socket |
| 11 |
|
| 12 |
|
| 13 |
SOCKET_TIMEOUT = 0.1 |
| 14 |
|
| 15 |
|
| 16 |
def handle_request(s, raise_on_timeout): |
| 17 |
try: |
| 18 |
conn, address = s.accept() |
| 19 |
except socket.timeout: |
| 20 |
print('handle_request: server accept timeout') |
| 21 |
if raise_on_timeout: |
| 22 |
raise |
| 23 |
else: |
| 24 |
return |
| 25 |
print('handle_request: accepted') |
| 26 |
res = conn.recv(100) |
| 27 |
assert res == b'hello', repr(res) |
| 28 |
# print('handle_request: recvd %r' % res) |
| 29 |
res = conn.sendall(b'bye') |
| 30 |
# print('handle_request: sent %r' % res) |
| 31 |
# print('handle_request: conn refcount: %s' % sys.getrefcount(conn)) |
| 32 |
|
| 33 |
|
| 34 |
def make_request(addr): |
| 35 |
# print('make_request') |
| 36 |
s = eventlet.connect(addr) |
| 37 |
# print('make_request - connected') |
| 38 |
res = s.sendall(b'hello') |
| 39 |
# print('make_request - sent %s' % res) |
| 40 |
res = s.recv(100) |
| 41 |
assert res == b'bye', repr(res) |
| 42 |
# print('make_request - recvd %r' % res) |
| 43 |
|
| 44 |
|
| 45 |
def run_interaction(run_client): |
| 46 |
s = eventlet.listen(('127.0.0.1', 0)) |
| 47 |
s.settimeout(SOCKET_TIMEOUT) |
| 48 |
addr = s.getsockname() |
| 49 |
print('run_interaction: addr:', addr) |
| 50 |
eventlet.spawn(handle_request, s, run_client) |
| 51 |
if run_client: |
| 52 |
eventlet.spawn(make_request, addr) |
| 53 |
eventlet.sleep(0.1 + SOCKET_TIMEOUT) |
| 54 |
print('run_interaction: refcount(s.fd)', sys.getrefcount(s.fd)) |
| 55 |
return weakref.ref(s.fd) |
| 56 |
|
| 57 |
|
| 58 |
def run_and_check(run_client): |
| 59 |
w = run_interaction(run_client=run_client) |
| 60 |
gc.collect() |
| 61 |
fd = w() |
| 62 |
print('run_and_check: weakref fd:', fd) |
| 63 |
if fd: |
| 64 |
print(pprint.pformat(gc.get_referrers(fd))) |
| 65 |
for x in gc.get_referrers(fd): |
| 66 |
print(pprint.pformat(x)) |
| 67 |
for y in gc.get_referrers(x): |
| 68 |
print('- {}'.format(pprint.pformat(y))) |
| 69 |
raise AssertionError('server should be dead by now') |
| 70 |
|
| 71 |
|
| 72 |
def test_clean_exit(): |
| 73 |
run_and_check(True) |
| 74 |
run_and_check(True) |
| 75 |
|
| 76 |
|
| 77 |
def test_timeout_exit(): |
| 78 |
run_and_check(False) |
| 79 |
run_and_check(False) |