def test_subscribe_error_after_a_cycle(self):
        with make_client(endpoint=endpoint,
                         appkey=appkey,
                         reconnect_interval=1) as client:

            old_received_message =\
                client._internal.connection.on_incoming_text_frame
            client._internal.connection.on_incoming_text_frame =\
                lambda *args: None

            so = SubscriptionObserver()
            so2 = SubscriptionObserver()
            client.subscribe(channel, SubscriptionMode.ADVANCED, so)
            client.unsubscribe(channel)
            client.subscribe(channel, SubscriptionMode.ADVANCED, so2)

            client._queue.join()

            self.assertEqual(client._internal.subscriptions[channel]._mode,
                             'cycle')
            old_received_message(
                '{"action":"rtm/subscribe/ok","body":{"channel":"' + channel +
                '","position":"position"},"id":0}')

            so.wait_subscribed()

            self.assertEqual(client._internal.subscriptions[channel]._mode,
                             'cycle')
            old_received_message(
                '{"action":"rtm/unsubscribe/ok","body":{},"id":1}')

            so.wait_not_subscribed()

            self.assertEqual(client._internal.subscriptions[channel]._mode,
                             'linked')
            old_received_message(
                '{"action":"rtm/subscribe/error","body":{},"id":2}')

            self.assertEqual(client._internal.subscriptions[channel]._mode,
                             'linked')
            self.assertEqual(
                client._internal.subscriptions[channel]._sm.get_state_name(),
                'Subscription.Failed')
            so2.wait_failed()

            expected_log = [
                'on_leave_unsubscribed', 'on_enter_subscribing',
                'on_leave_subscribing', 'on_enter_unsubscribing',
                'on_leave_unsubscribing', 'on_enter_unsubscribed', 'on_deleted'
            ]

            expected_log2 = [
                'on_created', 'on_leave_unsubscribed', 'on_enter_subscribing',
                'on_leave_subscribing', ('on_enter_failed', 'Subscribe error')
            ]

            self.assertEqual(so.log, expected_log)
            self.assertEqual(so2.log, expected_log2)
Esempio n. 2
0
    def test_subscribe_error_in_cycle_mode(self):
        with make_client(endpoint=endpoint,
                         appkey=appkey,
                         reconnect_interval=1) as client:

            old_received_message =\
                client._internal.connection.on_incoming_json
            client._internal.connection.on_incoming_json =\
                lambda *args: None

            so = SubscriptionObserver()
            so2 = SubscriptionObserver()
            client.subscribe(channel, SubscriptionMode.ADVANCED, so)
            client.unsubscribe(channel)
            client.subscribe(channel, SubscriptionMode.ADVANCED, so2)

            client._queue.join()

            self.assertEqual(client._internal.subscriptions[channel].mode,
                             'cycle')
            old_received_message({
                u"action": u"rtm/subscribe/error",
                u"body": {},
                u"id": 0
            })

            client._queue.join()

            self.assertEqual(client._internal.subscriptions[channel].mode,
                             'linked')
            old_received_message({
                u"action": u"rtm/subscribe/ok",
                u"body": {},
                u"id": 1
            })
            so2.wait_subscribed()

            expected_log = [
                'on_leave_unsubscribed', 'on_enter_subscribing',
                'on_leave_subscribing', 'on_deleted'
            ]

            expected_log2 = [
                'on_created', 'on_enter_subscribing', 'on_leave_subscribing',
                'on_enter_subscribed'
            ]

            self.assertEqual(so.log, expected_log)
            self.assertEqual(so2.log, expected_log2)
Esempio n. 3
0
    def test_filter(self):
        with make_client(endpoint=endpoint, appkey=appkey) as client:
            ch = make_channel_name('filter')
            so = SubscriptionObserver()
            mode = SubscriptionMode.RELIABLE
            args = {'filter': 'select test from ' + ch}
            client.subscribe(ch, mode, so, args=args)
            so.wait_subscribed()

            sync_publish(client, ch, {'test': 42, 'unused': 1})
            message = so.wait_for_channel_data()['messages'][0]
            self.assertEqual(message, {'test': 42})

            sync_publish(client, ch, {'unused': 1})
            message = so.wait_for_channel_data()['messages'][0]
            self.assertEqual(message, {'test': None})
    def test_change_observer(self):
        with make_client(endpoint=endpoint, appkey=appkey) as client:

            co = ClientObserver()
            client.observer = co

            channel = make_channel_name('change_observer')
            so1 = sync_subscribe(client, channel)

            client.unsubscribe(channel)
            so2 = SubscriptionObserver()
            client.subscribe(channel,
                             SubscriptionMode.ADVANCED,
                             subscription_observer=so2)

            so2.wait_subscribed()

            client.stop()

            co.wait_disconnected()

            self.maxDiff = None
            expected_so1_log = [
                'on_leave_unsubscribed', 'on_enter_subscribing',
                'on_leave_subscribing', 'on_enter_subscribed',
                'on_leave_subscribed', 'on_enter_unsubscribing',
                'on_leave_unsubscribing', 'on_enter_unsubscribed', 'on_deleted'
            ]

            expected_so2_log = [
                'on_created', 'on_leave_unsubscribed', 'on_enter_subscribing',
                'on_leave_subscribing', 'on_enter_subscribed'
            ]

            self.assertEqual(so1.log, expected_so1_log)
            self.assertEqual(so2.log, expected_so2_log)