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

Side by Side Diff: Lib/test/test_socket.py

Issue 17561: Add socket.create_server_sock() convenience function
Patch Set: Created 6 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:
View unified diff | Download patch
« Lib/socket.py ('K') | « Lib/socket.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 import unittest 3 import unittest
4 from test import support 4 from test import support
5 5
6 import errno 6 import errno
7 import io 7 import io
8 import socket 8 import socket
9 import select 9 import select
10 import tempfile 10 import tempfile
(...skipping 4230 matching lines...) Expand 10 before | Expand all | Expand 10 after
4241 expected_errnos.append(errno.ENETUNREACH) 4241 expected_errnos.append(errno.ENETUNREACH)
4242 4242
4243 self.assertIn(cm.exception.errno, expected_errnos) 4243 self.assertIn(cm.exception.errno, expected_errnos)
4244 4244
4245 def test_create_connection_timeout(self): 4245 def test_create_connection_timeout(self):
4246 # Issue #9792: create_connection() should not recast timeout errors 4246 # Issue #9792: create_connection() should not recast timeout errors
4247 # as generic socket errors. 4247 # as generic socket errors.
4248 with self.mocked_socket_module(): 4248 with self.mocked_socket_module():
4249 with self.assertRaises(socket.timeout): 4249 with self.assertRaises(socket.timeout):
4250 socket.create_connection((HOST, 1234)) 4250 socket.create_connection((HOST, 1234))
4251
4252
4253 class CreateServerSockTest(unittest.TestCase):
4254
4255 def echo_server(self, sock, port=None):
4256 def run():
4257 with sock:
4258 conn, _ = sock.accept()
4259 with conn:
4260 msg = conn.recv(1024)
4261 if not msg:
4262 return
4263 conn.sendall(msg)
4264
4265 sock.settimeout(2)
4266 t = threading.Thread(target=run)
4267 t.start()
4268 time.sleep(.1)
4269
4270 def test_base(self):
4271 port = support.find_unused_port()
4272 with socket.create_server_sock((None, port)) as sock:
4273 self.assertEqual(sock.getsockname()[1], port)
4274 self.assertEqual(sock.type, socket.SOCK_STREAM)
4275 if socket.has_dual_stack():
4276 self.assertEqual(sock.family, socket.AF_INET6)
4277 else:
4278 self.assertEqual(sock.family, socket.AF_INET)
4279 self.echo_server(sock)
4280 with socket.create_connection(('localhost', port), timeout=2) as cl:
4281 cl.sendall(b'foo')
4282 self.assertEqual(cl.recv(1024), b'foo')
4283
4284 def test_dual_stack(self):
4285 with socket.create_server_sock((None, 0)) as sock:
4286 self.echo_server(sock)
4287 port = sock.getsockname()[1]
4288 with socket.create_connection(("127.0.0.1", port), timeout=2) as cl:
4289 cl.sendall(b'foo')
4290 self.assertEqual(cl.recv(1024), b'foo')
4291
4292 with socket.create_server_sock((None, 0)) as sock:
4293 self.echo_server(sock)
4294 port = sock.getsockname()[1]
4295 if socket.has_dual_stack():
4296 with socket.create_connection(("::1", port), timeout=2) as cl:
4297 cl.sendall(b'foo')
4298 self.assertEqual(cl.recv(1024), b'foo')
4299 else:
4300 self.assertRaises(ConnectionRefusedError,
4301 socket.create_connection, ("::1", port))
4302 # just stop server
4303 with socket.create_connection(("127.0.0.1", port), timeout=2) \
4304 as cl:
4305 cl.sendall(b'foo')
4306 cl.recv(1024)
4307 unittest.skip('dual stack cannot be tested as not supported')
4251 4308
4252 4309
4253 @unittest.skipUnless(thread, 'Threading required for this test.') 4310 @unittest.skipUnless(thread, 'Threading required for this test.')
4254 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest): 4311 class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
4255 4312
4256 def __init__(self, methodName='runTest'): 4313 def __init__(self, methodName='runTest'):
4257 SocketTCPTest.__init__(self, methodName=methodName) 4314 SocketTCPTest.__init__(self, methodName=methodName)
4258 ThreadableTest.__init__(self) 4315 ThreadableTest.__init__(self)
4259 4316
4260 def clientSetUp(self): 4317 def clientSetUp(self):
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
4951 InterruptedSendTimeoutTest, 5008 InterruptedSendTimeoutTest,
4952 TestSocketSharing, 5009 TestSocketSharing,
4953 ]) 5010 ])
4954 5011
4955 thread_info = support.threading_setup() 5012 thread_info = support.threading_setup()
4956 support.run_unittest(*tests) 5013 support.run_unittest(*tests)
4957 support.threading_cleanup(*thread_info) 5014 support.threading_cleanup(*thread_info)
4958 5015
4959 if __name__ == "__main__": 5016 if __name__ == "__main__":
4960 test_main() 5017 test_main()
OLDNEW
« Lib/socket.py ('K') | « Lib/socket.py ('k') | no next file » | no next file with comments »

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