| 1 |
import contextlib |
| 2 |
|
| 3 |
import trio |
| 4 |
|
| 5 |
from websockets.trio.connection import Connection |
| 6 |
|
| 7 |
|
| 8 |
class InterceptingConnection(Connection): |
| 9 |
""" |
| 10 |
Connection subclass that can intercept outgoing packets. |
| 11 |
|
| 12 |
By interfacing with this connection, we simulate network conditions |
| 13 |
affecting what the component being tested receives during a test. |
| 14 |
|
| 15 |
""" |
| 16 |
|
| 17 |
def __init__(self, *args, **kwargs): |
| 18 |
super().__init__(*args, **kwargs) |
| 19 |
self.stream = InterceptingStream(self.stream) |
| 20 |
|
| 21 |
@contextlib.contextmanager |
| 22 |
def delay_frames_sent(self, delay): |
| 23 |
""" |
| 24 |
Add a delay before sending frames. |
| 25 |
|
| 26 |
Delays cumulate: they're added before every frame or before EOF. |
| 27 |
|
| 28 |
""" |
| 29 |
assert self.stream.delay_send_all is None |
| 30 |
self.stream.delay_send_all = delay |
| 31 |
try: |
| 32 |
yield |
| 33 |
finally: |
| 34 |
self.stream.delay_send_all = None |
| 35 |
|
| 36 |
@contextlib.contextmanager |
| 37 |
def delay_eof_sent(self, delay): |
| 38 |
""" |
| 39 |
Add a delay before sending EOF. |
| 40 |
|
| 41 |
Delays cumulate: they're added before every frame or before EOF. |
| 42 |
|
| 43 |
""" |
| 44 |
assert self.stream.delay_send_eof is None |
| 45 |
self.stream.delay_send_eof = delay |
| 46 |
try: |
| 47 |
yield |
| 48 |
finally: |
| 49 |
self.stream.delay_send_eof = None |
| 50 |
|
| 51 |
@contextlib.contextmanager |
| 52 |
def drop_frames_sent(self): |
| 53 |
""" |
| 54 |
Prevent frames from being sent. |
| 55 |
|
| 56 |
Since TCP is reliable, sending frames or EOF afterwards is unrealistic. |
| 57 |
|
| 58 |
""" |
| 59 |
assert not self.stream.drop_send_all |
| 60 |
self.stream.drop_send_all = True |
| 61 |
try: |
| 62 |
yield |
| 63 |
finally: |
| 64 |
self.stream.drop_send_all = False |
| 65 |
|
| 66 |
@contextlib.contextmanager |
| 67 |
def drop_eof_sent(self): |
| 68 |
""" |
| 69 |
Prevent EOF from being sent. |
| 70 |
|
| 71 |
Since TCP is reliable, sending frames or EOF afterwards is unrealistic. |
| 72 |
|
| 73 |
""" |
| 74 |
assert not self.stream.drop_send_eof |
| 75 |
self.stream.drop_send_eof = True |
| 76 |
try: |
| 77 |
yield |
| 78 |
finally: |
| 79 |
self.stream.drop_send_eof = False |
| 80 |
|
| 81 |
|
| 82 |
class InterceptingStream: |
| 83 |
""" |
| 84 |
Stream wrapper that intercepts calls to ``send_all()`` and ``send_eof()``. |
| 85 |
|
| 86 |
This is coupled to the implementation, which relies on these two methods. |
| 87 |
|
| 88 |
""" |
| 89 |
|
| 90 |
# We cannot delay EOF with trio's virtual streams because close_hook is |
| 91 |
# synchronous. We adopt the same approach as the other implementations. |
| 92 |
|
| 93 |
def __init__(self, stream): |
| 94 |
self.stream = stream |
| 95 |
self.delay_send_all = None |
| 96 |
self.delay_send_eof = None |
| 97 |
self.drop_send_all = False |
| 98 |
self.drop_send_eof = False |
| 99 |
|
| 100 |
def __getattr__(self, name): |
| 101 |
return getattr(self.stream, name) |
| 102 |
|
| 103 |
async def send_all(self, data): |
| 104 |
if self.delay_send_all is not None: |
| 105 |
await trio.sleep(self.delay_send_all) |
| 106 |
if not self.drop_send_all: |
| 107 |
await self.stream.send_all(data) |
| 108 |
|
| 109 |
async def send_eof(self): |
| 110 |
if self.delay_send_eof is not None: |
| 111 |
await trio.sleep(self.delay_send_eof) |
| 112 |
if not self.drop_send_eof: |
| 113 |
await self.stream.send_eof() |
| 114 |
|
| 115 |
|
| 116 |
trio.abc.HalfCloseableStream.register(InterceptingStream) |