def check_future_exception_never_retrieved(self, debug, m_log):
        self.loop.set_debug(debug)

        def memory_error():
            try:
                raise MemoryError()
            except BaseException as exc:
                return exc
        exc = memory_error()

        future = self._new_future(loop=self.loop)
        future.set_exception(exc)
        future = None
        test_utils.run_briefly(self.loop)
        support.gc_collect()

        if sys.version_info >= (3, 4):
            regex = f'^{self.cls.__name__} exception was never retrieved\n'
            exc_info = (type(exc), exc, exc.__traceback__)
            m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info)
        else:
            regex = r'^Future/Task exception was never retrieved\n'
            m_log.error.assert_called_once_with(mock.ANY, exc_info=False)
        message = m_log.error.call_args[0][0]
        self.assertRegex(message, re.compile(regex, re.DOTALL))
 def test_tb_logger_exception_unretrieved(self, m_log):
     fut = self._new_future(loop=self.loop)
     fut.set_exception(RuntimeError('boom'))
     del fut
     test_utils.run_briefly(self.loop)
     support.gc_collect()
     self.assertTrue(m_log.error.called)
 def test_ctor(self):
     fut = asyncio.Future(loop=self.loop)
     tr = self.socket_transport(waiter=fut)
     test_utils.run_briefly(self.loop)
     self.assertIsNone(fut.result())
     self.protocol.connection_made(tr)
     self.proactor.recv_into.assert_called_with(self.sock, self.buf)
Exemple #4
0
    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)
Exemple #5
0
    def test_notify_all(self):
        cond = asyncio.Condition(loop=self.loop)

        result = []

        async def c1(result):
            await cond.acquire()
            if await cond.wait():
                result.append(1)
                cond.release()
            return True

        async def c2(result):
            await cond.acquire()
            if await cond.wait():
                result.append(2)
                cond.release()
            return True

        t1 = asyncio.Task(c1(result), loop=self.loop)
        t2 = asyncio.Task(c2(result), loop=self.loop)

        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        self.loop.run_until_complete(cond.acquire())
        cond.notify_all()
        cond.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1, 2], result)

        self.assertTrue(t1.done())
        self.assertTrue(t1.result())
        self.assertTrue(t2.done())
        self.assertTrue(t2.result())
Exemple #6
0
 def test_eof_received_waiter(self):
     waiter = asyncio.Future(loop=self.loop)
     ssl_proto = self.ssl_protocol(waiter)
     self.connection_made(ssl_proto)
     ssl_proto.eof_received()
     test_utils.run_briefly(self.loop)
     self.assertIsInstance(waiter.exception(), ConnectionResetError)
Exemple #7
0
    def test_clear_with_waiters(self):
        ev = asyncio.Event(loop=self.loop)
        result = []

        async def c1(result):
            if await ev.wait():
                result.append(1)
            return True

        t = asyncio.Task(c1(result), loop=self.loop)
        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        ev.set()
        ev.clear()
        self.assertFalse(ev.is_set())

        ev.set()
        ev.set()
        self.assertEqual(1, len(ev._waiters))

        test_utils.run_briefly(self.loop)
        self.assertEqual([1], result)
        self.assertEqual(0, len(ev._waiters))

        self.assertTrue(t.done())
        self.assertTrue(t.result())
    def test_proto_type_switch(self):
        self.protocol = test_utils.make_test_protocol(asyncio.Protocol)
        tr = self.socket_transport()

        res = asyncio.Future(loop=self.loop)
        res.set_result(b'data')

        tr = self.socket_transport()
        tr._read_fut = res
        tr._loop_reading(res)
        self.loop._proactor.recv.assert_called_with(self.sock, 32768)
        self.protocol.data_received.assert_called_with(b'data')

        # switch protocol to a BufferedProtocol

        buf_proto = test_utils.make_test_protocol(asyncio.BufferedProtocol)
        buf = bytearray(4)
        buf_proto.get_buffer.side_effect = lambda hint: buf

        tr.set_protocol(buf_proto)
        test_utils.run_briefly(self.loop)
        res = asyncio.Future(loop=self.loop)
        res.set_result(4)

        tr._read_fut = res
        tr._loop_reading(res)
        self.loop._proactor.recv_into.assert_called_with(self.sock, buf)
        buf_proto.buffer_updated.assert_called_with(4)
    def tearDown(self):
        # just in case if we have transport close callbacks
        test_utils.run_briefly(self.loop)

        self.loop.close()
        gc.collect()
        super().tearDown()
Exemple #10
0
 def test_get_with_waiting_putters(self):
     q = asyncio.Queue(loop=self.loop, maxsize=1)
     asyncio.Task(q.put('a'), loop=self.loop)
     asyncio.Task(q.put('b'), loop=self.loop)
     test_utils.run_briefly(self.loop)
     self.assertEqual(self.loop.run_until_complete(q.get()), 'a')
     self.assertEqual(self.loop.run_until_complete(q.get()), 'b')
Exemple #11
0
 def test_wrap_future_cancel(self):
     f1 = concurrent.futures.Future()
     f2 = asyncio.wrap_future(f1, loop=self.loop)
     f2.cancel()
     test_utils.run_briefly(self.loop)
     self.assertTrue(f1.cancelled())
     self.assertTrue(f2.cancelled())
Exemple #12
0
    def test_del_stream_before_sock_closing(self):
        messages = []
        self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))

        with test_utils.run_test_server() as httpd:
            rd, wr = self.loop.run_until_complete(
                asyncio.open_connection(*httpd.address, loop=self.loop))
            sock = wr.get_extra_info('socket')
            self.assertNotEqual(sock.fileno(), -1)

            wr.write(b'GET / HTTP/1.0\r\n\r\n')
            f = rd.readline()
            data = self.loop.run_until_complete(f)
            self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')

            # drop refs to reader/writer
            del rd
            del wr
            gc.collect()
            # make a chance to close the socket
            test_utils.run_briefly(self.loop)

            self.assertEqual(1, len(messages))
            self.assertEqual(sock.fileno(), -1)

        self.assertEqual(1, len(messages))
        self.assertEqual('An open stream object is being garbage '
                         'collected; call "stream.close()" explicitly.',
                         messages[0]['message'])
Exemple #13
0
    def test_acquire(self):
        sem = asyncio.Semaphore(3, loop=self.loop)
        result = []

        self.assertTrue(self.loop.run_until_complete(sem.acquire()))
        self.assertTrue(self.loop.run_until_complete(sem.acquire()))
        self.assertFalse(sem.locked())

        async def c1(result):
            await sem.acquire()
            result.append(1)
            return True

        async def c2(result):
            await sem.acquire()
            result.append(2)
            return True

        async def c3(result):
            await sem.acquire()
            result.append(3)
            return True

        async def c4(result):
            await sem.acquire()
            result.append(4)
            return True

        t1 = asyncio.Task(c1(result), loop=self.loop)
        t2 = asyncio.Task(c2(result), loop=self.loop)
        t3 = asyncio.Task(c3(result), loop=self.loop)

        test_utils.run_briefly(self.loop)
        self.assertEqual([1], result)
        self.assertTrue(sem.locked())
        self.assertEqual(2, len(sem._waiters))
        self.assertEqual(0, sem._value)

        t4 = asyncio.Task(c4(result), loop=self.loop)

        sem.release()
        sem.release()
        self.assertEqual(2, sem._value)

        test_utils.run_briefly(self.loop)
        self.assertEqual(0, sem._value)
        self.assertEqual(3, len(result))
        self.assertTrue(sem.locked())
        self.assertEqual(1, len(sem._waiters))
        self.assertEqual(0, sem._value)

        self.assertTrue(t1.done())
        self.assertTrue(t1.result())
        race_tasks = [t2, t3, t4]
        done_tasks = [t for t in race_tasks if t.done() and t.result()]
        self.assertTrue(2, len(done_tasks))

        # cleanup locked semaphore
        sem.release()
        self.loop.run_until_complete(asyncio.gather(*race_tasks))
    def test_close_write_buffer(self):
        tr = self.create_transport()
        tr._buffer.extend(b'data')
        tr.close()

        self.assertFalse(self.loop.readers)
        test_utils.run_briefly(self.loop)
        self.assertFalse(self.protocol.connection_lost.called)
    def test_ctor(self):
        waiter = asyncio.Future(loop=self.loop)
        tr = self.socket_transport(waiter=waiter)
        self.loop.run_until_complete(waiter)

        self.loop.assert_reader(7, tr._read_ready)
        test_utils.run_briefly(self.loop)
        self.protocol.connection_made.assert_called_with(tr)
    def tearDown(self):
        # just in case if we have transport close callbacks
        if not self.loop.is_closed():
            test_utils.run_briefly(self.loop)

        self.doCleanups()
        support.gc_collect()
        super().tearDown()
 def _basetest_open_connection_error(self, open_connection_fut):
     reader, writer = self.loop.run_until_complete(open_connection_fut)
     writer._protocol.connection_lost(ZeroDivisionError())
     f = reader.read()
     with self.assertRaises(ZeroDivisionError):
         self.loop.run_until_complete(f)
     writer.close()
     test_utils.run_briefly(self.loop)
    def test_fatal_error_2(self):
        tr = self.socket_transport()
        tr._buffer = [b'data']
        tr._force_close(None)

        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertEqual(None, tr._buffer)
Exemple #19
0
 def test_wrap_future_cancel2(self):
     f1 = concurrent.futures.Future()
     f2 = asyncio.wrap_future(f1, loop=self.loop)
     f1.set_result(42)
     f2.cancel()
     test_utils.run_briefly(self.loop)
     self.assertFalse(f1.cancelled())
     self.assertEqual(f1.result(), 42)
     self.assertTrue(f2.cancelled())
 def test_connection_lost(self):
     # From issue #472.
     # yield from waiter hang if lost_connection was called.
     waiter = asyncio.Future(loop=self.loop)
     ssl_proto = self.ssl_protocol(waiter=waiter)
     self.connection_made(ssl_proto)
     ssl_proto.connection_lost(ConnectionAbortedError)
     test_utils.run_briefly(self.loop)
     self.assertIsInstance(waiter.exception(), ConnectionAbortedError)
Exemple #21
0
 def test_wrap_future_cancel2(self):
     f1 = concurrent.futures.Future()
     f2 = asyncio.wrap_future(f1, loop=self.loop)
     f1.set_result(42)
     f2.cancel()
     test_utils.run_briefly(self.loop)
     self.assertFalse(f1.cancelled())
     self.assertEqual(f1.result(), 42)
     self.assertTrue(f2.cancelled())
Exemple #22
0
 def test_connection_lost(self):
     # From issue #472.
     # yield from waiter hang if lost_connection was called.
     waiter = asyncio.Future(loop=self.loop)
     ssl_proto = self.ssl_protocol(waiter)
     self.connection_made(ssl_proto)
     ssl_proto.connection_lost(ConnectionAbortedError)
     test_utils.run_briefly(self.loop)
     self.assertIsInstance(waiter.exception(), ConnectionAbortedError)
Exemple #23
0
 def test_eof_received_waiter(self):
     waiter = self.loop.create_future()
     ssl_proto = self.ssl_protocol(waiter=waiter)
     self.connection_made(
         ssl_proto,
         do_handshake=mock.Mock(side_effect=ssl.SSLWantReadError))
     ssl_proto.eof_received()
     test_utils.run_briefly(self.loop)
     self.assertIsInstance(waiter.exception(), ConnectionResetError)
Exemple #24
0
    def test_close_during_handshake(self):
        # bpo-29743 Closing transport during handshake process leaks socket
        waiter = asyncio.Future(loop=self.loop)
        ssl_proto = self.ssl_protocol(waiter=waiter)

        transport = self.connection_made(ssl_proto)
        test_utils.run_briefly(self.loop)

        ssl_proto._app_transport.close()
        self.assertTrue(transport.abort.called)
Exemple #25
0
    def test_close_during_handshake(self):
        # bpo-29743 Closing transport during handshake process leaks socket
        waiter = asyncio.Future(loop=self.loop)
        ssl_proto = self.ssl_protocol(waiter=waiter)

        transport = self.connection_made(ssl_proto)
        test_utils.run_briefly(self.loop)

        ssl_proto._app_transport.close()
        self.assertTrue(transport.abort.called)
Exemple #26
0
 def _basetest_open_connection_error(self, open_connection_fut):
     messages = []
     self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
     reader, writer = self.loop.run_until_complete(open_connection_fut)
     writer._protocol.connection_lost(ZeroDivisionError())
     f = reader.read()
     with self.assertRaises(ZeroDivisionError):
         self.loop.run_until_complete(f)
     writer.close()
     test_utils.run_briefly(self.loop)
     self.assertEqual(messages, [])
Exemple #27
0
 def test_connection_lost(self):
     # From issue #472.
     # yield from waiter hang if lost_connection was called.
     waiter = self.loop.create_future()
     ssl_proto = self.ssl_protocol(waiter=waiter)
     self.connection_made(
         ssl_proto,
         do_handshake=mock.Mock(side_effect=ssl.SSLWantReadError))
     ssl_proto.connection_lost(ConnectionAbortedError)
     test_utils.run_briefly(self.loop)
     self.assertIsInstance(waiter.exception(), ConnectionAbortedError)
Exemple #28
0
 def _basetest_open_connection_error(self, open_connection_fut):
     messages = []
     self.loop.set_exception_handler(lambda loop, ctx: messages.append(ctx))
     reader, writer = self.loop.run_until_complete(open_connection_fut)
     writer._protocol.connection_lost(ZeroDivisionError())
     f = reader.read()
     with self.assertRaises(ZeroDivisionError):
         self.loop.run_until_complete(f)
     writer.close()
     test_utils.run_briefly(self.loop)
     self.assertEqual(messages, [])
    def test_loop_writing_closing(self):
        fut = self.loop.create_future()
        fut.set_result(1)

        tr = self.socket_transport()
        tr._write_fut = fut
        tr.close()
        tr._loop_writing(fut)
        self.assertIsNone(tr._write_fut)
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
    def test_loop_writing_closing(self):
        fut = asyncio.Future(loop=self.loop)
        fut.set_result(1)

        tr = self.socket_transport()
        tr._write_fut = fut
        tr.close()
        tr._loop_writing(fut)
        self.assertIsNone(tr._write_fut)
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
    def test_close(self):
        tr = self.socket_transport()
        tr.close()
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertTrue(tr.is_closing())
        self.assertEqual(tr._conn_lost, 1)

        self.protocol.connection_lost.reset_mock()
        tr.close()
        test_utils.run_briefly(self.loop)
        self.assertFalse(self.protocol.connection_lost.called)
    def test_loop_writing_force_close(self):
        exc_handler = mock.Mock()
        self.loop.set_exception_handler(exc_handler)
        fut = self.loop.create_future()
        fut.set_result(1)
        self.proactor.send.return_value = fut

        tr = self.socket_transport()
        tr.write(b'data')
        tr._force_close(None)
        test_utils.run_briefly(self.loop)
        exc_handler.assert_not_called()
Exemple #33
0
    def test_schedule_callbacks_list_mutation_1(self):
        # see http://bugs.python.org/issue28963 for details

        def mut(f):
            f.remove_done_callback(str)

        fut = self._new_future()
        fut.add_done_callback(mut)
        fut.add_done_callback(str)
        fut.add_done_callback(str)
        fut.set_result(1)
        test_utils.run_briefly(self.loop)
Exemple #34
0
    def test_schedule_callbacks_list_mutation_1(self):
        # see http://bugs.python.org/issue28963 for details

        def mut(f):
            f.remove_done_callback(str)

        fut = self._new_future()
        fut.add_done_callback(mut)
        fut.add_done_callback(str)
        fut.add_done_callback(str)
        fut.set_result(1)
        test_utils.run_briefly(self.loop)
Exemple #35
0
    def test_close_during_handshake(self):
        # bpo-29743 Closing transport during handshake process leaks socket
        waiter = self.loop.create_future()
        ssl_proto = self.ssl_protocol(waiter=waiter)

        transport = self.connection_made(
            ssl_proto,
            do_handshake=mock.Mock(side_effect=ssl.SSLWantReadError))
        test_utils.run_briefly(self.loop)

        ssl_proto._app_transport.close()
        self.assertTrue(transport.abort.called)
    def test_close(self):
        tr = self.socket_transport()
        tr.close()
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertTrue(tr.is_closing())
        self.assertEqual(tr._conn_lost, 1)

        self.protocol.connection_lost.reset_mock()
        tr.close()
        test_utils.run_briefly(self.loop)
        self.assertFalse(self.protocol.connection_lost.called)
    def test_loop_writing_force_close(self):
        exc_handler = mock.Mock()
        self.loop.set_exception_handler(exc_handler)
        fut = asyncio.Future(loop=self.loop)
        fut.set_result(1)
        self.proactor.send.return_value = fut

        tr = self.socket_transport()
        tr.write(b'data')
        tr._force_close(None)
        test_utils.run_briefly(self.loop)
        exc_handler.assert_not_called()
Exemple #38
0
    def test_acquire_hang(self):
        sem = asyncio.Semaphore(value=0, loop=self.loop)

        t1 = asyncio.Task(sem.acquire(), loop=self.loop)
        t2 = asyncio.Task(sem.acquire(), loop=self.loop)

        test_utils.run_briefly(self.loop)

        sem.release()
        t1.cancel()

        test_utils.run_briefly(self.loop)
        self.assertTrue(sem.locked())
Exemple #39
0
    def test_acquire_hang(self):
        sem = asyncio.Semaphore(value=0, loop=self.loop)

        t1 = asyncio.Task(sem.acquire(), loop=self.loop)
        t2 = asyncio.Task(sem.acquire(), loop=self.loop)

        test_utils.run_briefly(self.loop)

        sem.release()
        t1.cancel()

        test_utils.run_briefly(self.loop)
        self.assertTrue(sem.locked())
    def test_force_close(self):
        tr = self.socket_transport()
        tr._buffer = [b'data']
        read_fut = tr._read_fut = mock.Mock()
        write_fut = tr._write_fut = mock.Mock()
        tr._force_close(None)

        read_fut.cancel.assert_called_with()
        write_fut.cancel.assert_called_with()
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertEqual(None, tr._buffer)
        self.assertEqual(tr._conn_lost, 1)
    def test_force_close(self):
        tr = self.socket_transport()
        tr._buffer = [b'data']
        read_fut = tr._read_fut = mock.Mock()
        write_fut = tr._write_fut = mock.Mock()
        tr._force_close(None)

        read_fut.cancel.assert_called_with()
        write_fut.cancel.assert_called_with()
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertEqual(None, tr._buffer)
        self.assertEqual(tr._conn_lost, 1)
Exemple #42
0
    def test_acquire(self):
        with self.assertWarns(DeprecationWarning):
            lock = asyncio.Lock(loop=self.loop)
        result = []

        self.assertTrue(self.loop.run_until_complete(lock.acquire()))

        async def c1(result):
            if await lock.acquire():
                result.append(1)
            return True

        async def c2(result):
            if await lock.acquire():
                result.append(2)
            return True

        async def c3(result):
            if await lock.acquire():
                result.append(3)
            return True

        t1 = self.loop.create_task(c1(result))
        t2 = self.loop.create_task(c2(result))

        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        lock.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1], result)

        test_utils.run_briefly(self.loop)
        self.assertEqual([1], result)

        t3 = self.loop.create_task(c3(result))

        lock.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1, 2], result)

        lock.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1, 2, 3], result)

        self.assertTrue(t1.done())
        self.assertTrue(t1.result())
        self.assertTrue(t2.done())
        self.assertTrue(t2.result())
        self.assertTrue(t3.done())
        self.assertTrue(t3.result())
Exemple #43
0
    def test_acquire_hang(self):
        with self.assertWarns(DeprecationWarning):
            sem = asyncio.Semaphore(value=0, loop=self.loop)

        t1 = self.loop.create_task(sem.acquire())
        t2 = self.loop.create_task(sem.acquire())

        test_utils.run_briefly(self.loop)

        sem.release()
        t1.cancel()

        test_utils.run_briefly(self.loop)
        self.assertTrue(sem.locked())
Exemple #44
0
    def test_acquire(self):
        lock = asyncio.Lock(loop=self.loop)
        result = []

        self.assertTrue(self.loop.run_until_complete(lock.acquire()))

        async def c1(result):
            if await lock.acquire():
                result.append(1)
            return True

        async def c2(result):
            if await lock.acquire():
                result.append(2)
            return True

        async def c3(result):
            if await lock.acquire():
                result.append(3)
            return True

        t1 = asyncio.Task(c1(result), loop=self.loop)
        t2 = asyncio.Task(c2(result), loop=self.loop)

        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        lock.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1], result)

        test_utils.run_briefly(self.loop)
        self.assertEqual([1], result)

        t3 = asyncio.Task(c3(result), loop=self.loop)

        lock.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1, 2], result)

        lock.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1, 2, 3], result)

        self.assertTrue(t1.done())
        self.assertTrue(t1.result())
        self.assertTrue(t2.done())
        self.assertTrue(t2.result())
        self.assertTrue(t3.done())
        self.assertTrue(t3.result())
Exemple #45
0
    def test_wait_cancel_after_notify(self):
        # See bpo-32841
        waited = False

        cond = asyncio.Condition()
        cond._loop = self.loop

        async def wait_on_cond():
            nonlocal waited
            async with cond:
                waited = True  # Make sure this area was reached
                await cond.wait()

        waiter = asyncio.ensure_future(wait_on_cond(), loop=self.loop)
        test_utils.run_briefly(self.loop)  # Start waiting

        self.loop.run_until_complete(cond.acquire())
        cond.notify()
        test_utils.run_briefly(self.loop)  # Get to acquire()
        waiter.cancel()
        test_utils.run_briefly(self.loop)  # Activate cancellation
        cond.release()
        test_utils.run_briefly(self.loop)  # Cancellation should occur

        self.assertTrue(waiter.cancelled())
        self.assertTrue(waited)
    def test_proto_buf_switch(self):
        tr = self.socket_transport()
        test_utils.run_briefly(self.loop)
        self.protocol.get_buffer.assert_called_with(-1)

        # switch protocol to *another* BufferedProtocol

        buf_proto = test_utils.make_test_protocol(asyncio.BufferedProtocol)
        buf = bytearray(4)
        buf_proto.get_buffer.side_effect = lambda hint: buf
        tr._read_fut.done.side_effect = lambda: False
        tr.set_protocol(buf_proto)
        self.assertFalse(buf_proto.get_buffer.called)
        test_utils.run_briefly(self.loop)
        buf_proto.get_buffer.assert_called_with(-1)
Exemple #47
0
    def test_proto_buf_switch(self):
        tr = self.socket_transport()
        test_utils.run_briefly(self.loop)
        self.protocol.get_buffer.assert_called_with(-1)

        # switch protocol to *another* BufferedProtocol

        buf_proto = test_utils.make_test_protocol(asyncio.BufferedProtocol)
        buf = bytearray(4)
        buf_proto.get_buffer.side_effect = lambda hint: buf
        tr._read_fut.done.side_effect = lambda: False
        tr.set_protocol(buf_proto)
        self.assertFalse(buf_proto.get_buffer.called)
        test_utils.run_briefly(self.loop)
        buf_proto.get_buffer.assert_called_with(-1)
Exemple #48
0
    def test_cancel_post_init(self):
        async def cancel_make_transport():
            coro = self.loop.subprocess_exec(asyncio.SubprocessProtocol,
                                             *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())
            test_utils.run_briefly(self.loop)
Exemple #49
0
    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()
Exemple #50
0
    def test_pause_resume_reading(self):
        tr = self.socket_transport()
        test_utils.run_briefly(self.loop)
        self.assertFalse(tr._paused)
        self.assertTrue(tr.is_reading())
        self.loop.assert_reader(7, tr._read_ready)

        tr.pause_reading()
        tr.pause_reading()
        self.assertTrue(tr._paused)
        self.assertFalse(tr.is_reading())
        self.loop.assert_no_reader(7)

        tr.resume_reading()
        tr.resume_reading()
        self.assertFalse(tr._paused)
        self.assertTrue(tr.is_reading())
        self.loop.assert_reader(7, tr._read_ready)

        tr.close()
        self.assertFalse(tr.is_reading())
        self.loop.assert_no_reader(7)
Exemple #51
0
    def check_future_exception_never_retrieved(self, debug, m_log):
        self.loop.set_debug(debug)

        def memory_error():
            try:
                raise MemoryError()
            except BaseException as exc:
                return exc
        exc = memory_error()

        future = self._new_future(loop=self.loop)
        future.set_exception(exc)
        future = None
        test_utils.run_briefly(self.loop)
        support.gc_collect()

        regex = f'^{self.cls.__name__} exception was never retrieved\n'
        exc_info = (type(exc), exc, exc.__traceback__)
        m_log.error.assert_called_once_with(mock.ANY, exc_info=exc_info)

        message = m_log.error.call_args[0][0]
        self.assertRegex(message, re.compile(regex, re.DOTALL))
Exemple #52
0
    def test_cancel_release_race(self):
        # Issue 32734
        # Acquire 4 locks, cancel second, release first
        # and 2 locks are taken at once.
        with self.assertWarns(DeprecationWarning):
            lock = asyncio.Lock(loop=self.loop)
        lock_count = 0
        call_count = 0

        async def lockit():
            nonlocal lock_count
            nonlocal call_count
            call_count += 1
            await lock.acquire()
            lock_count += 1

        async def lockandtrigger():
            await lock.acquire()
            self.loop.call_soon(trigger)

        def trigger():
            t1.cancel()
            lock.release()

        t0 = self.loop.create_task(lockandtrigger())
        t1 = self.loop.create_task(lockit())
        t2 = self.loop.create_task(lockit())
        t3 = self.loop.create_task(lockit())

        # First loop acquires all
        test_utils.run_briefly(self.loop)
        self.assertTrue(t0.done())

        # Second loop calls trigger
        test_utils.run_briefly(self.loop)
        # Third loop calls cancellation
        test_utils.run_briefly(self.loop)

        # Make sure only one lock was taken
        self.assertEqual(lock_count, 1)
        # While 3 calls were made to lockit()
        self.assertEqual(call_count, 3)
        self.assertTrue(t1.cancelled() and t2.done())

        # Cleanup the task that is stuck on acquire.
        t3.cancel()
        test_utils.run_briefly(self.loop)
        self.assertTrue(t3.cancelled())
Exemple #53
0
    def test_cancel_race(self):
        # Several tasks:
        # - A acquires the lock
        # - B is blocked in acquire()
        # - C is blocked in acquire()
        #
        # Now, concurrently:
        # - B is cancelled
        # - A releases the lock
        #
        # If B's waiter is marked cancelled but not yet removed from
        # _waiters, A's release() call will crash when trying to set
        # B's waiter; instead, it should move on to C's waiter.

        # Setup: A has the lock, b and c are waiting.
        with self.assertWarns(DeprecationWarning):
            lock = asyncio.Lock(loop=self.loop)

        async def lockit(name, blocker):
            await lock.acquire()
            try:
                if blocker is not None:
                    await blocker
            finally:
                lock.release()

        fa = self.loop.create_future()
        ta = self.loop.create_task(lockit('A', fa))
        test_utils.run_briefly(self.loop)
        self.assertTrue(lock.locked())
        tb = self.loop.create_task(lockit('B', None))
        test_utils.run_briefly(self.loop)
        self.assertEqual(len(lock._waiters), 1)
        tc = self.loop.create_task(lockit('C', None))
        test_utils.run_briefly(self.loop)
        self.assertEqual(len(lock._waiters), 2)

        # Create the race and check.
        # Without the fix this failed at the last assert.
        fa.set_result(None)
        tb.cancel()
        self.assertTrue(lock._waiters[0].cancelled())
        test_utils.run_briefly(self.loop)
        self.assertFalse(lock.locked())
        self.assertTrue(ta.done())
        self.assertTrue(tb.cancelled())
        self.assertTrue(tc.done())
Exemple #54
0
    def test_wait(self):
        with self.assertWarns(DeprecationWarning):
            ev = asyncio.Event(loop=self.loop)
        self.assertFalse(ev.is_set())

        result = []

        async def c1(result):
            if await ev.wait():
                result.append(1)

        async def c2(result):
            if await ev.wait():
                result.append(2)

        async def c3(result):
            if await ev.wait():
                result.append(3)

        t1 = self.loop.create_task(c1(result))
        t2 = self.loop.create_task(c2(result))

        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        t3 = self.loop.create_task(c3(result))

        ev.set()
        test_utils.run_briefly(self.loop)
        self.assertEqual([3, 1, 2], result)

        self.assertTrue(t1.done())
        self.assertIsNone(t1.result())
        self.assertTrue(t2.done())
        self.assertIsNone(t2.result())
        self.assertTrue(t3.done())
        self.assertIsNone(t3.result())
Exemple #55
0
    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)
Exemple #56
0
    def test_wait_cancel_contested(self):
        cond = asyncio.Condition(loop=self.loop)

        self.loop.run_until_complete(cond.acquire())
        self.assertTrue(cond.locked())

        wait_task = asyncio.Task(cond.wait(), loop=self.loop)
        test_utils.run_briefly(self.loop)
        self.assertFalse(cond.locked())

        # Notify, but contest the lock before cancelling
        self.loop.run_until_complete(cond.acquire())
        self.assertTrue(cond.locked())
        cond.notify()
        self.loop.call_soon(wait_task.cancel)
        self.loop.call_soon(cond.release)

        try:
            self.loop.run_until_complete(wait_task)
        except asyncio.CancelledError:
            # Should not happen, since no cancellation points
            pass

        self.assertTrue(cond.locked())
Exemple #57
0
    def test_wait(self):
        ev = asyncio.Event(loop=self.loop)
        self.assertFalse(ev.is_set())

        result = []

        async def c1(result):
            if await ev.wait():
                result.append(1)

        async def c2(result):
            if await ev.wait():
                result.append(2)

        async def c3(result):
            if await ev.wait():
                result.append(3)

        t1 = asyncio.Task(c1(result), loop=self.loop)
        t2 = asyncio.Task(c2(result), loop=self.loop)

        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        t3 = asyncio.Task(c3(result), loop=self.loop)

        ev.set()
        test_utils.run_briefly(self.loop)
        self.assertEqual([3, 1, 2], result)

        self.assertTrue(t1.done())
        self.assertIsNone(t1.result())
        self.assertTrue(t2.done())
        self.assertIsNone(t2.result())
        self.assertTrue(t3.done())
        self.assertIsNone(t3.result())
Exemple #58
0
    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)
Exemple #59
0
    def test_notify_all(self):
        with self.assertWarns(DeprecationWarning):
            cond = asyncio.Condition(loop=self.loop)

        result = []

        async def c1(result):
            await cond.acquire()
            if await cond.wait():
                result.append(1)
                cond.release()
            return True

        async def c2(result):
            await cond.acquire()
            if await cond.wait():
                result.append(2)
                cond.release()
            return True

        t1 = self.loop.create_task(c1(result))
        t2 = self.loop.create_task(c2(result))

        test_utils.run_briefly(self.loop)
        self.assertEqual([], result)

        self.loop.run_until_complete(cond.acquire())
        cond.notify_all()
        cond.release()
        test_utils.run_briefly(self.loop)
        self.assertEqual([1, 2], result)

        self.assertTrue(t1.done())
        self.assertTrue(t1.result())
        self.assertTrue(t2.done())
        self.assertTrue(t2.result())
Exemple #60
0
    def test_exception_cancel(self):
        stream = asyncio.StreamReader(loop=self.loop, _asyncio_internal=True)

        t = asyncio.Task(stream.readline(), loop=self.loop)
        test_utils.run_briefly(self.loop)
        t.cancel()
        test_utils.run_briefly(self.loop)
        # The following line fails if set_exception() isn't careful.
        stream.set_exception(RuntimeError('message'))
        test_utils.run_briefly(self.loop)
        self.assertIs(stream._waiter, None)