| 1 |
import asyncio |
| 2 |
import contextlib |
| 3 |
|
| 4 |
from websockets.asyncio.connection import Connection |
| 5 |
|
| 6 |
|
| 7 |
class InterceptingConnection(Connection): |
| 8 |
""" |
| 9 |
Connection subclass that can intercept outgoing packets. |
| 10 |
|
| 11 |
By interfacing with this connection, we simulate network conditions |
| 12 |
affecting what the component being tested receives during a test. |
| 13 |
|
| 14 |
""" |
| 15 |
|
| 16 |
def connection_made(self, transport): |
| 17 |
super().connection_made(InterceptingTransport(transport)) |
| 18 |
|
| 19 |
@contextlib.contextmanager |
| 20 |
def delay_frames_sent(self, delay): |
| 21 |
""" |
| 22 |
Add a delay before sending frames. |
| 23 |
|
| 24 |
Misuse can result in out-of-order writes, which is unrealistic. |
| 25 |
|
| 26 |
""" |
| 27 |
assert self.transport.delay_write is None |
| 28 |
self.transport.delay_write = delay |
| 29 |
try: |
| 30 |
yield |
| 31 |
finally: |
| 32 |
self.transport.delay_write = None |
| 33 |
|
| 34 |
@contextlib.contextmanager |
| 35 |
def delay_eof_sent(self, delay): |
| 36 |
""" |
| 37 |
Add a delay before sending EOF. |
| 38 |
|
| 39 |
Misuse can result in out-of-order writes, which is unrealistic. |
| 40 |
|
| 41 |
""" |
| 42 |
assert self.transport.delay_write_eof is None |
| 43 |
self.transport.delay_write_eof = delay |
| 44 |
try: |
| 45 |
yield |
| 46 |
finally: |
| 47 |
self.transport.delay_write_eof = None |
| 48 |
|
| 49 |
@contextlib.contextmanager |
| 50 |
def drop_frames_sent(self): |
| 51 |
""" |
| 52 |
Prevent frames from being sent. |
| 53 |
|
| 54 |
Since TCP is reliable, sending frames or EOF afterwards is unrealistic. |
| 55 |
|
| 56 |
""" |
| 57 |
assert not self.transport.drop_write |
| 58 |
self.transport.drop_write = True |
| 59 |
try: |
| 60 |
yield |
| 61 |
finally: |
| 62 |
self.transport.drop_write = False |
| 63 |
|
| 64 |
@contextlib.contextmanager |
| 65 |
def drop_eof_sent(self): |
| 66 |
""" |
| 67 |
Prevent EOF from being sent. |
| 68 |
|
| 69 |
Since TCP is reliable, sending frames or EOF afterwards is unrealistic. |
| 70 |
|
| 71 |
""" |
| 72 |
assert not self.transport.drop_write_eof |
| 73 |
self.transport.drop_write_eof = True |
| 74 |
try: |
| 75 |
yield |
| 76 |
finally: |
| 77 |
self.transport.drop_write_eof = False |
| 78 |
|
| 79 |
|
| 80 |
class InterceptingTransport: |
| 81 |
""" |
| 82 |
Transport wrapper that intercepts calls to ``write()`` and ``write_eof()``. |
| 83 |
|
| 84 |
This is coupled to the implementation, which relies on these two methods. |
| 85 |
|
| 86 |
Since ``write()`` and ``write_eof()`` are synchronous, we can only schedule |
| 87 |
writes at a later time, after they return. This is unrealistic and can lead |
| 88 |
to out-of-order writes if tests aren't written carefully. |
| 89 |
|
| 90 |
""" |
| 91 |
|
| 92 |
def __init__(self, transport): |
| 93 |
self.loop = asyncio.get_running_loop() |
| 94 |
self.transport = transport |
| 95 |
self.delay_write = None |
| 96 |
self.delay_write_eof = None |
| 97 |
self.drop_write = False |
| 98 |
self.drop_write_eof = False |
| 99 |
|
| 100 |
def __getattr__(self, name): |
| 101 |
return getattr(self.transport, name) |
| 102 |
|
| 103 |
def write(self, data): |
| 104 |
if self.delay_write is not None: |
| 105 |
assert not self.drop_write |
| 106 |
self.loop.call_later(self.delay_write, self.transport.write, data) |
| 107 |
elif not self.drop_write: |
| 108 |
self.transport.write(data) |
| 109 |
|
| 110 |
def write_eof(self): |
| 111 |
if self.delay_write_eof is not None: |
| 112 |
assert not self.drop_write_eof |
| 113 |
self.loop.call_later(self.delay_write_eof, self.transport.write_eof) |
| 114 |
elif not self.drop_write_eof: |
| 115 |
self.transport.write_eof() |