From: R. David Murray Date: Sat, 11 Apr 2009 17:52:56 +0000 (+0000) Subject: Make test_asyncore tests match code changes introduced by the X-Git-Tag: v2.7a1~1501 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5331d4b4de59cca76a9d61f2e66851cfa70bba00;p=python Make test_asyncore tests match code changes introduced by the fix to Issue1161031, refactoring the test to simplify it in the process. --- diff --git a/Lib/test/test_asyncore.py b/Lib/test/test_asyncore.py index 13b39e1219..e1ccb5a852 100644 --- a/Lib/test/test_asyncore.py +++ b/Lib/test/test_asyncore.py @@ -115,12 +115,24 @@ class HelperFunctionTests(unittest.TestCase): def test_readwrite(self): # Check that correct methods are called by readwrite() + attributes = ('read', 'expt', 'write', 'closed', 'error_handled') + + expected = ( + (select.POLLIN, 'read'), + (select.POLLPRI, 'expt'), + (select.POLLOUT, 'write'), + (select.POLLERR, 'closed'), + (select.POLLHUP, 'closed'), + (select.POLLNVAL, 'closed'), + ) + class testobj: def __init__(self): self.read = False self.write = False self.closed = False self.expt = False + self.error_handled = False def handle_read_event(self): self.read = True @@ -137,54 +149,25 @@ class HelperFunctionTests(unittest.TestCase): def handle_error(self): self.error_handled = True - for flag in (select.POLLIN, select.POLLPRI): + for flag, expectedattr in expected: tobj = testobj() - self.assertEqual(tobj.read, False) + self.assertEqual(getattr(tobj, expectedattr), False) asyncore.readwrite(tobj, flag) - self.assertEqual(tobj.read, True) - # check that ExitNow exceptions in the object handler method - # bubbles all the way up through asyncore readwrite call - tr1 = exitingdummy() - self.assertRaises(asyncore.ExitNow, asyncore.readwrite, tr1, flag) - - # check that an exception other than ExitNow in the object handler - # method causes the handle_error method to get called - tr2 = crashingdummy() - asyncore.readwrite(tr2, flag) - self.assertEqual(tr2.error_handled, True) - - tobj = testobj() - self.assertEqual(tobj.write, False) - asyncore.readwrite(tobj, select.POLLOUT) - self.assertEqual(tobj.write, True) - - # check that ExitNow exceptions in the object handler method - # bubbles all the way up through asyncore readwrite call - tr1 = exitingdummy() - self.assertRaises(asyncore.ExitNow, asyncore.readwrite, tr1, - select.POLLOUT) - - # check that an exception other than ExitNow in the object handler - # method causes the handle_error method to get called - tr2 = crashingdummy() - asyncore.readwrite(tr2, select.POLLOUT) - self.assertEqual(tr2.error_handled, True) - - for flag in (select.POLLERR, select.POLLHUP, select.POLLNVAL): - tobj = testobj() - self.assertEqual((tobj.expt, tobj.closed)[flag == select.POLLHUP], False) - asyncore.readwrite(tobj, flag) - self.assertEqual((tobj.expt, tobj.closed)[flag == select.POLLHUP], True) + # Only the attribute modified by the routine we expect to be + # called should be True. + for attr in attributes: + self.assertEqual(getattr(tobj, attr), attr==expectedattr) # check that ExitNow exceptions in the object handler method - # bubbles all the way up through asyncore readwrite calls + # bubbles all the way up through asyncore readwrite call tr1 = exitingdummy() self.assertRaises(asyncore.ExitNow, asyncore.readwrite, tr1, flag) # check that an exception other than ExitNow in the object handler # method causes the handle_error method to get called tr2 = crashingdummy() + self.assertEqual(tr2.error_handled, False) asyncore.readwrite(tr2, flag) self.assertEqual(tr2.error_handled, True)