diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-12-20 10:23:05 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-20 10:23:05 (GMT) |
commit | 6ca78affc8023bc5023189d64d8050857662042a (patch) | |
tree | e6cae1875586d1c722005311492b4126707d9a2e | |
parent | 7c5c3f7254d78babcaf7a2ec187fd6ec53b8403c (diff) | |
download | cpython-6ca78affc8023bc5023189d64d8050857662042a.zip cpython-6ca78affc8023bc5023189d64d8050857662042a.tar.gz cpython-6ca78affc8023bc5023189d64d8050857662042a.tar.bz2 |
bpo-23819: Get rid of assert statements in test_asyncio (GH-30212)
To keep checks even if run tests with optimized Python.
Either use special assertion methods like assertEqual() or
raise an AssertionError explicitly.
-rw-r--r-- | Lib/test/test_asyncio/test_base_events.py | 30 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_events.py | 60 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_futures.py | 18 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_sendfile.py | 12 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_sock_lowlevel.py | 14 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_sslproto.py | 6 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_streams.py | 4 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_tasks.py | 4 | ||||
-rw-r--r-- | Lib/test/test_asyncio/test_unix_events.py | 3 | ||||
-rw-r--r-- | Lib/test/test_asyncio/utils.py | 14 |
10 files changed, 102 insertions, 63 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index d812bc9..4c0d09e 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -647,7 +647,7 @@ class BaseEventLoopTests(test_utils.TestCase): 'Exception in callback.*zero'), exc_info=(ZeroDivisionError, MOCK_ANY, MOCK_ANY)) - assert not mock_handler.called + self.assertFalse(mock_handler.called) def test_set_exc_handler_broken(self): def run_loop(): @@ -1007,22 +1007,26 @@ class MyProto(asyncio.Protocol): if create_future: self.done = asyncio.get_running_loop().create_future() + def _assert_state(self, *expected): + if self.state not in expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'CONNECTED' transport.write(b'GET / HTTP/1.0\r\nHost: example.com\r\n\r\n') def data_received(self, data): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.nbytes += len(data) def eof_received(self): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.state = 'EOF' def connection_lost(self, exc): - assert self.state in ('CONNECTED', 'EOF'), self.state + self._assert_state('CONNECTED', 'EOF') self.state = 'CLOSED' if self.done: self.done.set_result(None) @@ -1037,20 +1041,24 @@ class MyDatagramProto(asyncio.DatagramProtocol): if create_future: self.done = loop.create_future() + def _assert_state(self, expected): + if self.state != expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'INITIALIZED' def datagram_received(self, data, addr): - assert self.state == 'INITIALIZED', self.state + self._assert_state('INITIALIZED') self.nbytes += len(data) def error_received(self, exc): - assert self.state == 'INITIALIZED', self.state + self._assert_state('INITIALIZED') def connection_lost(self, exc): - assert self.state == 'INITIALIZED', self.state + self._assert_state('INITIALIZED') self.state = 'CLOSED' if self.done: self.done.set_result(None) @@ -1399,7 +1407,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase): addr = ('00:01:02:03:04:05', 1) def getaddrinfo(host, port, *args, **kw): - assert (host, port) == addr + self.assertEqual((host, port), addr) return [(999, 1, 999, '', (addr, 1))] m_socket.getaddrinfo = getaddrinfo @@ -1698,7 +1706,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase): lambda: MyDatagramProto(create_future=True, loop=self.loop), family=socket.AF_UNIX) transport, protocol = self.loop.run_until_complete(fut) - assert transport._sock.family == socket.AF_UNIX + self.assertEqual(transport._sock.family, socket.AF_UNIX) transport.close() self.loop.run_until_complete(protocol.done) self.assertEqual('CLOSED', protocol.state) diff --git a/Lib/test/test_asyncio/test_events.py b/Lib/test/test_asyncio/test_events.py index fe791fa..c46c9dd 100644 --- a/Lib/test/test_asyncio/test_events.py +++ b/Lib/test/test_asyncio/test_events.py @@ -88,23 +88,27 @@ class MyBaseProto(asyncio.Protocol): self.connected = loop.create_future() self.done = loop.create_future() + def _assert_state(self, *expected): + if self.state not in expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'CONNECTED' if self.connected: self.connected.set_result(None) def data_received(self, data): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.nbytes += len(data) def eof_received(self): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.state = 'EOF' def connection_lost(self, exc): - assert self.state in ('CONNECTED', 'EOF'), self.state + self._assert_state('CONNECTED', 'EOF') self.state = 'CLOSED' if self.done: self.done.set_result(None) @@ -125,20 +129,24 @@ class MyDatagramProto(asyncio.DatagramProtocol): if loop is not None: self.done = loop.create_future() + def _assert_state(self, expected): + if self.state != expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'INITIALIZED' def datagram_received(self, data, addr): - assert self.state == 'INITIALIZED', self.state + self._assert_state('INITIALIZED') self.nbytes += len(data) def error_received(self, exc): - assert self.state == 'INITIALIZED', self.state + self._assert_state('INITIALIZED') def connection_lost(self, exc): - assert self.state == 'INITIALIZED', self.state + self._assert_state('INITIALIZED') self.state = 'CLOSED' if self.done: self.done.set_result(None) @@ -154,23 +162,27 @@ class MyReadPipeProto(asyncio.Protocol): if loop is not None: self.done = loop.create_future() + def _assert_state(self, expected): + if self.state != expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == ['INITIAL'], self.state + self._assert_state(['INITIAL']) self.state.append('CONNECTED') def data_received(self, data): - assert self.state == ['INITIAL', 'CONNECTED'], self.state + self._assert_state(['INITIAL', 'CONNECTED']) self.nbytes += len(data) def eof_received(self): - assert self.state == ['INITIAL', 'CONNECTED'], self.state + self._assert_state(['INITIAL', 'CONNECTED']) self.state.append('EOF') def connection_lost(self, exc): if 'EOF' not in self.state: self.state.append('EOF') # It is okay if EOF is missed. - assert self.state == ['INITIAL', 'CONNECTED', 'EOF'], self.state + self._assert_state(['INITIAL', 'CONNECTED', 'EOF']) self.state.append('CLOSED') if self.done: self.done.set_result(None) @@ -185,13 +197,17 @@ class MyWritePipeProto(asyncio.BaseProtocol): if loop is not None: self.done = loop.create_future() + def _assert_state(self, expected): + if self.state != expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'CONNECTED' def connection_lost(self, exc): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.state = 'CLOSED' if self.done: self.done.set_result(None) @@ -210,31 +226,35 @@ class MySubprocessProtocol(asyncio.SubprocessProtocol): self.got_data = {1: asyncio.Event(), 2: asyncio.Event()} + def _assert_state(self, expected): + if self.state != expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'CONNECTED' self.connected.set_result(None) def connection_lost(self, exc): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.state = 'CLOSED' self.completed.set_result(None) def pipe_data_received(self, fd, data): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.data[fd] += data self.got_data[fd].set() def pipe_connection_lost(self, fd, exc): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') if exc: self.disconnects[fd].set_exception(exc) else: self.disconnects[fd].set_result(exc) def process_exited(self): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.returncode = self.transport.get_returncode() @@ -1284,7 +1304,7 @@ class EventLoopTestsMixin: else: break else: - assert False, 'Can not create socket.' + self.fail('Can not create socket.') f = self.loop.create_datagram_endpoint( lambda: MyDatagramProto(loop=self.loop), sock=sock) diff --git a/Lib/test/test_asyncio/test_futures.py b/Lib/test/test_asyncio/test_futures.py index 42b9499..0c379e0 100644 --- a/Lib/test/test_asyncio/test_futures.py +++ b/Lib/test/test_asyncio/test_futures.py @@ -54,30 +54,30 @@ class DuckFuture: or self.__exception is not None) def result(self): - assert not self.cancelled() + self.assertFalse(self.cancelled()) if self.__exception is not None: raise self.__exception return self.__result def exception(self): - assert not self.cancelled() + self.assertFalse(self.cancelled()) return self.__exception def set_result(self, result): - assert not self.done() - assert result is not None + self.assertFalse(self.done()) + self.assertIsNotNone(result) self.__result = result def set_exception(self, exception): - assert not self.done() - assert exception is not None + self.assertFalse(self.done()) + self.assertIsNotNone(exception) self.__exception = exception def __iter__(self): if not self.done(): self._asyncio_future_blocking = True yield self - assert self.done() + self.assertTrue(self.done()) return self.result() @@ -91,12 +91,12 @@ class DuckTests(test_utils.TestCase): def test_wrap_future(self): f = DuckFuture() g = asyncio.wrap_future(f) - assert g is f + self.assertIs(g, f) def test_ensure_future(self): f = DuckFuture() g = asyncio.ensure_future(f) - assert g is f + self.assertIs(g, f) class BaseFutureTests: diff --git a/Lib/test/test_asyncio/test_sendfile.py b/Lib/test/test_asyncio/test_sendfile.py index 01c6986..0a5466a 100644 --- a/Lib/test/test_asyncio/test_sendfile.py +++ b/Lib/test/test_asyncio/test_sendfile.py @@ -36,25 +36,29 @@ class MySendfileProto(asyncio.Protocol): self.data = bytearray() self.close_after = close_after + def _assert_state(self, *expected): + if self.state not in expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'CONNECTED' if self.connected: self.connected.set_result(None) def eof_received(self): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.state = 'EOF' def connection_lost(self, exc): - assert self.state in ('CONNECTED', 'EOF'), self.state + self._assert_state('CONNECTED', 'EOF') self.state = 'CLOSED' if self.done: self.done.set_result(None) def data_received(self, data): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.nbytes += len(data) self.data.extend(data) super().data_received(data) diff --git a/Lib/test/test_asyncio/test_sock_lowlevel.py b/Lib/test/test_asyncio/test_sock_lowlevel.py index d8a5df8..ab022a3 100644 --- a/Lib/test/test_asyncio/test_sock_lowlevel.py +++ b/Lib/test/test_asyncio/test_sock_lowlevel.py @@ -23,24 +23,28 @@ class MyProto(asyncio.Protocol): self.connected = loop.create_future() self.done = loop.create_future() + def _assert_state(self, *expected): + if self.state not in expected: + raise AssertionError(f'state: {self.state!r}, expected: {expected!r}') + def connection_made(self, transport): self.transport = transport - assert self.state == 'INITIAL', self.state + self._assert_state('INITIAL') self.state = 'CONNECTED' if self.connected: self.connected.set_result(None) transport.write(b'GET / HTTP/1.0\r\nHost: example.com\r\n\r\n') def data_received(self, data): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.nbytes += len(data) def eof_received(self): - assert self.state == 'CONNECTED', self.state + self._assert_state('CONNECTED') self.state = 'EOF' def connection_lost(self, exc): - assert self.state in ('CONNECTED', 'EOF'), self.state + self._assert_state('CONNECTED', 'EOF') self.state = 'CLOSED' if self.done: self.done.set_result(None) @@ -451,7 +455,7 @@ class BaseSockTestsMixin: else: break else: - assert False, 'Can not create socket.' + self.fail('Can not create socket.') f = self.loop.create_connection( lambda: MyProto(loop=self.loop), sock=sock) diff --git a/Lib/test/test_asyncio/test_sslproto.py b/Lib/test/test_asyncio/test_sslproto.py index a47e43d..22a216a 100644 --- a/Lib/test/test_asyncio/test_sslproto.py +++ b/Lib/test/test_asyncio/test_sslproto.py @@ -383,9 +383,9 @@ class BaseStartTLS(func_tests.FunctionalTestCaseMixin): def get_buffer(self, sizehint): return self.buf - def buffer_updated(self, nsize): - assert nsize == 1 - self.on_data.set_result(bytes(self.buf[:nsize])) + def buffer_updated(slf, nsize): + self.assertEqual(nsize, 1) + slf.on_data.set_result(bytes(slf.buf[:nsize])) class ClientProtoSecond(asyncio.Protocol): def __init__(self, on_data, on_eof): diff --git a/Lib/test/test_asyncio/test_streams.py b/Lib/test/test_asyncio/test_streams.py index a6ea24c..227b227 100644 --- a/Lib/test/test_asyncio/test_streams.py +++ b/Lib/test/test_asyncio/test_streams.py @@ -987,10 +987,10 @@ os.close(fd) wr.close() f = wr.wait_closed() self.loop.run_until_complete(f) - assert rd.at_eof() + self.assertTrue(rd.at_eof()) f = rd.read() data = self.loop.run_until_complete(f) - assert data == b'' + self.assertEqual(data, b'') self.assertEqual(messages, []) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index a88cb89..832ff80 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -278,7 +278,7 @@ class BaseTaskTests: self.set_event_loop(loop) fut = asyncio.ensure_future(Aw(coro()), loop=loop) loop.run_until_complete(fut) - assert fut.result() == 'ok' + self.assertEqual(fut.result(), 'ok') def test_ensure_future_neither(self): with self.assertRaises(TypeError): @@ -1016,7 +1016,7 @@ class BaseTaskTests: async def main(): result = await asyncio.wait_for(inner(), timeout=.01) - assert result == 1 + self.assertEqual(result, 1) asyncio.run(main()) diff --git a/Lib/test/test_asyncio/test_unix_events.py b/Lib/test/test_asyncio/test_unix_events.py index 6436385..c342285 100644 --- a/Lib/test/test_asyncio/test_unix_events.py +++ b/Lib/test/test_asyncio/test_unix_events.py @@ -34,7 +34,8 @@ def EXITCODE(exitcode): def SIGNAL(signum): - assert 1 <= signum <= 68 + if not 1 <= signum <= 68: + raise AssertionError(f'invalid signum {signum}') return 32768 - signum diff --git a/Lib/test/test_asyncio/utils.py b/Lib/test/test_asyncio/utils.py index 3765194..0b9cde6 100644 --- a/Lib/test/test_asyncio/utils.py +++ b/Lib/test/test_asyncio/utils.py @@ -409,12 +409,13 @@ class TestLoop(base_events.BaseEventLoop): return False def assert_writer(self, fd, callback, *args): - assert fd in self.writers, 'fd {} is not registered'.format(fd) + if fd not in self.writers: + raise AssertionError(f'fd {fd} is not registered') handle = self.writers[fd] - assert handle._callback == callback, '{!r} != {!r}'.format( - handle._callback, callback) - assert handle._args == args, '{!r} != {!r}'.format( - handle._args, args) + if handle._callback != callback: + raise AssertionError(f'{handle._callback!r} != {callback!r}') + if handle._args != args: + raise AssertionError(f'{handle._args!r} != {args!r}') def _ensure_fd_no_transport(self, fd): if not isinstance(fd, int): @@ -530,7 +531,8 @@ class TestCase(unittest.TestCase): thread.join() def set_event_loop(self, loop, *, cleanup=True): - assert loop is not None + if loop is None: + raise AssertionError('loop is None') # ensure that the event loop is passed explicitly in asyncio events.set_event_loop(None) if cleanup: |