Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(165452)

Delta Between Two Patch Sets: Lib/test/test_asyncore.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 7 years ago
Right Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_ast.py ('k') | Lib/test/test_augassign.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import asyncore 1 import asyncore
2 import unittest 2 import unittest
3 import select 3 import select
4 import os 4 import os
5 import socket 5 import socket
6 import sys 6 import sys
7 import time 7 import time
8 import warnings 8 import warnings
9 import errno 9 import errno
10 import struct 10 import struct
11 11
12 from test import support 12 from test import support
13 from test.support import TESTFN, run_unittest, unlink 13 from test.support import TESTFN, run_unittest, unlink, HOST, HOSTv6
14 from io import BytesIO 14 from io import BytesIO
15 from io import StringIO 15 from io import StringIO
16 16
17 try: 17 try:
18 import threading 18 import threading
19 except ImportError: 19 except ImportError:
20 threading = None 20 threading = None
21 21
22 HOST = support.HOST 22 TIMEOUT = 3
23
24 HAS_UNIX_SOCKETS = hasattr(socket, 'AF_UNIX') 23 HAS_UNIX_SOCKETS = hasattr(socket, 'AF_UNIX')
25 24
26 class dummysocket: 25 class dummysocket:
27 def __init__(self): 26 def __init__(self):
28 self.closed = False 27 self.closed = False
29 28
30 def close(self): 29 def close(self):
31 self.closed = True 30 self.closed = True
32 31
33 def fileno(self): 32 def fileno(self):
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 389
391 n = 1000 390 n = 1000
392 while d.out_buffer and n > 0: 391 while d.out_buffer and n > 0:
393 asyncore.poll() 392 asyncore.poll()
394 n -= 1 393 n -= 1
395 394
396 evt.wait() 395 evt.wait()
397 396
398 self.assertEqual(cap.getvalue(), data*2) 397 self.assertEqual(cap.getvalue(), data*2)
399 finally: 398 finally:
400 t.join() 399 t.join(timeout=TIMEOUT)
400 if t.is_alive():
401 self.fail("join() timed out")
402
401 403
402 404
403 class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests): 405 class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests):
404 usepoll = True 406 usepoll = True
405 407
406 @unittest.skipUnless(hasattr(asyncore, 'file_wrapper'), 408 @unittest.skipUnless(hasattr(asyncore, 'file_wrapper'),
407 'asyncore.file_wrapper required') 409 'asyncore.file_wrapper required')
408 class FileWrapperTest(unittest.TestCase): 410 class FileWrapperTest(unittest.TestCase):
409 def setUp(self): 411 def setUp(self):
410 self.d = b"It's not dead, it's sleeping!" 412 self.d = b"It's not dead, it's sleeping!"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 508
507 def __init__(self, family, address): 509 def __init__(self, family, address):
508 BaseTestHandler.__init__(self) 510 BaseTestHandler.__init__(self)
509 self.create_socket(family) 511 self.create_socket(family)
510 self.connect(address) 512 self.connect(address)
511 513
512 def handle_connect(self): 514 def handle_connect(self):
513 pass 515 pass
514 516
515 517
516 class BaseTestAPI(unittest.TestCase): 518 class BaseTestAPI:
517 519
518 def tearDown(self): 520 def tearDown(self):
519 asyncore.close_all() 521 asyncore.close_all()
520 522
521 def loop_waiting_for_flag(self, instance, timeout=5): 523 def loop_waiting_for_flag(self, instance, timeout=5):
522 timeout = float(timeout) / 100 524 timeout = float(timeout) / 100
523 count = 100 525 count = 100
524 while asyncore.socket_map and count > 0: 526 while asyncore.socket_map and count > 0:
525 asyncore.loop(timeout=0.01, count=1, use_poll=self.use_poll) 527 asyncore.loop(timeout=0.01, count=1, use_poll=self.use_poll)
526 if instance.flag: 528 if instance.flag:
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 # stop serving 737 # stop serving
736 server.close() 738 server.close()
737 self.assertFalse(server.connected) 739 self.assertFalse(server.connected)
738 self.assertFalse(server.accepting) 740 self.assertFalse(server.accepting)
739 741
740 def test_create_socket(self): 742 def test_create_socket(self):
741 s = asyncore.dispatcher() 743 s = asyncore.dispatcher()
742 s.create_socket(self.family) 744 s.create_socket(self.family)
743 self.assertEqual(s.socket.family, self.family) 745 self.assertEqual(s.socket.family, self.family)
744 SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0) 746 SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
745 self.assertEqual(s.socket.type, socket.SOCK_STREAM | SOCK_NONBLOCK) 747 sock_type = socket.SOCK_STREAM | SOCK_NONBLOCK
748 if hasattr(socket, 'SOCK_CLOEXEC'):
749 self.assertIn(s.socket.type,
750 (sock_type | socket.SOCK_CLOEXEC, sock_type))
751 else:
752 self.assertEqual(s.socket.type, sock_type)
746 753
747 def test_bind(self): 754 def test_bind(self):
748 if HAS_UNIX_SOCKETS and self.family == socket.AF_UNIX: 755 if HAS_UNIX_SOCKETS and self.family == socket.AF_UNIX:
749 self.skipTest("Not applicable to AF_UNIX sockets.") 756 self.skipTest("Not applicable to AF_UNIX sockets.")
750 s1 = asyncore.dispatcher() 757 s1 = asyncore.dispatcher()
751 s1.create_socket(self.family) 758 s1.create_socket(self.family)
752 s1.bind(self.addr) 759 s1.bind(self.addr)
753 s1.listen(5) 760 s1.listen(5)
754 port = s1.socket.getsockname()[1] 761 port = s1.socket.getsockname()[1]
755 762
756 s2 = asyncore.dispatcher() 763 s2 = asyncore.dispatcher()
757 s2.create_socket(self.family) 764 s2.create_socket(self.family)
758 # EADDRINUSE indicates the socket was correctly bound 765 # EADDRINUSE indicates the socket was correctly bound
759 self.assertRaises(socket.error, s2.bind, (self.addr[0], port)) 766 self.assertRaises(OSError, s2.bind, (self.addr[0], port))
760 767
761 def test_set_reuse_addr(self): 768 def test_set_reuse_addr(self):
762 if HAS_UNIX_SOCKETS and self.family == socket.AF_UNIX: 769 if HAS_UNIX_SOCKETS and self.family == socket.AF_UNIX:
763 self.skipTest("Not applicable to AF_UNIX sockets.") 770 self.skipTest("Not applicable to AF_UNIX sockets.")
764 sock = socket.socket(self.family) 771 sock = socket.socket(self.family)
765 try: 772 try:
766 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 773 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
767 except socket.error: 774 except OSError:
768 unittest.skip("SO_REUSEADDR not supported on this platform") 775 unittest.skip("SO_REUSEADDR not supported on this platform")
769 else: 776 else:
770 # if SO_REUSEADDR succeeded for sock we expect asyncore 777 # if SO_REUSEADDR succeeded for sock we expect asyncore
771 # to do the same 778 # to do the same
772 s = asyncore.dispatcher(socket.socket(self.family)) 779 s = asyncore.dispatcher(socket.socket(self.family))
773 self.assertFalse(s.socket.getsockopt(socket.SOL_SOCKET, 780 self.assertFalse(s.socket.getsockopt(socket.SOL_SOCKET,
774 socket.SO_REUSEADDR)) 781 socket.SO_REUSEADDR))
775 s.socket.close() 782 s.socket.close()
776 s.create_socket(self.family) 783 s.create_socket(self.family)
777 s.set_reuse_addr() 784 s.set_reuse_addr()
778 self.assertTrue(s.socket.getsockopt(socket.SOL_SOCKET, 785 self.assertTrue(s.socket.getsockopt(socket.SOL_SOCKET,
779 socket.SO_REUSEADDR)) 786 socket.SO_REUSEADDR))
780 finally: 787 finally:
781 sock.close() 788 sock.close()
782 789
783 @unittest.skipUnless(threading, 'Threading required for this test.') 790 @unittest.skipUnless(threading, 'Threading required for this test.')
784 @support.reap_threads 791 @support.reap_threads
785 def test_quick_connect(self): 792 def test_quick_connect(self):
786 # see: http://bugs.python.org/issue10340 793 # see: http://bugs.python.org/issue10340
787 if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object()) ): 794 if self.family in (socket.AF_INET, getattr(socket, "AF_INET6", object()) ):
788 server = BaseServer(self.family, self.addr) 795 server = BaseServer(self.family, self.addr)
789 t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1, 796 t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1,
790 count=500)) 797 count=500))
791 t.start() 798 t.start()
792 799 def cleanup():
800 t.join(timeout=TIMEOUT)
801 if t.is_alive():
802 self.fail("join() timed out")
803 self.addCleanup(cleanup)
793 804
794 s = socket.socket(self.family, socket.SOCK_STREAM) 805 s = socket.socket(self.family, socket.SOCK_STREAM)
795 s.settimeout(.2) 806 s.settimeout(.2)
796 s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, 807 s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
797 struct.pack('ii', 1, 0)) 808 struct.pack('ii', 1, 0))
798 try: 809 try:
799 s.connect(server.address) 810 s.connect(server.address)
800 except socket.error: 811 except OSError:
801 pass 812 pass
802 finally: 813 finally:
803 s.close() 814 s.close()
804 815
805 class TestAPI_UseIPv4Sockets(BaseTestAPI): 816 class TestAPI_UseIPv4Sockets(BaseTestAPI):
806 family = socket.AF_INET 817 family = socket.AF_INET
807 addr = (HOST, 0) 818 addr = (HOST, 0)
808 819
809 @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 support required') 820 @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 support required')
810 class TestAPI_UseIPv6Sockets(BaseTestAPI): 821 class TestAPI_UseIPv6Sockets(BaseTestAPI):
811 family = socket.AF_INET6 822 family = socket.AF_INET6
812 addr = ('::1', 0) 823 addr = (HOSTv6, 0)
813 824
814 @unittest.skipUnless(HAS_UNIX_SOCKETS, 'Unix sockets required') 825 @unittest.skipUnless(HAS_UNIX_SOCKETS, 'Unix sockets required')
815 class TestAPI_UseUnixSockets(BaseTestAPI): 826 class TestAPI_UseUnixSockets(BaseTestAPI):
816 if HAS_UNIX_SOCKETS: 827 if HAS_UNIX_SOCKETS:
817 family = socket.AF_UNIX 828 family = socket.AF_UNIX
818 addr = support.TESTFN 829 addr = support.TESTFN
819 830
820 def tearDown(self): 831 def tearDown(self):
821 unlink(self.addr) 832 unlink(self.addr)
822 BaseTestAPI.tearDown(self) 833 BaseTestAPI.tearDown(self)
823 834
824 class TestAPI_UseIPv4Select(TestAPI_UseIPv4Sockets): 835 class TestAPI_UseIPv4Select(TestAPI_UseIPv4Sockets, unittest.TestCase):
825 use_poll = False 836 use_poll = False
826 837
827 @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required') 838 @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required')
828 class TestAPI_UseIPv4Poll(TestAPI_UseIPv4Sockets): 839 class TestAPI_UseIPv4Poll(TestAPI_UseIPv4Sockets, unittest.TestCase):
829 use_poll = True 840 use_poll = True
830 841
831 class TestAPI_UseIPv6Select(TestAPI_UseIPv6Sockets): 842 class TestAPI_UseIPv6Select(TestAPI_UseIPv6Sockets, unittest.TestCase):
832 use_poll = False 843 use_poll = False
833 844
834 @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required') 845 @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required')
835 class TestAPI_UseIPv6Poll(TestAPI_UseIPv6Sockets): 846 class TestAPI_UseIPv6Poll(TestAPI_UseIPv6Sockets, unittest.TestCase):
836 use_poll = True 847 use_poll = True
837 848
838 class TestAPI_UseUnixSocketsSelect(TestAPI_UseUnixSockets): 849 class TestAPI_UseUnixSocketsSelect(TestAPI_UseUnixSockets, unittest.TestCase):
839 use_poll = False 850 use_poll = False
840 851
841 @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required') 852 @unittest.skipUnless(hasattr(select, 'poll'), 'select.poll required')
842 class TestAPI_UseUnixSocketsPoll(TestAPI_UseUnixSockets): 853 class TestAPI_UseUnixSocketsPoll(TestAPI_UseUnixSockets, unittest.TestCase):
843 use_poll = True 854 use_poll = True
844 855
845 def test_main():
846 tests = [HelperFunctionTests, DispatcherTests, DispatcherWithSendTests,
847 DispatcherWithSendTests_UsePoll, FileWrapperTest,
848 TestAPI_UseIPv4Select, TestAPI_UseIPv4Poll, TestAPI_UseIPv6Select,
849 TestAPI_UseIPv6Poll, TestAPI_UseUnixSocketsSelect,
850 TestAPI_UseUnixSocketsPoll]
851 run_unittest(*tests)
852
853 if __name__ == "__main__": 856 if __name__ == "__main__":
854 test_main() 857 unittest.main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+