Kaydet (Commit) 75992edf authored tarafından Neal Norwitz's avatar Neal Norwitz

Speed up this test by about 99%. Remove sleeps and replace with events.

(This may fail on some slow platforms, but we can fix those cases which
should be relatively isolated and easier to find now.)
Move two test cases that didn't require a server to be started
to a separate TestCase.  These tests were taking 3 seconds which
is what the timeout was set to.
üst 1b3e41c6
...@@ -18,14 +18,15 @@ HOST = "localhost" ...@@ -18,14 +18,15 @@ HOST = "localhost"
PORT = None PORT = None
def server(evt, buf): def server(evt, buf):
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.settimeout(1)
serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serv.bind(("", 0))
global PORT
PORT = serv.getsockname()[1]
serv.listen(5)
evt.set()
try: try:
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv.settimeout(3)
serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serv.bind(("", 0))
global PORT
PORT = serv.getsockname()[1]
serv.listen(5)
conn, addr = serv.accept() conn, addr = serv.accept()
except socket.timeout: except socket.timeout:
pass pass
...@@ -38,7 +39,6 @@ def server(evt, buf): ...@@ -38,7 +39,6 @@ def server(evt, buf):
buf = buf[sent:] buf = buf[sent:]
n -= 1 n -= 1
time.sleep(0.01)
conn.close() conn.close()
finally: finally:
...@@ -52,16 +52,8 @@ class GeneralTests(TestCase): ...@@ -52,16 +52,8 @@ class GeneralTests(TestCase):
self.evt = threading.Event() self.evt = threading.Event()
servargs = (self.evt, "220 Hola mundo\n") servargs = (self.evt, "220 Hola mundo\n")
threading.Thread(target=server, args=servargs).start() threading.Thread(target=server, args=servargs).start()
self.evt.wait()
# wait until server thread has assigned a port number self.evt.clear()
n = 500
while PORT is None and n > 0:
time.sleep(0.01)
n -= 1
# wait a little longer (sometimes connections are refused
# on slow machines without this additional wait)
time.sleep(0.5)
def tearDown(self): def tearDown(self):
self.evt.wait() self.evt.wait()
...@@ -76,29 +68,12 @@ class GeneralTests(TestCase): ...@@ -76,29 +68,12 @@ class GeneralTests(TestCase):
smtp = smtplib.SMTP("%s:%s" % (HOST, PORT)) smtp = smtplib.SMTP("%s:%s" % (HOST, PORT))
smtp.sock.close() smtp.sock.close()
def testNotConnected(self):
# Test various operations on an unconnected SMTP object that
# should raise exceptions (at present the attempt in SMTP.send
# to reference the nonexistent 'sock' attribute of the SMTP object
# causes an AttributeError)
smtp = smtplib.SMTP()
self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
self.assertRaises(smtplib.SMTPServerDisconnected,
smtp.send, 'test msg')
def testLocalHostName(self): def testLocalHostName(self):
# check that supplied local_hostname is used # check that supplied local_hostname is used
smtp = smtplib.SMTP(HOST, PORT, local_hostname="testhost") smtp = smtplib.SMTP(HOST, PORT, local_hostname="testhost")
self.assertEqual(smtp.local_hostname, "testhost") self.assertEqual(smtp.local_hostname, "testhost")
smtp.sock.close() smtp.sock.close()
def testNonnumericPort(self):
# check that non-numeric port raises socket.error
self.assertRaises(socket.error, smtplib.SMTP,
"localhost", "bogus")
self.assertRaises(socket.error, smtplib.SMTP,
"localhost:bogus")
def testTimeoutDefault(self): def testTimeoutDefault(self):
# default # default
smtp = smtplib.SMTP(HOST, PORT) smtp = smtplib.SMTP(HOST, PORT)
...@@ -128,6 +103,7 @@ def debugging_server(server_class, serv_evt, client_evt): ...@@ -128,6 +103,7 @@ def debugging_server(server_class, serv_evt, client_evt):
serv = server_class(("", 0), ('nowhere', -1)) serv = server_class(("", 0), ('nowhere', -1))
global PORT global PORT
PORT = serv.getsockname()[1] PORT = serv.getsockname()[1]
serv_evt.set()
try: try:
if hasattr(select, 'poll'): if hasattr(select, 'poll'):
...@@ -150,12 +126,12 @@ def debugging_server(server_class, serv_evt, client_evt): ...@@ -150,12 +126,12 @@ def debugging_server(server_class, serv_evt, client_evt):
except socket.timeout: except socket.timeout:
pass pass
finally: finally:
# allow some time for the client to read the result if not client_evt.isSet():
time.sleep(0.5) # allow some time for the client to read the result
serv.close() time.sleep(0.5)
serv.close()
asyncore.close_all() asyncore.close_all()
PORT = None PORT = None
time.sleep(0.5)
serv_evt.set() serv_evt.set()
MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n' MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
...@@ -181,14 +157,8 @@ class DebuggingServerTests(TestCase): ...@@ -181,14 +157,8 @@ class DebuggingServerTests(TestCase):
threading.Thread(target=debugging_server, args=serv_args).start() threading.Thread(target=debugging_server, args=serv_args).start()
# wait until server thread has assigned a port number # wait until server thread has assigned a port number
n = 500 self.serv_evt.wait()
while PORT is None and n > 0: self.serv_evt.clear()
time.sleep(0.01)
n -= 1
# wait a little longer (sometimes connections are refused
# on slow machines without this additional wait)
time.sleep(0.5)
def tearDown(self): def tearDown(self):
# indicate that the client is finished # indicate that the client is finished
...@@ -258,6 +228,26 @@ class DebuggingServerTests(TestCase): ...@@ -258,6 +228,26 @@ class DebuggingServerTests(TestCase):
self.assertEqual(self.output.getvalue(), mexpect) self.assertEqual(self.output.getvalue(), mexpect)
class NonConnectingTests(TestCase):
def testNotConnected(self):
# Test various operations on an unconnected SMTP object that
# should raise exceptions (at present the attempt in SMTP.send
# to reference the nonexistent 'sock' attribute of the SMTP object
# causes an AttributeError)
smtp = smtplib.SMTP()
self.assertRaises(smtplib.SMTPServerDisconnected, smtp.ehlo)
self.assertRaises(smtplib.SMTPServerDisconnected,
smtp.send, 'test msg')
def testNonnumericPort(self):
# check that non-numeric port raises socket.error
self.assertRaises(socket.error, smtplib.SMTP,
"localhost", "bogus")
self.assertRaises(socket.error, smtplib.SMTP,
"localhost:bogus")
# test response of client to a non-successful HELO message # test response of client to a non-successful HELO message
class BadHELOServerTests(TestCase): class BadHELOServerTests(TestCase):
...@@ -269,16 +259,8 @@ class BadHELOServerTests(TestCase): ...@@ -269,16 +259,8 @@ class BadHELOServerTests(TestCase):
self.evt = threading.Event() self.evt = threading.Event()
servargs = (self.evt, "199 no hello for you!\n") servargs = (self.evt, "199 no hello for you!\n")
threading.Thread(target=server, args=servargs).start() threading.Thread(target=server, args=servargs).start()
self.evt.wait()
# wait until server thread has assigned a port number self.evt.clear()
n = 500
while PORT is None and n > 0:
time.sleep(0.01)
n -= 1
# wait a little longer (sometimes connections are refused
# on slow machines without this additional wait)
time.sleep(0.5)
def tearDown(self): def tearDown(self):
self.evt.wait() self.evt.wait()
...@@ -355,14 +337,8 @@ class SMTPSimTests(TestCase): ...@@ -355,14 +337,8 @@ class SMTPSimTests(TestCase):
threading.Thread(target=debugging_server, args=serv_args).start() threading.Thread(target=debugging_server, args=serv_args).start()
# wait until server thread has assigned a port number # wait until server thread has assigned a port number
n = 500 self.serv_evt.wait()
while PORT is None and n > 0: self.serv_evt.clear()
time.sleep(0.01)
n -= 1
# wait a little longer (sometimes connections are refused
# on slow machines without this additional wait)
time.sleep(0.5)
def tearDown(self): def tearDown(self):
# indicate that the client is finished # indicate that the client is finished
...@@ -427,6 +403,7 @@ class SMTPSimTests(TestCase): ...@@ -427,6 +403,7 @@ class SMTPSimTests(TestCase):
def test_main(verbose=None): def test_main(verbose=None):
test_support.run_unittest(GeneralTests, DebuggingServerTests, test_support.run_unittest(GeneralTests, DebuggingServerTests,
NonConnectingTests,
BadHELOServerTests, SMTPSimTests) BadHELOServerTests, SMTPSimTests)
if __name__ == '__main__': if __name__ == '__main__':
......
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