PORT = None
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:
- 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()
except socket.timeout:
pass
buf = buf[sent:]
n -= 1
- time.sleep(0.01)
conn.close()
finally:
self.evt = threading.Event()
servargs = (self.evt, "220 Hola mundo\n")
threading.Thread(target=server, args=servargs).start()
-
- # wait until server thread has assigned a port number
- 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)
+ self.evt.wait()
+ self.evt.clear()
def tearDown(self):
self.evt.wait()
smtp = smtplib.SMTP("%s:%s" % (HOST, PORT))
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):
# check that supplied local_hostname is used
smtp = smtplib.SMTP(HOST, PORT, local_hostname="testhost")
self.assertEqual(smtp.local_hostname, "testhost")
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):
# default
smtp = smtplib.SMTP(HOST, PORT)
serv = server_class(("", 0), ('nowhere', -1))
global PORT
PORT = serv.getsockname()[1]
+ serv_evt.set()
try:
if hasattr(select, 'poll'):
except socket.timeout:
pass
finally:
- # allow some time for the client to read the result
- time.sleep(0.5)
- serv.close()
+ if not client_evt.isSet():
+ # allow some time for the client to read the result
+ time.sleep(0.5)
+ serv.close()
asyncore.close_all()
PORT = None
- time.sleep(0.5)
serv_evt.set()
MSG_BEGIN = '---------- MESSAGE FOLLOWS ----------\n'
threading.Thread(target=debugging_server, args=serv_args).start()
# wait until server thread has assigned a port number
- 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)
+ self.serv_evt.wait()
+ self.serv_evt.clear()
def tearDown(self):
# indicate that the client is finished
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
class BadHELOServerTests(TestCase):
self.evt = threading.Event()
servargs = (self.evt, "199 no hello for you!\n")
threading.Thread(target=server, args=servargs).start()
-
- # wait until server thread has assigned a port number
- 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)
+ self.evt.wait()
+ self.evt.clear()
def tearDown(self):
self.evt.wait()
threading.Thread(target=debugging_server, args=serv_args).start()
# wait until server thread has assigned a port number
- 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)
+ self.serv_evt.wait()
+ self.serv_evt.clear()
def tearDown(self):
# indicate that the client is finished
def test_main(verbose=None):
test_support.run_unittest(GeneralTests, DebuggingServerTests,
+ NonConnectingTests,
BadHELOServerTests, SMTPSimTests)
if __name__ == '__main__':