diff options
| author | Victor Stinner <victor.stinner@gmail.com> | 2014-07-30 15:31:53 +0200 |
|---|---|---|
| committer | Victor Stinner <victor.stinner@gmail.com> | 2014-07-30 15:31:53 +0200 |
| commit | 65e86f2eba816d72b67083e21a79ff1d6a6e4925 (patch) | |
| tree | 961668a2fb92ba4c669219ef392b170663f37ed1 | |
| parent | dfcf393bd957a60106339d02cd9a2786192f7993 (diff) | |
| download | trollius-git-65e86f2eba816d72b67083e21a79ff1d6a6e4925.tar.gz | |
Fix unit tests in debug mode: mock a non-blocking socket for socket operations
which now raise an exception if the socket is blocking.
| -rw-r--r-- | asyncio/test_utils.py | 6 | ||||
| -rw-r--r-- | tests/test_events.py | 2 | ||||
| -rw-r--r-- | tests/test_selector_events.py | 10 |
3 files changed, 13 insertions, 5 deletions
diff --git a/asyncio/test_utils.py b/asyncio/test_utils.py index 840bbf9..ac7680d 100644 --- a/asyncio/test_utils.py +++ b/asyncio/test_utils.py @@ -417,3 +417,9 @@ def disable_logger(): yield finally: logger.setLevel(old_level) + +def mock_nonblocking_socket(): + """Create a mock of a non-blocking socket.""" + sock = mock.Mock(socket.socket) + sock.gettimeout.return_value = 0.0 + return sock diff --git a/tests/test_events.py b/tests/test_events.py index 70ba3ad..0cfc028 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -1231,6 +1231,7 @@ class EventLoopTestsMixin: "Don't support pipes for Windows") def test_write_pipe_disconnect_on_close(self): rsock, wsock = test_utils.socketpair() + rsock.setblocking(False) pipeobj = io.open(wsock.detach(), 'wb', 1024) proto = MyWritePipeProto(loop=self.loop) @@ -1368,6 +1369,7 @@ class EventLoopTestsMixin: for sock_type in (socket.SOCK_STREAM, socket.SOCK_DGRAM): sock = socket.socket(family, sock_type) with sock: + sock.setblocking(False) connect = self.loop.sock_connect(sock, address) with self.assertRaises(ValueError) as cm: self.loop.run_until_complete(connect) diff --git a/tests/test_selector_events.py b/tests/test_selector_events.py index bd6c2f2..5fee411 100644 --- a/tests/test_selector_events.py +++ b/tests/test_selector_events.py @@ -135,7 +135,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): self.assertRaises(RuntimeError, self.loop._write_to_self) def test_sock_recv(self): - sock = mock.Mock() + sock = test_utils.mock_nonblocking_socket() self.loop._sock_recv = mock.Mock() f = self.loop.sock_recv(sock, 1024) @@ -183,7 +183,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): self.assertIs(err, f.exception()) def test_sock_sendall(self): - sock = mock.Mock() + sock = test_utils.mock_nonblocking_socket() self.loop._sock_sendall = mock.Mock() f = self.loop.sock_sendall(sock, b'data') @@ -193,7 +193,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): self.loop._sock_sendall.call_args[0]) def test_sock_sendall_nodata(self): - sock = mock.Mock() + sock = test_utils.mock_nonblocking_socket() self.loop._sock_sendall = mock.Mock() f = self.loop.sock_sendall(sock, b'') @@ -295,7 +295,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): self.loop.add_writer.call_args[0]) def test_sock_connect(self): - sock = mock.Mock() + sock = test_utils.mock_nonblocking_socket() self.loop._sock_connect = mock.Mock() f = self.loop.sock_connect(sock, ('127.0.0.1', 8080)) @@ -361,7 +361,7 @@ class BaseSelectorEventLoopTests(test_utils.TestCase): self.assertIsInstance(f.exception(), OSError) def test_sock_accept(self): - sock = mock.Mock() + sock = test_utils.mock_nonblocking_socket() self.loop._sock_accept = mock.Mock() f = self.loop.sock_accept(sock) |
