def test_close_kill_running(self): async def kill_running(): create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, *PROGRAM_BLOCKED) transport, protocol = await create kill_called = False def kill(): nonlocal kill_called kill_called = True orig_kill() proc = transport.get_extra_info('subprocess') orig_kill = proc.kill proc.kill = kill returncode = transport.get_returncode() transport.close() await transport._wait() return (returncode, kill_called) # Ignore "Close running child process: kill ..." log with test_utils.disable_logger(): returncode, killed = self.loop.run_until_complete(kill_running()) self.assertIsNone(returncode) # transport.close() must kill the process if it is still running self.assertTrue(killed) test_utils.run_briefly(self.loop)
def test_communicate_ignore_broken_pipe(self): proc, large_data = self.prepare_broken_pipe_test() # communicate() must ignore BrokenPipeError when feeding stdin with test_utils.disable_logger(): self.loop.run_until_complete(proc.communicate(large_data)) self.loop.run_until_complete(proc.wait())
def test_close_kill_running(self): async def kill_running(): create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, *PROGRAM_BLOCKED) transport, protocol = await create kill_called = False def kill(): nonlocal kill_called kill_called = True orig_kill() proc = transport.get_extra_info('subprocess') orig_kill = proc.kill proc.kill = kill returncode = transport.get_returncode() transport.close() await asyncio.wait_for(transport._wait(), 5) return (returncode, kill_called) # Ignore "Close running child process: kill ..." log with test_utils.disable_logger(): try: returncode, killed = self.loop.run_until_complete( kill_running()) except asyncio.TimeoutError: self.skipTest( "Timeout failure on waiting for subprocess stopping") self.assertIsNone(returncode) # transport.close() must kill the process if it is still running self.assertTrue(killed) test_utils.run_briefly(self.loop)
def test_read_ready_conn_reset(self, m_exc): err = self.sock.recv.side_effect = ConnectionResetError() transport = self.socket_transport() transport._force_close = mock.Mock() with test_utils.disable_logger(): transport._read_ready() transport._force_close.assert_called_with(err)
def test_handshake_timeout(self): # bpo-29970: Check that a connection is aborted if handshake is not # completed in timeout period, instead of remaining open indefinitely ssl_proto = self.ssl_protocol() transport = self.connection_made(ssl_proto) with test_utils.disable_logger(): self.loop.run_until_complete(tasks.sleep(0.2, loop=self.loop)) self.assertTrue(transport.abort.called)
def test_stdin_broken_pipe(self): proc, large_data = self.prepare_broken_pipe_test() async def write_stdin(proc, data): proc.stdin.write(data) await proc.stdin.drain() coro = write_stdin(proc, large_data) # drain() must raise BrokenPipeError or ConnectionResetError with test_utils.disable_logger(): self.assertRaises((BrokenPipeError, ConnectionResetError), self.loop.run_until_complete, coro) self.loop.run_until_complete(proc.wait())
def test_cancel_make_subprocess_transport_exec(self): async def cancel_make_transport(): coro = asyncio.create_subprocess_exec(*PROGRAM_BLOCKED) task = self.loop.create_task(coro) self.loop.call_soon(task.cancel) try: await task except asyncio.CancelledError: pass # ignore the log: # "Exception during subprocess creation, kill the subprocess" with test_utils.disable_logger(): self.loop.run_until_complete(cancel_make_transport())
def test_cancel_make_subprocess_transport_exec(self): async def cancel_make_transport(): coro = asyncio.create_subprocess_exec(*PROGRAM_BLOCKED, loop=self.loop) task = self.loop.create_task(coro) self.loop.call_soon(task.cancel) try: await task except asyncio.CancelledError: pass # ignore the log: # "Exception during subprocess creation, kill the subprocess" with test_utils.disable_logger(): self.loop.run_until_complete(cancel_make_transport())
def test_cancel_handshake(self): # Python issue #23197: cancelling a handshake must not raise an # exception or log an error, even if the handshake failed waiter = asyncio.Future(loop=self.loop) ssl_proto = self.ssl_protocol(waiter) handshake_fut = asyncio.Future(loop=self.loop) def do_handshake(callback): exc = Exception() callback(exc) handshake_fut.set_result(None) return [] waiter.cancel() self.connection_made(ssl_proto, do_handshake) with test_utils.disable_logger(): self.loop.run_until_complete(handshake_fut)
def test_close_dont_kill_finished(self): async def kill_running(): create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, *PROGRAM_BLOCKED) transport, protocol = await create proc = transport.get_extra_info('subprocess') # kill the process (but asyncio is not notified immediately) proc.kill() proc.wait() proc.kill = mock.Mock() proc_returncode = proc.poll() transport_returncode = transport.get_returncode() transport.close() return (proc_returncode, transport_returncode, proc.kill.called) # Ignore "Unknown child process pid ..." log of SafeChildWatcher, # emitted because the test already consumes the exit status: # proc.wait() with test_utils.disable_logger(): result = self.loop.run_until_complete(kill_running()) test_utils.run_briefly(self.loop) proc_returncode, transport_return_code, killed = result self.assertIsNotNone(proc_returncode) self.assertIsNone(transport_return_code) # transport.close() must not kill the process if it finished, even if # the transport was not notified yet self.assertFalse(killed) # Unlike SafeChildWatcher, FastChildWatcher does not pop the # callbacks if waitpid() is called elsewhere. Let's clear them # manually to avoid a warning when the watcher is detached. if (sys.platform != 'win32' and isinstance(self, SubprocessFastWatcherTests)): asyncio.get_child_watcher()._callbacks.clear()
def test_make_ssl_transport(self): m = mock.Mock() self.loop._add_reader = mock.Mock() self.loop._add_reader._is_coroutine = False self.loop._add_writer = mock.Mock() self.loop._remove_reader = mock.Mock() self.loop._remove_writer = mock.Mock() waiter = asyncio.Future(loop=self.loop) with test_utils.disable_logger(): transport = self.loop._make_ssl_transport( m, asyncio.Protocol(), m, waiter) with self.assertRaisesRegex(RuntimeError, r'SSL transport.*not.*initialized'): transport.is_reading() # execute the handshake while the logger is disabled # to ignore SSL handshake failure test_utils.run_briefly(self.loop) self.assertTrue(transport.is_reading()) transport.pause_reading() transport.pause_reading() self.assertFalse(transport.is_reading()) transport.resume_reading() transport.resume_reading() self.assertTrue(transport.is_reading()) # Sanity check class_name = transport.__class__.__name__ self.assertIn("ssl", class_name.lower()) self.assertIn("transport", class_name.lower()) transport.close() # execute pending callbacks to close the socket transport test_utils.run_briefly(self.loop)
def test_make_ssl_transport(self): m = mock.Mock() self.loop._add_reader = mock.Mock() self.loop._add_reader._is_coroutine = False self.loop._add_writer = mock.Mock() self.loop._remove_reader = mock.Mock() self.loop._remove_writer = mock.Mock() waiter = asyncio.Future(loop=self.loop) with test_utils.disable_logger(): transport = self.loop._make_ssl_transport(m, asyncio.Protocol(), m, waiter) with self.assertRaisesRegex(RuntimeError, r'SSL transport.*not.*initialized'): transport.is_reading() # execute the handshake while the logger is disabled # to ignore SSL handshake failure test_utils.run_briefly(self.loop) self.assertTrue(transport.is_reading()) transport.pause_reading() transport.pause_reading() self.assertFalse(transport.is_reading()) transport.resume_reading() transport.resume_reading() self.assertTrue(transport.is_reading()) # Sanity check class_name = transport.__class__.__name__ self.assertIn("ssl", class_name.lower()) self.assertIn("transport", class_name.lower()) transport.close() # execute pending callbacks to close the socket transport test_utils.run_briefly(self.loop)
def test_write_to_self_tryagain(self): self.loop._csock.send.side_effect = BlockingIOError with test_utils.disable_logger(): self.assertIsNone(self.loop._write_to_self())