Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 1 | import socket |
| 2 | import threading |
| 3 | import ftplib |
| 4 | import time |
| 5 | |
| 6 | from unittest import TestCase |
| 7 | from test import test_support |
| 8 | |
| 9 | def server(evt): |
| 10 | serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame^] | 11 | serv.settimeout(3) |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 12 | serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 13 | serv.bind(("", 9091)) |
| 14 | serv.listen(5) |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame^] | 15 | try: |
| 16 | conn, addr = serv.accept() |
| 17 | except socket.timeout: |
| 18 | pass |
| 19 | else: |
| 20 | conn.send("1 Hola mundo\n") |
| 21 | conn.close() |
| 22 | finally: |
| 23 | serv.close() |
| 24 | evt.set() |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 25 | |
| 26 | class GeneralTests(TestCase): |
| 27 | |
| 28 | def setUp(self): |
| 29 | ftplib.FTP.port = 9091 |
| 30 | self.evt = threading.Event() |
| 31 | threading.Thread(target=server, args=(self.evt,)).start() |
| 32 | time.sleep(.1) |
| 33 | |
| 34 | def tearDown(self): |
| 35 | self.evt.wait() |
| 36 | |
| 37 | def testBasic(self): |
| 38 | # do nothing |
| 39 | ftplib.FTP() |
| 40 | |
| 41 | # connects |
| 42 | ftp = ftplib.FTP("localhost") |
| 43 | ftp.sock.close() |
| 44 | |
| 45 | def testTimeoutDefault(self): |
| 46 | # default |
| 47 | ftp = ftplib.FTP("localhost") |
| 48 | self.assertTrue(ftp.sock.gettimeout() is None) |
| 49 | ftp.sock.close() |
| 50 | |
| 51 | def testTimeoutValue(self): |
| 52 | # a value |
| 53 | ftp = ftplib.FTP("localhost", timeout=30) |
| 54 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 55 | ftp.sock.close() |
| 56 | |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame^] | 57 | def testTimeoutConnect(self): |
| 58 | ftp = ftplib.FTP() |
| 59 | ftp.connect("localhost", timeout=30) |
| 60 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 61 | ftp.sock.close() |
| 62 | |
| 63 | def testTimeoutDifferentOrder(self): |
| 64 | ftp = ftplib.FTP(timeout=30) |
| 65 | ftp.connect("localhost") |
| 66 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 67 | ftp.sock.close() |
| 68 | |
| 69 | def testTimeoutDirectAccess(self): |
| 70 | ftp = ftplib.FTP() |
| 71 | ftp.timeout = 30 |
| 72 | ftp.connect("localhost") |
| 73 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 74 | ftp.sock.close() |
| 75 | |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 76 | def testTimeoutNone(self): |
| 77 | # None, having other default |
| 78 | previous = socket.getdefaulttimeout() |
| 79 | socket.setdefaulttimeout(30) |
| 80 | try: |
| 81 | ftp = ftplib.FTP("localhost", timeout=None) |
| 82 | finally: |
| 83 | socket.setdefaulttimeout(previous) |
| 84 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 85 | ftp.close() |
| 86 | |
| 87 | |
| 88 | |
| 89 | def test_main(verbose=None): |
| 90 | test_support.run_unittest(GeneralTests) |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | test_main() |