Commit 2b369016 authored by Denis Bilenko's avatar Denis Bilenko

test__socket.py: simplify cleanup, make sure totalrefcount check passes

--HG--
extra : transplant_source : .%EE%D7y%C3%F6%23%9D%A9/%D3%16%83%97u%E1%D7%00%92Y
parent d25f7811
import os import os
import traceback
import gevent import gevent
from gevent import socket from gevent import socket
import greentest import greentest
...@@ -13,22 +12,9 @@ class TestTCP(greentest.TestCase): ...@@ -13,22 +12,9 @@ class TestTCP(greentest.TestCase):
def setUp(self): def setUp(self):
greentest.TestCase.setUp(self) greentest.TestCase.setUp(self)
self.listener = socket.tcp_listener(('127.0.0.1', 0)) self.listener = socket.tcp_listener(('127.0.0.1', 0))
self.tokill = []
def tearDown(self): def tearDown(self):
del self.listener del self.listener
try:
gevent.killall(self.tokill, block=True)
except:
traceback.print_exc()
# eat LinkedExited notifications
while True:
try:
gevent.sleep(0.001)
break
except:
traceback.print_exc()
del self.tokill
greentest.TestCase.tearDown(self) greentest.TestCase.tearDown(self)
def create_connection(self): def create_connection(self):
...@@ -54,10 +40,8 @@ class TestTCP(greentest.TestCase): ...@@ -54,10 +40,8 @@ class TestTCP(greentest.TestCase):
#print '%s: client' % getcurrent() #print '%s: client' % getcurrent()
server_proc = gevent.spawn_link_exception(server) server_proc = gevent.spawn_link_exception(server)
self.tokill.append(server_proc)
client = self.create_connection() client = self.create_connection()
client_reader = gevent.spawn_link_exception(client.makefile().read) client_reader = gevent.spawn_link_exception(client.makefile().read)
self.tokill.append(client_reader)
gevent.sleep(0.001) gevent.sleep(0.001)
client.send('hello world') client.send('hello world')
...@@ -71,32 +55,36 @@ class TestTCP(greentest.TestCase): ...@@ -71,32 +55,36 @@ class TestTCP(greentest.TestCase):
def test_recv_timeout(self): def test_recv_timeout(self):
acceptor = gevent.spawn_link_exception(self.listener.accept) acceptor = gevent.spawn_link_exception(self.listener.accept)
client = self.create_connection()
client.settimeout(0.1)
start = time.time()
try: try:
data = client.recv(1024) client = self.create_connection()
except self.TIMEOUT_ERROR: client.settimeout(0.1)
assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start) start = time.time()
else: try:
raise AssertionError('%s should have been raised, instead recv returned %r' % (self.TIMEOUT_ERROR, data, )) data = client.recv(1024)
acceptor.get() except self.TIMEOUT_ERROR:
assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start)
else:
raise AssertionError('%s should have been raised, instead recv returned %r' % (self.TIMEOUT_ERROR, data, ))
finally:
acceptor.get()
def test_sendall_timeout(self): def test_sendall_timeout(self):
acceptor = gevent.spawn_link_exception(self.listener.accept) acceptor = gevent.spawn_link_exception(self.listener.accept)
client = self.create_connection()
client.settimeout(0.1)
start = time.time()
send_succeed = False
data_sent = 'h' * 100000
try: try:
result = client.sendall(data_sent) client = self.create_connection()
except self.TIMEOUT_ERROR: client.settimeout(0.1)
assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start) start = time.time()
else: send_succeed = False
assert time.time() - start <= 0.1 + 0.01, (time.time() - start) data_sent = 'h' * 100000
send_succeed = True try:
conn, addr = acceptor.get() client.sendall(data_sent)
except self.TIMEOUT_ERROR:
assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start)
else:
assert time.time() - start <= 0.1 + 0.01, (time.time() - start)
send_succeed = True
finally:
conn, addr = acceptor.get()
if send_succeed: if send_succeed:
client.close() client.close()
data_read = conn.makefile().read() data_read = conn.makefile().read()
...@@ -113,13 +101,15 @@ class TestTCP(greentest.TestCase): ...@@ -113,13 +101,15 @@ class TestTCP(greentest.TestCase):
fd.close() fd.close()
acceptor = gevent.spawn(accept_once) acceptor = gevent.spawn(accept_once)
client = self.create_connection() try:
fd = client.makefile() client = self.create_connection()
client.close() fd = client.makefile()
assert fd.readline() == 'hello\n' client.close()
assert fd.read() == '' assert fd.readline() == 'hello\n'
fd.close() assert fd.read() == ''
acceptor.get() fd.close()
finally:
acceptor.get()
if hasattr(socket, 'ssl'): if hasattr(socket, 'ssl'):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment