Mixin that defines most tests but doesn't inherit unittest.TestCase. Tests are run by the ServerTests and ClientTests subclasses.
| 81 | |
| 82 | |
| 83 | class CommonTests: |
| 84 | """ |
| 85 | Mixin that defines most tests but doesn't inherit unittest.TestCase. |
| 86 | |
| 87 | Tests are run by the ServerTests and ClientTests subclasses. |
| 88 | |
| 89 | """ |
| 90 | |
| 91 | def setUp(self): |
| 92 | super().setUp() |
| 93 | |
| 94 | # This logic is encapsulated in a coroutine to prevent it from executing |
| 95 | # before the event loop is running which causes asyncio.get_event_loop() |
| 96 | # to raise a DeprecationWarning on Python ≥ 3.10. |
| 97 | |
| 98 | async def create_protocol(): |
| 99 | # Disable pings to make it easier to test what frames are sent exactly. |
| 100 | return WebSocketCommonProtocol(ping_interval=None) |
| 101 | |
| 102 | self.protocol = self.loop.run_until_complete(create_protocol()) |
| 103 | self.transport = TransportMock() |
| 104 | self.transport.setup_mock(self.loop, self.protocol) |
| 105 | |
| 106 | def tearDown(self): |
| 107 | self.transport.close() |
| 108 | self.loop.run_until_complete(self.protocol.close()) |
| 109 | super().tearDown() |
| 110 | |
| 111 | # Utilities for writing tests. |
| 112 | |
| 113 | def make_drain_slow(self, delay=MS): |
| 114 | # Process connection_made in order to initialize self.protocol.transport. |
| 115 | self.run_loop_once() |
| 116 | |
| 117 | original_drain = self.protocol._drain |
| 118 | |
| 119 | async def delayed_drain(): |
| 120 | await asyncio.sleep(delay) |
| 121 | await original_drain() |
| 122 | |
| 123 | self.protocol._drain = delayed_drain |
| 124 | |
| 125 | close_frame = Frame( |
| 126 | True, |
| 127 | OP_CLOSE, |
| 128 | Close(CloseCode.NORMAL_CLOSURE, "close").serialize(), |
| 129 | ) |
| 130 | local_close = Frame( |
| 131 | True, |
| 132 | OP_CLOSE, |
| 133 | Close(CloseCode.NORMAL_CLOSURE, "local").serialize(), |
| 134 | ) |
| 135 | remote_close = Frame( |
| 136 | True, |
| 137 | OP_CLOSE, |
| 138 | Close(CloseCode.NORMAL_CLOSURE, "remote").serialize(), |
| 139 | ) |
| 140 |