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,6 +55,7 @@ class TestTCP(greentest.TestCase): ...@@ -71,6 +55,7 @@ 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)
try:
client = self.create_connection() client = self.create_connection()
client.settimeout(0.1) client.settimeout(0.1)
start = time.time() start = time.time()
...@@ -80,22 +65,25 @@ class TestTCP(greentest.TestCase): ...@@ -80,22 +65,25 @@ class TestTCP(greentest.TestCase):
assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start) assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start)
else: else:
raise AssertionError('%s should have been raised, instead recv returned %r' % (self.TIMEOUT_ERROR, data, )) raise AssertionError('%s should have been raised, instead recv returned %r' % (self.TIMEOUT_ERROR, data, ))
finally:
acceptor.get() 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)
try:
client = self.create_connection() client = self.create_connection()
client.settimeout(0.1) client.settimeout(0.1)
start = time.time() start = time.time()
send_succeed = False send_succeed = False
data_sent = 'h' * 100000 data_sent = 'h' * 100000
try: try:
result = client.sendall(data_sent) client.sendall(data_sent)
except self.TIMEOUT_ERROR: except self.TIMEOUT_ERROR:
assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start) assert 0.1 - 0.01 <= time.time() - start <= 0.1 + 0.1, (time.time() - start)
else: else:
assert time.time() - start <= 0.1 + 0.01, (time.time() - start) assert time.time() - start <= 0.1 + 0.01, (time.time() - start)
send_succeed = True send_succeed = True
finally:
conn, addr = acceptor.get() conn, addr = acceptor.get()
if send_succeed: if send_succeed:
client.close() client.close()
...@@ -113,12 +101,14 @@ class TestTCP(greentest.TestCase): ...@@ -113,12 +101,14 @@ class TestTCP(greentest.TestCase):
fd.close() fd.close()
acceptor = gevent.spawn(accept_once) acceptor = gevent.spawn(accept_once)
try:
client = self.create_connection() client = self.create_connection()
fd = client.makefile() fd = client.makefile()
client.close() client.close()
assert fd.readline() == 'hello\n' assert fd.readline() == 'hello\n'
assert fd.read() == '' assert fd.read() == ''
fd.close() fd.close()
finally:
acceptor.get() acceptor.get()
......
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