Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1 | import socket |
| 2 | import threading |
| 3 | import poplib |
| 4 | import time |
| 5 | |
| 6 | from unittest import TestCase |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 8 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | HOST = support.HOST |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 10 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 11 | def server(evt, serv): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 12 | serv.listen(5) |
| 13 | try: |
| 14 | conn, addr = serv.accept() |
| 15 | except socket.timeout: |
| 16 | pass |
| 17 | else: |
Guido van Rossum | 8a392d7 | 2007-11-21 22:09:45 +0000 | [diff] [blame] | 18 | conn.send(b"+ Hola mundo\n") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | conn.close() |
| 20 | finally: |
| 21 | serv.close() |
| 22 | evt.set() |
| 23 | |
| 24 | class GeneralTests(TestCase): |
| 25 | |
| 26 | def setUp(self): |
| 27 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 28 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 29 | self.sock.settimeout(3) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | self.port = support.bind_port(self.sock) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 31 | threading.Thread(target=server, args=(self.evt,self.sock)).start() |
| 32 | time.sleep(.1) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 33 | |
| 34 | def tearDown(self): |
| 35 | self.evt.wait() |
| 36 | |
| 37 | def testBasic(self): |
| 38 | # connects |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 39 | pop = poplib.POP3(HOST, self.port) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 40 | pop.sock.close() |
| 41 | |
| 42 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +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) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 49 | self.assertEqual(pop.sock.gettimeout(), 30) |
| 50 | pop.sock.close() |
| 51 | |
| 52 | def testTimeoutNone(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame^] | 53 | self.assertTrue(socket.getdefaulttimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 54 | socket.setdefaulttimeout(30) |
| 55 | try: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 56 | pop = poplib.POP3(HOST, self.port, timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 57 | finally: |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame^] | 58 | socket.setdefaulttimeout(None) |
| 59 | self.assertTrue(pop.sock.gettimeout() is None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 60 | pop.sock.close() |
| 61 | |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +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() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 66 | |
| 67 | |
| 68 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 69 | support.run_unittest(GeneralTests) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 70 | |
| 71 | if __name__ == '__main__': |
| 72 | test_main() |