def test_respond_to_ping_with_pong(config_path, router): # This test shows proper handling of ping/pong keep-alives # by connecting to a pong-demanding server (crossbar.timeout.json) # and keeping the connection open for longer than the server's timeout. # Failure would be an exception being thrown because of the server # closing the connection. class MyClient(Client): pass exceptionless = True try: client = MyClient(url=router.url) client.start() wait_for_session(client) async_adapter.sleep(5) # this is purely to demonstrate we can make calls while sending # pongs client.publish(topic="test", message="test") client.stop() except Exception as e: print(e) exceptionless = False assert exceptionless
def wait_for_registrations(client, number_of_registrations): with async_adapter.Timeout(TIMEOUT): while ( len(client.session.registration_map.keys()) < number_of_registrations ): async_adapter.sleep(0.01)
def wait_for_messages(client, number_of_messages): messages_received = (client.session.message_handler.messages_received) with async_adapter.Timeout(TIMEOUT): while len(messages_received) < number_of_messages: async_adapter.sleep(0.01) return messages_received
def wait_for_messages(client, number_of_messages): messages_received = ( client.session.message_handler.messages_received) with async_adapter.Timeout(TIMEOUT): while len(messages_received) < number_of_messages: async_adapter.sleep(0.01) return messages_received
def assert_stops_raising( fn, exception_type=Exception, timeout=5, interval=0.1): with async_adapter.Timeout(timeout): while True: try: fn() except exception_type: pass else: return async_adapter.sleep(interval)
def send_ping_and_expect_pong(): payload = 'wampy::' + str(uuid.uuid4()) # we send a Ping with a unique payload, and we expect # a Pong back echoing the same payload - but within the # deadline of ``heartbeat_timeout_seconds``. ping = Ping(payload=payload, mask_payload=True) try: socket.sendall(bytes(ping.frame)) except OSError: # connection closed by parent thread, or wampy # has been disconnected from server... # either way, this gthread will be killed as # soon if the Close message is received else # schedule another Ping logger.info('ping failed') pass except BrokenPipeError: logger.info('server ripped out from under us!') pass pong = None with async_adapter.Timeout( heartbeat_timeout, raise_after=False ): while pong is None: # required for Hub to implelent TimeOut async_adapter.sleep() try: maybe_my_pong = self.pongs.get(block=False) except async_adapter.QueueEmpty: continue if maybe_my_pong: if maybe_my_pong.payload == payload: pong = maybe_my_pong else: # possibly Pinging faster than the server is # Ponging, else Hub hasn't scheduled the right # gthread yet. logger.error('Pongs out of order?') self.pongs.put(maybe_my_pong) if pong is None: logger.info('missed a Pong from the server') self.missed_pongs += 1
def send_ping_and_expect_pong(): payload = 'wampy::' + str(uuid.uuid4()) # we send a Ping with a unique payload, and we expect # a Pong back echoing the same payload - but within the # deadline of ``heartbeat_timeout_seconds``. ping = Ping(payload=payload, mask_payload=True) try: socket.sendall(bytes(ping.frame)) except OSError: # connection closed by parent thread, or wampy # has been disconnected from server... # either way, this gthread will be killed as # soon if the Close message is received else # schedule another Ping logger.info('ping failed') pass except BrokenPipeError: logger.info('server ripped out from under us!') pass pong = None with async_adapter.Timeout(heartbeat_timeout, raise_after=False): while pong is None: # required for Hub to implelent TimeOut async_adapter.sleep() try: maybe_my_pong = self.pongs.get(block=False) except async_adapter.QueueEmpty: continue if maybe_my_pong: if maybe_my_pong.payload == payload: pong = maybe_my_pong else: # possibly Pinging faster than the server is # Ponging, else Hub hasn't scheduled the right # gthread yet. logger.error('Pongs out of order?') self.pongs.put(maybe_my_pong) if pong is None: logger.info('missed a Pong from the server') self.missed_pongs += 1
def test_pinging(router): with patch('wampy.transports.websocket.connection.heartbeat', 1): with patch('wampy.transports.websocket.connection.heartbeat_timeout', 2): client = Client(router.url) client.start() wait_for_session(client) assert client.is_pinging ws = client.session.connection assert ws.missed_pongs == 0 async_adapter.sleep(10) assert ws.missed_pongs == 0 client.stop()
def test_pinging(router): with patch('wampy.transports.websocket.connection.heartbeat', 1): with patch( 'wampy.transports.websocket.connection.heartbeat_timeout', 2 ): client = Client(router.url) client.start() wait_for_session(client) assert client.is_pinging ws = client.session.connection assert ws.missed_pongs == 0 async_adapter.sleep(10) assert ws.missed_pongs == 0 client.stop()
def test_pings_and_missed_pongs(router, heartbeat, heartbeat_timeout, sleep, expected_missed_pongs): with patch('wampy.transports.websocket.connection.heartbeat', heartbeat): with patch('wampy.transports.websocket.connection.heartbeat_timeout', heartbeat_timeout): with Client(url=router.url) as client: wait_for_session(client) assert client.is_pinging is True ws = client.session.connection assert ws.missed_pongs == 0 # this prevents Pongs being put into the shared queue with patch.object(ws, 'handle_pong'): async_adapter.sleep(sleep) assert client.is_pinging is False assert ws.missed_pongs == expected_missed_pongs
def test_pings_and_missed_pongs( router, heartbeat, heartbeat_timeout, sleep, expected_missed_pongs ): with patch('wampy.transports.websocket.connection.heartbeat', heartbeat): with patch( 'wampy.transports.websocket.connection.heartbeat_timeout', heartbeat_timeout ): with Client(url=router.url) as client: wait_for_session(client) assert client.is_pinging is True ws = client.session.connection assert ws.missed_pongs == 0 # this prevents Pongs being put into the shared queue with patch.object(ws, 'handle_pong'): async_adapter.sleep(sleep) assert client.is_pinging is False assert ws.missed_pongs == expected_missed_pongs
def wait_for_session(client): with async_adapter.Timeout(TIMEOUT): while client.session.id is None: async_adapter.sleep(0.01)