Rev | Line | |
---|
[209] | 1 | import unittest
|
---|
| 2 | import settings
|
---|
| 3 |
|
---|
| 4 | import time
|
---|
| 5 | import mosquitto
|
---|
| 6 |
|
---|
| 7 | import serial
|
---|
| 8 |
|
---|
| 9 | def on_message(mosq, obj, msg):
|
---|
| 10 | obj.message_queue.append(msg)
|
---|
| 11 |
|
---|
| 12 | class mqtt_basic(unittest.TestCase):
|
---|
| 13 |
|
---|
| 14 | message_queue = []
|
---|
| 15 |
|
---|
| 16 | @classmethod
|
---|
| 17 | def setUpClass(self):
|
---|
| 18 | self.client = mosquitto.Mosquitto("pubsubclient_ut", clean_session=True,obj=self)
|
---|
| 19 | self.client.connect(settings.server_ip)
|
---|
| 20 | self.client.on_message = on_message
|
---|
| 21 | self.client.subscribe("outTopic",0)
|
---|
| 22 |
|
---|
| 23 | @classmethod
|
---|
| 24 | def tearDownClass(self):
|
---|
| 25 | self.client.disconnect()
|
---|
| 26 |
|
---|
| 27 | def test_one(self):
|
---|
| 28 | i=30
|
---|
| 29 | while len(self.message_queue) == 0 and i > 0:
|
---|
| 30 | self.client.loop()
|
---|
| 31 | time.sleep(0.5)
|
---|
| 32 | i -= 1
|
---|
| 33 | self.assertTrue(i>0, "message receive timed-out")
|
---|
| 34 | self.assertEqual(len(self.message_queue), 1, "unexpected number of messages received")
|
---|
| 35 | msg = self.message_queue[0]
|
---|
| 36 | self.assertEqual(msg.mid,0,"message id not 0")
|
---|
| 37 | self.assertEqual(msg.topic,"outTopic","message topic incorrect")
|
---|
| 38 | self.assertEqual(msg.payload,"hello world")
|
---|
| 39 | self.assertEqual(msg.qos,0,"message qos not 0")
|
---|
| 40 | self.assertEqual(msg.retain,False,"message retain flag incorrect")
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.