def test_reconnect_zero_threshold(self): client = Client( endpoint=endpoint, appkey=appkey, reconnect_interval=0, fail_count_threshold=0) client.observer = ClientObserver() client.start() client.observer.wait_connected() client._internal._endpoint = 'ws://bogus' emulate_websocket_disconnect(client) client.observer.wait_disconnected() client.observer.wait_stopped() client.dispose() expected_log = [ 'on_leave_stopped', 'on_enter_connecting', 'on_leave_connecting', 'on_enter_connected', 'on_leave_connected', 'on_enter_awaiting', 'on_leave_awaiting', 'on_enter_connecting', 'on_leave_connecting', 'on_enter_stopped', 'on_leave_stopped', 'on_enter_disposed'] self.assertEqual(client.observer.log, expected_log)
def test_reconnect(self): client = Client( endpoint=endpoint, appkey=appkey, reconnect_interval=0, fail_count_threshold=1) client.observer = ClientObserver() client.start() client.observer.wait_connected('First connect timeout') emulate_websocket_disconnect(client) client.observer.wait_disconnected() client.observer.wait_connected('Second connect timeout') client.stop() client.observer.wait_stopped() client.dispose() expected_log = [ 'on_leave_stopped', 'on_enter_connecting', 'on_leave_connecting', 'on_enter_connected', 'on_leave_connected', 'on_enter_awaiting', 'on_leave_awaiting', 'on_enter_connecting', 'on_leave_connecting', 'on_enter_connected', 'on_leave_connected', 'on_enter_stopping', 'on_leave_stopping', 'on_enter_stopped', 'on_leave_stopped', 'on_enter_disposed'] self.assertEqual(client.observer.log, expected_log)
def test_automatic_resubscribe(self): client = Client( endpoint=endpoint, appkey=appkey, reconnect_interval=0) client.observer = ClientObserver() channel = make_channel_name('resubscribe') client.start() client.observer.wait_connected('First connect timeout') so = sync_subscribe(client, channel) sync_publish(client, channel, 'first-message') first_channel_data = so.wait_for_channel_data() emulate_websocket_disconnect(client) so.wait_not_subscribed() client.observer.wait_disconnected() client.observer.wait_connected('Second connect timeout') so.wait_subscribed('Second subscribe timeout') sync_publish(client, channel, 'second-message') second_channel_data = so.wait_for_channel_data() client.unsubscribe(channel) so.wait_not_subscribed() client.stop() client.dispose() expected_log = [ 'on_leave_unsubscribed', 'on_enter_subscribing', 'on_leave_subscribing', 'on_enter_subscribed', ('data', first_channel_data), # point of disconnect 'on_leave_subscribed', 'on_enter_unsubscribed', # point of reconnect 'on_leave_unsubscribed', 'on_enter_subscribing', 'on_leave_subscribing', 'on_enter_subscribed', ('data', second_channel_data), 'on_leave_subscribed', 'on_enter_unsubscribing', 'on_leave_unsubscribing', 'on_enter_unsubscribed', 'on_deleted'] self.assertEqual(so.log, expected_log)
def test_two_clients_with_best_effort_delivery(self): with make_client(endpoint=endpoint, appkey=appkey, reconnect_interval=0) as subscriber: with make_client(endpoint=endpoint, appkey=appkey) as publisher: co1 = ClientObserver() subscriber.observer = co1 co2 = ClientObserver() publisher.observer = co2 try: so = sync_subscribe(subscriber, channel, mode=SubscriptionMode.RELIABLE) sync_publish(publisher, channel, 'first-message') so.wait_for_channel_data('First receive timeout') emulate_websocket_disconnect(subscriber) so.wait_not_subscribed() # send a message while subscriber is disconnected sync_publish(publisher, channel, 'second-message') so.wait_subscribed('Second subscribe timeout') so.wait_for_channel_data('Second receive timeout') # send a message after subscribed reconnected publisher.publish(channel, 'third-message') so.wait_for_channel_data('Third receive timeout') expected_messages =\ ['first-message', 'second-message', 'third-message'] got_messages = [] for log_entry in so.log: if log_entry[0] == 'data': got_messages += log_entry[1]['messages'] self.assertEqual(got_messages, expected_messages) except Exception: print('Subscriber log: {0}'.format(co1.log)) print('Publisher log: {0}'.format(co2.log)) print('Subscription log: {0}'.format(so.log)) raise
def test_reauth(self): client = Client(endpoint=endpoint, appkey=appkey, reconnect_interval=0) auth_delegate = auth.RoleSecretAuthDelegate(role, secret) auth_event = threading.Event() mailbox = [] co = ClientObserver() client.observer = co client.start() co.wait_connected() def auth_callback(auth_result): if type(auth_result) == auth.Done: mailbox.append('Auth success') auth_event.set() else: mailbox.append('Auth failure: {0}'.format( auth_result.message)) auth_event.set() client.authenticate(auth_delegate, auth_callback) if not auth_event.wait(30): raise RuntimeError("Auth timeout") self.assertEqual(mailbox, ['Auth success']) so = sync_subscribe(client, restricted_channel) message1 = make_channel_name('before disconnect') sync_publish(client, restricted_channel, message1) first_data = so.wait_for_channel_data() self.assertTrue(message1 in first_data['messages']) emulate_websocket_disconnect(client) co.wait_disconnected() co.wait_connected() message2 = make_channel_name('after reconnect') sync_publish(client, restricted_channel, message2) second_data = so.wait_for_channel_data() self.assertTrue(message2 in second_data['messages']) client.stop() client.dispose()
def test_two_clients(self): with make_client(endpoint=endpoint, appkey=appkey, reconnect_interval=0) as subscriber: with make_client(endpoint=endpoint, appkey=appkey) as publisher: co1 = ClientObserver() subscriber.observer = co1 co2 = ClientObserver() publisher.observer = co2 try: so = sync_subscribe(subscriber, channel) sync_publish(publisher, channel, 'first-message') so.wait_for_channel_data('First receive timeout') emulate_websocket_disconnect(subscriber) so.wait_not_subscribed() # send a message while subscriber is disconnected sync_publish(publisher, channel, 'second-message') so.wait_subscribed('Second subscribe timeout') so.wait_for_channel_data('Second receive timeout') # send a message after subscribed reconnected publisher.publish(channel, 'third-message') so.wait_for_channel_data('Third receive timeout') expected_messages =\ ['first-message', 'second-message', 'third-message'] got_messages = so.extract_received_messages() self.assertEqual(got_messages, expected_messages) except Exception: print('Subscriber log: {0}'.format(co1.log)) print('Publisher log: {0}'.format(co2.log)) print('Subscription log: {0}'.format(so.log)) raise