Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_auto_reconnect(app, watcher, manager):
# add client
client = App().register(manager)
node = Node().register(client)
chan = node.add('client1', *app.bind, reconnect_delay=1, connect_timeout=1)
assert watcher.wait('connected', channel=chan)
watcher.clear()
# close server
app.fire(close(), app.channel)
assert watcher.wait('closed', channel=app.channel)
watcher.clear()
# client gets an unreachable
assert watcher.wait('connect', channel=chan)
assert watcher.wait('unreachable', channel=chan)
watcher.clear()
# start a new server
node2 = Node(port=app.bind[1], server_ip=app.bind[0])
node2.register(manager)
assert watcher.wait('ready', channel=node2.channel)
watcher.clear()
assert watcher.wait('connected', channel=chan)
def bind(manager, watcher):
server = UDPServer(0).register(manager)
assert watcher.wait('ready', channel='server')
host, port = server.host, server.port
server.fire(close())
assert watcher.wait('closed', channel='server')
server.unregister()
assert watcher.wait('unregistered', channel='server')
return host, port
try:
assert pytest.wait_for(client, "ready")
assert pytest.wait_for(server, "ready")
wait_host(server)
# 1st connect
client.fire(connect(server.host, server.port))
assert pytest.wait_for(client, "connected")
assert pytest.wait_for(server, "connected")
assert pytest.wait_for(client, "data", b"Ready")
client.fire(write(b"foo"))
assert pytest.wait_for(server, "data", b"foo")
# disconnect
client.fire(close())
assert pytest.wait_for(client, "disconnected")
# 2nd reconnect
client.fire(connect(server.host, server.port))
assert pytest.wait_for(client, "connected")
assert pytest.wait_for(server, "connected")
assert pytest.wait_for(client, "data", b"Ready")
client.fire(write(b"foo"))
assert pytest.wait_for(server, "data", b"foo")
client.fire(close())
assert pytest.wait_for(client, "disconnected")
assert pytest.wait_for(server, "disconnected")
server.fire(close())
def quit(self, sock, source, reason="Leaving"):
user = self.users[sock]
channels = [self.channels[channel] for channel in user.channels]
for channel in channels:
channel.users.remove(user)
if not channel.users:
del self.channels[channel.name]
users = chain(*map(attrgetter("users"), channels))
self.fire(close(sock))
self._notify(
users,
Message("QUIT", reason, prefix=user.prefix), user
)
def _on_close(self, *args):
if self._sock is not None:
if args and (args[0] != self._sock):
return
if not self._close_sent:
self._write(b"\x88\x00")
self._close_sent = True
if self._close_received and self._close_sent:
if self._sock:
self.fire(close(self._sock), self.parent.channel)
else:
self.fire(close(), self.parent.channel)
def _on_target_disconnected(self, event):
"""Disconnected Event Handler
This unbound function will be later added as an event handler to a
dynamically created and registered client instance and used to process
Disconnected events of a connected client.
"""
channel = event.channels[0]
sock = self._sockets[channel]
self.fire(close(sock), "source")
del self._sockets[channel]
del self._clients[sock]
if final:
if opcode < 8:
# if text or continuation of text, convert
if opcode == 1 \
or opcode == 0 and self._pending_type == 1:
msg = msg.decode("utf-8", "replace")
self._pending_type = None
self._pending_payload = bytearray()
msgs.append(msg)
# check for client closing the connection
elif opcode == 8:
self._close_received = True
if self._sock:
self.fire(close(self._sock))
else:
self.fire(close())
break
# check for Ping
elif opcode == 9:
if self._close_sent:
return
frame = bytearray(b'\x8a')
frame += self._encode_tail(msg, self._sock is None)
self._write(frame)
else:
self._pending_payload = msg
if opcode != 0:
self._pending_type = opcode
return msgs
def __init__(self, *args):
"x.__init__(...) initializes x; see x.__class__.__doc__ for signature"
super(close, self).__init__(*args)
if sock in self._buffers:
parser = self._buffers[sock]
else:
self._buffers[sock] = parser = HttpParser(0, True)
# If we receive an SSL handshake at the start of a request
# and we're not a secure server, then immediately close the
# client connection since we can't respond to it anyway.
if is_ssl_handshake(data) and not self._server.secure:
if sock in self._buffers:
del self._buffers[sock]
if sock in self._clients:
del self._clients[sock]
return self.fire(close(sock))
_scheme = "https" if self._server.secure else "http"
parser.execute(data, len(data))
if not parser.is_headers_complete():
if parser.errno is not None:
if parser.errno == BAD_FIRST_LINE:
req = wrappers.Request(sock, server=self._server)
else:
req = wrappers.Request(
sock,
parser.get_method(),
parser.get_scheme() or _scheme,
parser.get_path(),
parser.get_version(),
parser.get_query_string(),
server=self._server
# if there have been parts already, combine
msg = self._pending_payload + msg
if final:
if opcode < 8:
# if text or continuation of text, convert
if opcode == 1 \
or opcode == 0 and self._pending_type == 1:
msg = msg.decode("utf-8", "replace")
self._pending_type = None
self._pending_payload = bytearray()
msgs.append(msg)
# check for client closing the connection
elif opcode == 8:
self._close_received = True
if self._sock:
self.fire(close(self._sock))
else:
self.fire(close())
break
# check for Ping
elif opcode == 9:
if self._close_sent:
return
frame = bytearray(b'\x8a')
frame += self._encode_tail(msg, self._sock is None)
self._write(frame)
else:
self._pending_payload = msg
if opcode != 0:
self._pending_type = opcode
return msgs