2013-10-17 17:40:50 -03:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
if sys.platform != 'win32':
|
|
|
|
raise unittest.SkipTest('Windows only')
|
|
|
|
|
2013-10-30 18:44:05 -03:00
|
|
|
import _winapi
|
|
|
|
|
2013-10-17 17:40:50 -03:00
|
|
|
import asyncio
|
|
|
|
from asyncio import test_utils
|
2013-10-30 18:44:05 -03:00
|
|
|
from asyncio import _overlapped
|
2014-01-25 10:32:06 -04:00
|
|
|
from asyncio import windows_events
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
class UpperProto(asyncio.Protocol):
|
2013-10-17 17:40:50 -03:00
|
|
|
def __init__(self):
|
|
|
|
self.buf = []
|
|
|
|
|
|
|
|
def connection_made(self, trans):
|
|
|
|
self.trans = trans
|
|
|
|
|
|
|
|
def data_received(self, data):
|
|
|
|
self.buf.append(data)
|
|
|
|
if b'\n' in data:
|
|
|
|
self.trans.write(b''.join(self.buf).upper())
|
|
|
|
self.trans.close()
|
|
|
|
|
|
|
|
|
|
|
|
class ProactorTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
self.loop = asyncio.ProactorEventLoop()
|
2013-10-17 17:40:50 -03:00
|
|
|
asyncio.set_event_loop(None)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.loop.close()
|
|
|
|
self.loop = None
|
|
|
|
|
|
|
|
def test_close(self):
|
|
|
|
a, b = self.loop._socketpair()
|
2014-01-25 10:32:06 -04:00
|
|
|
trans = self.loop._make_socket_transport(a, asyncio.Protocol())
|
2013-10-17 17:40:50 -03:00
|
|
|
f = asyncio.async(self.loop.sock_recv(b, 100))
|
|
|
|
trans.close()
|
|
|
|
self.loop.run_until_complete(f)
|
|
|
|
self.assertEqual(f.result(), b'')
|
2014-01-10 19:16:50 -04:00
|
|
|
b.close()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_double_bind(self):
|
|
|
|
ADDRESS = r'\\.\pipe\test_double_bind-%s' % os.getpid()
|
|
|
|
server1 = windows_events.PipeServer(ADDRESS)
|
|
|
|
with self.assertRaises(PermissionError):
|
|
|
|
server2 = windows_events.PipeServer(ADDRESS)
|
|
|
|
server1.close()
|
|
|
|
|
|
|
|
def test_pipe(self):
|
|
|
|
res = self.loop.run_until_complete(self._test_pipe())
|
|
|
|
self.assertEqual(res, 'done')
|
|
|
|
|
|
|
|
def _test_pipe(self):
|
|
|
|
ADDRESS = r'\\.\pipe\_test_pipe-%s' % os.getpid()
|
|
|
|
|
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
yield from self.loop.create_pipe_connection(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.Protocol, ADDRESS)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
[server] = yield from self.loop.start_serving_pipe(
|
|
|
|
UpperProto, ADDRESS)
|
|
|
|
self.assertIsInstance(server, windows_events.PipeServer)
|
|
|
|
|
|
|
|
clients = []
|
|
|
|
for i in range(5):
|
2014-01-25 10:32:06 -04:00
|
|
|
stream_reader = asyncio.StreamReader(loop=self.loop)
|
|
|
|
protocol = asyncio.StreamReaderProtocol(stream_reader)
|
2013-10-17 17:40:50 -03:00
|
|
|
trans, proto = yield from self.loop.create_pipe_connection(
|
2013-11-01 18:20:55 -03:00
|
|
|
lambda: protocol, ADDRESS)
|
2014-01-25 10:32:06 -04:00
|
|
|
self.assertIsInstance(trans, asyncio.Transport)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(protocol, proto)
|
|
|
|
clients.append((stream_reader, trans))
|
|
|
|
|
|
|
|
for i, (r, w) in enumerate(clients):
|
|
|
|
w.write('lower-{}\n'.format(i).encode())
|
|
|
|
|
|
|
|
for i, (r, w) in enumerate(clients):
|
|
|
|
response = yield from r.readline()
|
|
|
|
self.assertEqual(response, 'LOWER-{}\n'.format(i).encode())
|
|
|
|
w.close()
|
|
|
|
|
|
|
|
server.close()
|
|
|
|
|
|
|
|
with self.assertRaises(FileNotFoundError):
|
|
|
|
yield from self.loop.create_pipe_connection(
|
2014-01-25 10:32:06 -04:00
|
|
|
asyncio.Protocol, ADDRESS)
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
return 'done'
|
2013-10-20 16:02:53 -03:00
|
|
|
|
2013-10-30 18:44:05 -03:00
|
|
|
def test_wait_for_handle(self):
|
|
|
|
event = _overlapped.CreateEvent(None, True, False, None)
|
|
|
|
self.addCleanup(_winapi.CloseHandle, event)
|
|
|
|
|
|
|
|
# Wait for unset event with 0.2s timeout;
|
|
|
|
# result should be False at timeout
|
|
|
|
f = self.loop._proactor.wait_for_handle(event, 0.2)
|
|
|
|
start = self.loop.time()
|
|
|
|
self.loop.run_until_complete(f)
|
|
|
|
elapsed = self.loop.time() - start
|
|
|
|
self.assertFalse(f.result())
|
2013-11-18 06:05:22 -04:00
|
|
|
self.assertTrue(0.18 < elapsed < 0.5, elapsed)
|
2013-10-30 18:44:05 -03:00
|
|
|
|
|
|
|
_overlapped.SetEvent(event)
|
|
|
|
|
|
|
|
# Wait for for set event;
|
|
|
|
# result should be True immediately
|
|
|
|
f = self.loop._proactor.wait_for_handle(event, 10)
|
|
|
|
start = self.loop.time()
|
|
|
|
self.loop.run_until_complete(f)
|
|
|
|
elapsed = self.loop.time() - start
|
|
|
|
self.assertTrue(f.result())
|
2013-12-12 21:45:18 -04:00
|
|
|
self.assertTrue(0 <= elapsed < 0.1, elapsed)
|
2013-10-30 18:44:05 -03:00
|
|
|
|
|
|
|
_overlapped.ResetEvent(event)
|
|
|
|
|
|
|
|
# Wait for unset event with a cancelled future;
|
|
|
|
# CancelledError should be raised immediately
|
|
|
|
f = self.loop._proactor.wait_for_handle(event, 10)
|
|
|
|
f.cancel()
|
|
|
|
start = self.loop.time()
|
2014-01-25 10:32:06 -04:00
|
|
|
with self.assertRaises(asyncio.CancelledError):
|
2013-10-30 18:44:05 -03:00
|
|
|
self.loop.run_until_complete(f)
|
|
|
|
elapsed = self.loop.time() - start
|
2013-12-12 21:45:18 -04:00
|
|
|
self.assertTrue(0 <= elapsed < 0.1, elapsed)
|
2013-10-30 18:44:05 -03:00
|
|
|
|
2013-10-20 16:02:53 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|