Exemple #1
0
    def test_close_port():
        config_dict = {}
        config_dict['topic'] = 'foo/bar'

        with mock.patch('user.MQTTSubscribe.MQTTSubscriber'):
            SUT = MQTTSubscribeDriver(**config_dict)
            SUT.closePort()
            SUT.subscriber.disconnect.assert_called_once()  # pylint: disable=no-member
Exemple #2
0
    def test_close_port():
        config_dict = {}
        config_dict['topic'] = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])

        with mock.patch('user.MQTTSubscribe.MQTTSubscriber'):
            SUT = MQTTSubscribeDriver(**config_dict)
            SUT.closePort()
            SUT.subscriber.disconnect.assert_called_once()  # pylint: disable=no-member
    def runit(self, payload, file_pointer, check_results=True):
        test_data = json.load(file_pointer, object_hook=utils.byteify)
        config_dict = configobj.ConfigObj(test_data['config'])
        testruns = test_data['testruns']

        cdict = config_dict['MQTTSubscribeService']
        if not 'message_callback' in config_dict['MQTTSubscribeService']:
            config_dict['MQTTSubscribeService']['message_callback'] = {}
        config_dict['MQTTSubscribeService']['message_callback'][
            'type'] = payload

        driver = MQTTSubscribeDriver(**cdict)

        host = 'localhost'
        port = 1883
        keepalive = 60

        userdata = {'topics': [], 'connected_flag': False}
        client = mqtt.Client(userdata=userdata)
        client.on_connect = utils.on_connect
        client.connect(host, port, keepalive)
        client.loop_start()

        max_connect_wait = 1  # ToDo - configure
        i = 1
        while not userdata['connected_flag']:
            if i > max_connect_wait:
                self.fail("Timed out waiting for connections.")
            time.sleep(1)
            i += 1

        userdata2 = {
            'topics': cdict['topics'].sections,
            'connected_flag': False,
            'msg': False,
            'max_msg_wait': 1  # ToDo - configure
        }
        client2 = mqtt.Client(userdata=userdata2)
        client2.on_connect = utils.on_connect
        client2.on_message = utils.on_message
        client2.connect(host, port, keepalive)
        client2.loop_start()

        max_connect2_wait = 1  # ToDo - configure
        i = 1
        while not userdata2['connected_flag']:
            #print("waiting to connect")
            if i > max_connect2_wait:
                self.fail("Timed out waiting for connection 2.")
            time.sleep(1)
            i += 1

        max_waits = 10
        for testrun in testruns:
            for topics in testrun['messages']:
                for topic in topics:
                    topic_info = topics[topic]
                    msg_count = utils.send_msg(utils.send_mqtt_msg, payload,
                                               client.publish, topic,
                                               topic_info, userdata2, self)
                    wait_count = utils.wait_on_queue(driver, msg_count,
                                                     max_waits, 1)

                    # If queue not filled, fail now
                    # otherwise will end up in 'infinite' loop in genLoopPackets
                    if wait_count >= max_waits:
                        self.fail("Could not fill queue.")

            records = []
            gen = driver.genLoopPackets()

            more_data = False
            # hack to check if there is more data in the queues
            for topics in testrun['messages']:
                for topic in driver.subscriber.manager.subscribed_topics:
                    if driver.subscriber.manager.has_data(topic):
                        more_data = True

            while more_data:
                data = next(gen, None)
                records.append(data)
                more_data = False
                # hack to check if there is more data in the queues
                for topics in testrun['messages']:
                    for topic in driver.subscriber.manager.subscribed_topics:
                        if driver.subscriber.manager.has_data(topic):
                            more_data = True

            if check_results:
                results = testrun['results']
                result = {}
                found = False
                for result in results:
                    if 'driver' in result['test']:
                        if payload in result['payloads']:
                            found = True
                            break
                self.assertTrue(found, "No results for %s" % payload)

                utils.check(self, payload, records, result['records'])
            else:
                for record in records:
                    print(record)

        driver.closePort()
        client.disconnect()
        client2.disconnect()