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

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

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 6 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 324 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
(...skipping 26 matching lines...) Expand all
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 self.addCleanup(t.join) 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 OSError: 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)
(...skipping 14 matching lines...) Expand all
837 848
838 class TestAPI_UseUnixSocketsSelect(TestAPI_UseUnixSockets, unittest.TestCase): 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, unittest.TestCase): 853 class TestAPI_UseUnixSocketsPoll(TestAPI_UseUnixSockets, unittest.TestCase):
843 use_poll = True 854 use_poll = True
844 855
845 if __name__ == "__main__": 856 if __name__ == "__main__":
846 unittest.main() 857 unittest.main()
LEFTRIGHT

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