2018-01-25 19:08:09 -04:00
|
|
|
import asyncio
|
2018-01-26 02:30:57 -04:00
|
|
|
import time
|
2018-01-25 19:08:09 -04:00
|
|
|
import threading
|
|
|
|
import unittest
|
|
|
|
|
2018-01-26 02:30:57 -04:00
|
|
|
from test import support
|
2018-01-25 19:08:09 -04:00
|
|
|
from test.test_asyncio import utils as test_utils
|
|
|
|
from test.test_asyncio import functional as func_tests
|
|
|
|
|
|
|
|
|
2018-06-02 00:34:09 -03:00
|
|
|
def tearDownModule():
|
|
|
|
asyncio.set_event_loop_policy(None)
|
|
|
|
|
|
|
|
|
2018-01-25 19:08:09 -04:00
|
|
|
class BaseStartServer(func_tests.FunctionalTestCaseMixin):
|
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def test_start_server_1(self):
|
|
|
|
HELLO_MSG = b'1' * 1024 * 5 + b'\n'
|
|
|
|
|
|
|
|
def client(sock, addr):
|
2018-01-26 02:30:57 -04:00
|
|
|
for i in range(10):
|
|
|
|
time.sleep(0.2)
|
|
|
|
if srv.is_serving():
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
sock.settimeout(2)
|
2018-01-25 19:08:09 -04:00
|
|
|
sock.connect(addr)
|
|
|
|
sock.send(HELLO_MSG)
|
|
|
|
sock.recv_all(1)
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
async def serve(reader, writer):
|
|
|
|
await reader.readline()
|
|
|
|
main_task.cancel()
|
|
|
|
writer.write(b'1')
|
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
|
|
|
|
async def main(srv):
|
|
|
|
async with srv:
|
|
|
|
await srv.serve_forever()
|
|
|
|
|
2019-05-27 16:56:22 -03:00
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
srv = self.loop.run_until_complete(asyncio.start_server(
|
|
|
|
serve, support.HOSTv4, 0, loop=self.loop, start_serving=False))
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
|
|
|
|
main_task = self.loop.create_task(main(srv))
|
|
|
|
|
|
|
|
addr = srv.sockets[0].getsockname()
|
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
|
|
with self.tcp_client(lambda sock: client(sock, addr)):
|
|
|
|
self.loop.run_until_complete(main_task)
|
|
|
|
|
2019-05-27 10:57:20 -03:00
|
|
|
self.assertEqual(srv.sockets, ())
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
self.assertIsNone(srv._sockets)
|
|
|
|
self.assertIsNone(srv._waiters)
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(RuntimeError, r'is closed'):
|
|
|
|
self.loop.run_until_complete(srv.serve_forever())
|
|
|
|
|
|
|
|
|
|
|
|
class SelectorStartServerTests(BaseStartServer, unittest.TestCase):
|
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
return asyncio.SelectorEventLoop()
|
|
|
|
|
2019-05-03 12:09:17 -03:00
|
|
|
@support.skip_unless_bind_unix_socket
|
2018-01-25 19:08:09 -04:00
|
|
|
def test_start_unix_server_1(self):
|
|
|
|
HELLO_MSG = b'1' * 1024 * 5 + b'\n'
|
|
|
|
started = threading.Event()
|
|
|
|
|
|
|
|
def client(sock, addr):
|
2018-01-26 02:30:57 -04:00
|
|
|
sock.settimeout(2)
|
2018-01-25 19:08:09 -04:00
|
|
|
started.wait(5)
|
|
|
|
sock.connect(addr)
|
|
|
|
sock.send(HELLO_MSG)
|
|
|
|
sock.recv_all(1)
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
async def serve(reader, writer):
|
|
|
|
await reader.readline()
|
|
|
|
main_task.cancel()
|
|
|
|
writer.write(b'1')
|
|
|
|
writer.close()
|
|
|
|
await writer.wait_closed()
|
|
|
|
|
|
|
|
async def main(srv):
|
|
|
|
async with srv:
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
await srv.start_serving()
|
|
|
|
self.assertTrue(srv.is_serving())
|
|
|
|
started.set()
|
|
|
|
await srv.serve_forever()
|
|
|
|
|
|
|
|
with test_utils.unix_socket_path() as addr:
|
2019-05-27 16:56:22 -03:00
|
|
|
with self.assertWarns(DeprecationWarning):
|
|
|
|
srv = self.loop.run_until_complete(asyncio.start_unix_server(
|
|
|
|
serve, addr, loop=self.loop, start_serving=False))
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
main_task = self.loop.create_task(main(srv))
|
|
|
|
|
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
|
|
|
with self.unix_client(lambda sock: client(sock, addr)):
|
|
|
|
self.loop.run_until_complete(main_task)
|
|
|
|
|
2019-05-27 10:57:20 -03:00
|
|
|
self.assertEqual(srv.sockets, ())
|
2018-01-25 19:08:09 -04:00
|
|
|
|
|
|
|
self.assertIsNone(srv._sockets)
|
|
|
|
self.assertIsNone(srv._waiters)
|
|
|
|
self.assertFalse(srv.is_serving())
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(RuntimeError, r'is closed'):
|
|
|
|
self.loop.run_until_complete(srv.serve_forever())
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(hasattr(asyncio, 'ProactorEventLoop'), 'Windows only')
|
|
|
|
class ProactorStartServerTests(BaseStartServer, unittest.TestCase):
|
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
return asyncio.ProactorEventLoop()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|