Exemplo n.º 1
0
    def test_on_ready(self):
        self.heard_control = False
        consumer = Consumer('valid-token')

        @consumer.on('heardControl')
        def fn(*args):
            self.assertTrue(consumer.ready, 'Provider was not ready')
            self.heard_control = True

        @consumer.on('checkedPacket')
        def fn(*args):
            self.assertTrue(self.heard_control, 'Order was not preserved')

        @consumer.on('pong')
        def fn(*args):
            self.packet_received.set()

        consumer.send_control(UUID, 'test').subscribe(UUID)
        consumer.connect(**CONNECT_KWARGS)
        consumer.send_control(UUID, 'ping')
        self.wait_for_packet(consumer)
Exemplo n.º 2
0
 def setUp(self):
     super(ConsumerTestCase, self).setUp()
     self.client_type = 'consumer'
     self.consumer = Consumer('valid-token')
     self.consumer.connect(**CONNECT_KWARGS)
     self.client = self.consumer
Exemplo n.º 3
0
class ConsumerTestCase(HeimdallrClientTestCase):
    def setUp(self):
        super(ConsumerTestCase, self).setUp()
        self.client_type = 'consumer'
        self.consumer = Consumer('valid-token')
        self.consumer.connect(**CONNECT_KWARGS)
        self.client = self.consumer

    def tearDown(self):
        super(ConsumerTestCase, self).tearDown()

    def test_receives_packets(self):
        def fn(data):
            self.assertDictEqual(
                data,
                {'ping': 'data'},
                'Ping data did not match'
            )
            self.packet_received.set()

        self.consumer.on('ping', fn)
        self.trigger('ping')
        self.wait_for_packet()

    def test_raises_exceptions(self):
        self.trigger('JSON-error')
        self.assertRaises(
            HeimdallrClientException,
            partial(self.wait_for_packet, self.consumer)
        )
        self.trigger('js-error')
        self.assertRaises(
            HeimdallrClientException,
            partial(self.wait_for_packet, self.consumer)
        )

    def test_decorates_callbacks(self):
        @self.consumer.on('ping')
        def fn(data):
            self.assertDictEqual(
                data,
                {'ping': 'data'},
                'Ping data did not match'
            )
            self.packet_received.set()

        self.trigger('ping')
        self.wait_for_packet()

    def test_send_control(self):
        self.consumer.on('heardControl', self.set_packet_received)
        self.consumer.send_control(UUID, 'test')
        self.wait_for_packet()

    def test_on_ready(self):
        self.heard_control = False
        consumer = Consumer('valid-token')

        @consumer.on('heardControl')
        def fn(*args):
            self.assertTrue(consumer.ready, 'Provider was not ready')
            self.heard_control = True

        @consumer.on('checkedPacket')
        def fn(*args):
            self.assertTrue(self.heard_control, 'Order was not preserved')

        @consumer.on('pong')
        def fn(*args):
            self.packet_received.set()

        consumer.send_control(UUID, 'test').subscribe(UUID)
        consumer.connect(**CONNECT_KWARGS)
        consumer.send_control(UUID, 'ping')
        self.wait_for_packet(consumer)

    def test_multiple_listeners(self):
        self.called_first = False

        @self.consumer.on('ping')
        def fn(*args):
            self.called_first = True

        @self.consumer.on('ping')
        def fn(*args):
            self.assertTrue(self.called_first, 'Order was not preserved')
            self.set_packet_received()

        self.trigger('ping')
        self.wait_for_packet()

    def test_remove_listener(self):
        def callback(*args):
            self.fail('Called removed listener')

        @self.consumer.on('ping')
        def fn(*args):
            callback()

        @self.consumer.on('ping')
        def fn(*args):
            callback()

        self.consumer.remove_listener('ping')

        self.consumer.on('ping', callback)

        @self.consumer.on('ping')
        def fn(*args):
            self.set_packet_received()

        self.consumer.remove_listener('ping', callback)
        self.trigger('ping')
        self.wait_for_packet()

    def test_set_filter(self):
        self.consumer.on('checkedPacket', self.set_packet_received)
        self.consumer.set_filter(UUID, {'event': [], 'sensor': []})
        self.wait_for_packet()

    def test_get_state(self):
        self.consumer.on('checkedPacket', self.set_packet_received)
        self.consumer.get_state(UUID, [])
        self.wait_for_packet()

    def test_subscription_actions(self):
        subscription_actions = [
            'subscribe',
            'unsubscribe',
            'join_stream',
            'leave_stream'
        ]
        self.count = 0

        @self.consumer.on('checkedPacket')
        def fn(*args):
            self.count += 1
            if self.count == len(subscription_actions):
                self.packet_received.set()

        for action in subscription_actions:
            getattr(self.consumer, action)(UUID)

        self.wait_for_packet()