Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 1 | import socket |
| 2 | import threading |
| 3 | import poplib |
| 4 | import time |
| 5 | |
| 6 | from unittest import TestCase |
| 7 | from test import test_support |
| 8 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 9 | HOST = test_support.HOST |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 10 | |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 11 | def server(evt, serv): |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 12 | serv.listen(5) |
| 13 | try: |
| 14 | conn, addr = serv.accept() |
| 15 | except socket.timeout: |
| 16 | pass |
| 17 | else: |
| 18 | conn.send("+ Hola mundo\n") |
| 19 | conn.close() |
| 20 | finally: |
| 21 | serv.close() |
| 22 | evt.set() |
| 23 | |
| 24 | class GeneralTests(TestCase): |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 25 | |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 26 | def setUp(self): |
| 27 | self.evt = threading.Event() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 28 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 29 | self.sock.settimeout(3) |
| 30 | self.port = test_support.bind_port(self.sock) |
| 31 | threading.Thread(target=server, args=(self.evt,self.sock)).start() |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 32 | time.sleep(.1) |
| 33 | |
| 34 | def tearDown(self): |
| 35 | self.evt.wait() |
| 36 | |
| 37 | def testBasic(self): |
| 38 | # connects |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 39 | pop = poplib.POP3(HOST, self.port) |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 40 | pop.sock.close() |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 41 | |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 42 | def testTimeoutDefault(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 43 | self.assertTrue(socket.getdefaulttimeout() is None) |
| 44 | socket.setdefaulttimeout(30) |
| 45 | try: |
| 46 | pop = poplib.POP3("localhost", self.port) |
| 47 | finally: |
| 48 | socket.setdefaulttimeout(None) |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 49 | self.assertEqual(pop.sock.gettimeout(), 30) |
| 50 | pop.sock.close() |
| 51 | |
| 52 | def testTimeoutNone(self): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 53 | self.assertTrue(socket.getdefaulttimeout() is None) |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 54 | socket.setdefaulttimeout(30) |
| 55 | try: |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 56 | pop = poplib.POP3(HOST, self.port, timeout=None) |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 57 | finally: |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 58 | socket.setdefaulttimeout(None) |
| 59 | self.assertTrue(pop.sock.gettimeout() is None) |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 60 | pop.sock.close() |
| 61 | |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 62 | def testTimeoutValue(self): |
| 63 | pop = poplib.POP3("localhost", self.port, timeout=30) |
| 64 | self.assertEqual(pop.sock.gettimeout(), 30) |
| 65 | pop.sock.close() |
Facundo Batista | b20c500 | 2007-03-27 18:50:29 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def test_main(verbose=None): |
| 69 | test_support.run_unittest(GeneralTests) |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | test_main() |