Commit 78e19616 authored by Facundo Batista's avatar Facundo Batista

Prevent asyncore.dispatcher tests from hanging by adding loop counters

to server & client, and by adding asyncore.close_all calls in
tearDown. Also choose correct expected logging results based on the
value of __debug__  [Alan McIntyre - GSoC]
parent 66263cf1
...@@ -52,23 +52,31 @@ class crashingdummy: ...@@ -52,23 +52,31 @@ class crashingdummy:
self.error_handled = True self.error_handled = True
# used when testing senders; just collects what it gets until newline is sent # used when testing senders; just collects what it gets until newline is sent
class capture_server(threading.Thread): def capture_server(evt, buf):
def run(self): serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serv.settimeout(3)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
global PORT serv.bind(("", PORT))
PORT = test_support.bind_port(sock, HOST, PORT) serv.listen(5)
sock.listen(1) try:
conn, client = sock.accept() conn, addr = serv.accept()
self.captured = "" except socket.timeout:
while 1: pass
else:
n = 200
while n > 0:
data = conn.recv(10) data = conn.recv(10)
# keep everything except for the newline terminator
buf.write(data.replace('\n', ''))
if '\n' in data: if '\n' in data:
break break
self.captured = self.captured + data n -= 1
time.sleep(0.01)
conn.close() conn.close()
sock.close() finally:
serv.close()
evt.set()
class HelperFunctionTests(unittest.TestCase): class HelperFunctionTests(unittest.TestCase):
...@@ -228,6 +236,12 @@ class HelperFunctionTests(unittest.TestCase): ...@@ -228,6 +236,12 @@ class HelperFunctionTests(unittest.TestCase):
class DispatcherTests(unittest.TestCase): class DispatcherTests(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
asyncore.close_all()
def test_basic(self): def test_basic(self):
d = asyncore.dispatcher() d = asyncore.dispatcher()
self.assertEqual(d.readable(), True) self.assertEqual(d.readable(), True)
...@@ -273,7 +287,11 @@ class DispatcherTests(unittest.TestCase): ...@@ -273,7 +287,11 @@ class DispatcherTests(unittest.TestCase):
sys.stdout = stdout sys.stdout = stdout
lines = fp.getvalue().splitlines() lines = fp.getvalue().splitlines()
if __debug__:
expected = ['EGGS: %s' % l1, 'info: %s' % l2, 'SPAM: %s' % l3] expected = ['EGGS: %s' % l1, 'info: %s' % l2, 'SPAM: %s' % l3]
else:
expected = ['EGGS: %s' % l1, 'SPAM: %s' % l3]
self.assertEquals(lines, expected) self.assertEquals(lines, expected)
def test_unhandled(self): def test_unhandled(self):
...@@ -312,25 +330,33 @@ class dispatcherwithsend_noread(asyncore.dispatcher_with_send): ...@@ -312,25 +330,33 @@ class dispatcherwithsend_noread(asyncore.dispatcher_with_send):
class DispatcherWithSendTests(unittest.TestCase): class DispatcherWithSendTests(unittest.TestCase):
usepoll = False usepoll = False
def setUp(self):
pass
def tearDown(self):
asyncore.close_all()
def test_send(self): def test_send(self):
s = capture_server() self.evt = threading.Event()
s.start() cap = StringIO()
threading.Thread(target=capture_server, args=(self.evt,cap)).start()
time.sleep(1) # Give server time to initialize time.sleep(1) # Give server time to initialize
data = "Suppose there isn't a 16-ton weight?"*100 data = "Suppose there isn't a 16-ton weight?"*5
d = dispatcherwithsend_noread() d = dispatcherwithsend_noread()
d.create_socket(socket.AF_INET, socket.SOCK_STREAM) d.create_socket(socket.AF_INET, socket.SOCK_STREAM)
d.connect((HOST, PORT)) d.connect((HOST, PORT))
d.send(data) d.send(data)
d.send('\n') d.send('\n')
while d.out_buffer: n = 1000
while d.out_buffer and n > 0:
asyncore.poll() asyncore.poll()
n -= 1
s.stopit = True self.evt.wait()
s.join()
self.assertEqual(s.captured, data) self.assertEqual(cap.getvalue(), data)
class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests): class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests):
......
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