Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def run_client():
# Use connect as an asynchronous context manager.
async with connect(get_server_uri(server)) as client:
self.assertEqual(client.state, State.OPEN)
# Check that exiting the context manager closed the connection.
self.assertEqual(client.state, State.CLOSED)
def setup(self):
self.blinkboard = BlinkBoardWidget()
self.connected_sockets = {}
self.connected_sockets[self.generate_identifier()] = None
for state in [State.OPEN, State.CLOSING, State.CLOSED]:
socket = mock.Mock()
socket.ws.state = state
socket.last_message_recv = time.time()
socket.id = self.generate_identifier()
self.connected_sockets[socket.id] = socket
async def run_client():
# Use connect as an asynchronous context manager.
async with connect(get_server_uri(server)) as client:
self.assertEqual(client.state, State.OPEN)
# Check that exiting the context manager closed the connection.
self.assertEqual(client.state, State.CLOSED)
"""
# On the server side, websockets completes the closing handshake and
# closes the TCP connection immediately. Yield to the event loop after
# sending the close frame to run the test while the connection is in
# the CLOSING state.
if not self.protocol.is_client:
self.make_drain_slow()
close_frame_data = serialize_close(code, reason)
# Trigger the closing handshake from the remote endpoint.
self.receive_frame(Frame(True, OP_CLOSE, close_frame_data))
self.run_loop_once() # read_frame executes
# Empty the outgoing data stream so we can make assertions later on.
self.assertOneFrameSent(True, OP_CLOSE, close_frame_data)
assert self.protocol.state is State.CLOSING
# Complete the closing sequence at 1ms intervals so the test can run
# at each point even it goes back to the event loop several times.
self.loop.call_later(2 * MS, self.receive_eof_if_client)
async def run_client():
# Await connect.
client = await connect(get_server_uri(server))
self.assertEqual(client.state, State.OPEN)
await client.close()
self.assertEqual(client.state, State.CLOSED)
async def failme(request, ws):
raise NotImplementedError('OUCH')
websocket = await websockets.connect(liveclient.wsl + '/failure')
# Sent within the closing frame span messages will be ignored but
# won't create any error as the server is polite and awaits the
# closing frame to ends the interaction in a friendly manner.
await websocket.send('first')
# The server is on hold, waiting for the closing handshake
# We provide it to be civilized.
await websocket.close()
# The websocket was closed with the error, but in a gentle way.
# No exception raised.
assert websocket.state == websockets.protocol.State.CLOSED
assert websocket.close_code == 1011
assert websocket.close_reason == 'Handler died prematurely.'
def run_client():
# Yield from connect.
client = yield from connect(get_server_uri(server))
self.assertEqual(client.state, State.OPEN)
yield from client.close()
self.assertEqual(client.state, State.CLOSED)
websocket = await websockets.connect(liveclient.wsl + '/failure')
monkeypatch.setattr('roll.WSProtocol.TIMEOUT', 1)
with pytest.raises(websockets.exceptions.ConnectionClosed):
# The client has 1 second timeout before the
# closing handshake timeout and the brutal disconnection.
# We wait beyond the closing frame timeout :
await asyncio.sleep(2)
# No, we try sending while the closing frame timespan has expired
await websocket.send('first')
# No need to close here, the closing was unilateral, as we
# did not comply in time.
# We check the remains of the disowned client :
assert websocket.state == websockets.protocol.State.CLOSED
assert websocket.close_code == 1011
assert websocket.close_reason == 'Handler died prematurely.'
async def management_loop(self):
while True:
try:
if self.stop:
return
if self.time and (time.time() - self.time) > BinanceWebsocket.REFRESH_KEY_TIMEOUT:
self.start_user_info()
if self.user_webscoket:
if self.user_webscoket.state == State.OPEN:
self.user_webscoket.ping()
elif self.user_webscoket.state == State.CLOSED:
self.start_user_info(force_reconnect=True)
if self.ticker_websocket and self.ticker_websocket.state == State.CLOSED:
self.start_ticker()
await asyncio.sleep(60)
except:
self.logError(traceback.format_exc())