-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise exception in tests when blocking code is called in event loop
- Loading branch information
Showing
3 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import asyncio | ||
import os | ||
import ssl | ||
import time | ||
from socket import socket as _socket | ||
|
||
|
||
class BlockingError(Exception): ... | ||
|
||
|
||
def _raise_if_blocking(func): | ||
def wrapper(*args, **kwargs): | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
return func(*args, **kwargs) | ||
msg = f"Blocking call to {func.__module__}.{func.__qualname__}" | ||
raise BlockingError(msg) | ||
|
||
return wrapper | ||
|
||
|
||
def _wrap_os_blocking(func): | ||
def os_op(fd, *args, **kwargs): | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
return func(fd, *args, **kwargs) | ||
if os.get_blocking(fd): | ||
msg = f"Blocking call to {func.__module__}.{func.__qualname__}" | ||
raise BlockingError(msg) | ||
return func(fd, *args, **kwargs) | ||
|
||
return os_op | ||
|
||
|
||
def _wrap_socket_blocking(func): | ||
def socket_op(self, *args, **kwargs): | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
return func(self, *args, **kwargs) | ||
if self.getblocking(): | ||
msg = f"Blocking call to {func.__module__}.{func.__qualname__}" | ||
raise BlockingError(msg) | ||
return func(self, *args, **kwargs) | ||
|
||
return socket_op | ||
|
||
|
||
def init(): | ||
time.sleep = _raise_if_blocking(time.sleep) | ||
|
||
os.read = _wrap_os_blocking(os.read) | ||
os.write = _wrap_os_blocking(os.write) | ||
|
||
_socket.send = _wrap_socket_blocking(_socket.send) | ||
_socket.sendall = _wrap_socket_blocking(_socket.sendall) | ||
_socket.sendto = _wrap_socket_blocking(_socket.sendto) | ||
_socket.recv = _wrap_socket_blocking(_socket.recv) | ||
_socket.recv_into = _wrap_socket_blocking(_socket.recv_into) | ||
_socket.recvfrom = _wrap_socket_blocking(_socket.recvfrom) | ||
_socket.recvfrom_into = _wrap_socket_blocking(_socket.recvfrom_into) | ||
_socket.recvmsg = _wrap_socket_blocking(_socket.recvmsg) | ||
_socket.recvmsg_into = _wrap_socket_blocking(_socket.recvmsg_into) | ||
|
||
ssl.SSLSocket.write = _wrap_socket_blocking(ssl.SSLSocket.write) | ||
ssl.SSLSocket.send = _wrap_socket_blocking(ssl.SSLSocket.send) | ||
ssl.SSLSocket.read = _wrap_socket_blocking(ssl.SSLSocket.read) | ||
ssl.SSLSocket.recv = _wrap_socket_blocking(ssl.SSLSocket.recv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters