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_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_auth_before_start(self):
        client = Client(endpoint=endpoint, appkey=appkey)

        auth_event = threading.Event()
        auth_delegate = auth.RoleSecretAuthDelegate(role, secret)
        mailbox = []

        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))
                auth_event.set()

        client.authenticate(auth_delegate, auth_callback)

        client.start()

        if not auth_event.wait(60):
            raise RuntimeError('Auth never finished')

        self.assertEqual(mailbox, ['Auth success'])

        client.stop()
        client.dispose()
    def test_start_stop(self):
        client = Client(endpoint=endpoint, appkey=appkey)
        client.observer = ClientObserver()

        client.start()
        client.observer.wait_connected()
        client.stop()
        client.observer.wait_stopped()

        expected_log_1 = [
            'on_leave_stopped',
            'on_enter_connecting',
            'on_leave_connecting',
            'on_enter_connected',
            'on_leave_connected',
            'on_enter_stopping',
            'on_leave_stopping',
            'on_enter_stopped']

        expected_log_2 = [
            'on_leave_stopped',
            'on_enter_connecting',
            'on_leave_connecting',
            'on_enter_stopped']

        try:
            self.assertEqual(client.observer.log, expected_log_1)
        except:
            self.assertEqual(client.observer.log, expected_log_2)
    def test_1(self):
        client = Client(endpoint=endpoint, appkey=appkey)

        global error
        error = "Publish didn't happen"
        exit = threading.Event()

        def after_publish(ack):
            global error
            if ack['action'] == 'rtm/publish/ok':
                error = None
            else:
                error = 'Publish failed {0}'.format(ack)
            exit.set()

        client.publish(channel, 'some-message', callback=after_publish)
        client.start()

        if not exit.wait(60):
            raise RuntimeError('Publish never finished')

        self.assertEqual(error, None)

        client.stop()
        client.dispose()
Exemple #6
0
    def test_stop_already_stopped(self):
        client = Client(endpoint=endpoint, appkey=appkey)
        client.observer = ClientObserver()
        client.stop()
        client.dispose()

        expected_log = ['on_leave_stopped', 'on_enter_disposed']

        self.assertEqual(client.observer.log, expected_log)
 def test_before_start(self):
     client = Client(endpoint=endpoint, appkey=appkey)
     co = ClientObserver()
     client.observer = co
     so = SubscriptionObserver()
     client.subscribe(channel,
                      SubscriptionMode.ADVANCED,
                      subscription_observer=so)
     client.start()
     co.wait_connected()
     sync_publish(client, channel, 'message')
     channel_data = so.wait_for_channel_data()
     self.assertEqual(channel_data['messages'], ['message'])
     client.stop()
     co.wait_stopped()
    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_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()
Exemple #10
0
    def test_start_wait_stop(self):
        client = Client(endpoint=endpoint, appkey=appkey)
        client.observer = ClientObserver()

        client.start()
        client.observer.wait_connected()
        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_stopping',
            'on_leave_stopping', 'on_enter_stopped', 'on_leave_stopped',
            'on_enter_disposed'
        ]

        self.assertEqual(client.observer.log, expected_log)
    def test_missing_pong(self):
        satori.rtm.connection.ping_interval_in_seconds = 0.4
        client = Client(endpoint=endpoint, appkey=appkey, reconnect_interval=0)
        co = ClientObserver()
        client.observer = co

        client.start()
        client.observer.wait_connected('First connect timeout')

        # emulate the absence of server pongs and silence in the socket
        client._internal.connection.on_ws_ponged = lambda: None
        client._internal.connection.last_ponged_time = 0
        client._internal.connection.on_ws_pong = lambda x, y: None

        client.observer.wait_disconnected('First disconnect timeout')
        client.observer.wait_connected('Second connect timeout')

        client.stop()
        client.dispose()
        satori.rtm.connection.ping_interval_in_seconds = 60
    def test_repeat_second_message(self):
        client = Client(endpoint=endpoint, appkey=appkey, reconnect_interval=1)
        client.observer = ClientObserver()

        channel = make_channel_name('resubscribe')

        client.start()
        client.observer.wait_connected()
        so = sync_subscribe(client, channel)
        sync_publish(client, channel, 'first-message')
        first_channel_data = so.wait_for_channel_data()
        sync_publish(client, channel, 'second-message')
        second_channel_data = so.wait_for_channel_data()
        client.unsubscribe(channel)
        client.subscribe(channel,
                         SubscriptionMode.ADVANCED,
                         so,
                         args={'position': first_channel_data['position']})
        self.assertEqual(second_channel_data['messages'],
                         so.wait_for_channel_data()['messages'])
        client.unsubscribe(channel)
        so.wait_not_subscribed()
        client.stop()
        client.dispose()
    def test_missing_client_observer_callbacks_are_fine(self):

        client = Client(endpoint=endpoint, appkey=appkey)
        client.observer = object()
        client.start()
        client.stop()
 def test_rapid_stop_start(self):
     client = Client(endpoint=endpoint, appkey=appkey)
     for i in six.moves.range(100):
         client.start()
         client.stop()
     print_resource_usage()
 def test_many_queued_publishes(self):
     client = Client(endpoint=endpoint, appkey=appkey)
     for i in six.moves.range(1000000):
         client.publish(channel=channel, message=message)
     print_resource_usage()
 def test_many_idle_subscriptions(self):
     client = Client(endpoint=endpoint, appkey=appkey)
     for i in six.moves.range(10000):
         client.subscribe(channel='{0}.{1}'.format(channel, i))
     print_resource_usage()
Exemple #17
0
    def connect(self):
        config = self.config
        self.GPIO = GPIO
        self.GPIO.setmode(self.GPIO.BOARD)

        should_authenticate = 'role' in config[
            'satori'] and 'role_secret_key' in config['satori']
        auth_delegate = None if not should_authenticate else auth.RoleSecretAuthDelegate(
            config['satori']['role'], config['satori']['role_secret_key'])

        client = Client(
            endpoint=config['satori']['endpoint'],
            appkey=config['satori']['appkey'],
        )
        ready_event = threading.Event()

        class Observer:
            def on_enter_connected(self):
                ready_event.set()

            def on_enter_stopped(self):
                ready_event.set()

        client.observer = Observer()
        client.start()
        if not ready_event.wait(70):
            if client.last_connecting_error():
                client.dispose()
                raise RuntimeError(
                    "Client connection timeout, last connection error: {0}".
                    format(client.last_connecting_error()))
            else:
                raise RuntimeError("Client connection timeout")
        ready_event.clear()
        if not client.is_connected():
            client.dispose()
            raise RuntimeError("Client connection error: {0}".format(
                client.last_connecting_error()))

        auth_mailbox = []

        def auth_callback(auth_result):
            auth_mailbox.append(auth_result)
            ready_event.set()

        if auth_delegate:
            client.authenticate(auth_delegate, callback=auth_callback)

            if not ready_event.wait(20):
                client.dispose()
                print('[FAIL] Authentication process has timed out')
                raise Exception('Authentication process has timed out')

            auth_result = auth_mailbox[0]

            if type(auth_result) == auth.Error:
                raise Exception(auth_result.message)

            print('[OK] Auth success in make_client')

        print("[OK] Connected to Satori RTM")
        client.subscribe(config['channels']['in'], SubscriptionMode.SIMPLE,
                         self)

        self.client = client