eclipse / paho.mqtt.testing

An Eclipse Paho project - a Python broker for testing
https://eclipse.org/paho
Other
108 stars 73 forks source link

MQTTV5: test_unsubscribe: assert is always true #39

Closed troian closed 6 years ago

troian commented 7 years ago
    def test_unsubscribe(self):
      callback2.clear()
      bclient.connect(host=host, port=port, cleanstart=True)
      bclient.subscribe([topics[0]], [MQTTV5.SubscribeOptions(2)])
      bclient.subscribe([topics[1]], [MQTTV5.SubscribeOptions(2)])
      bclient.subscribe([topics[2]], [MQTTV5.SubscribeOptions(2)])
      time.sleep(1) # wait for any retained messages, hopefully
      # Unsubscribed from one topic
      bclient.unsubscribe([topics[0]])

      aclient.connect(host=host, port=port, cleanstart=True)
      aclient.publish(topics[0], b"topic 0 - unsubscribed", 1, retained=False)
      aclient.publish(topics[1], b"topic 1", 1, retained=False)
      aclient.publish(topics[2], b"topic 2", 1, retained=False)
      time.sleep(2)

      bclient.disconnect()
      aclient.disconnect()
      self.assertEqual(len(callback2.messages), 2, callback2.messages)

Test above will always fail as callback2.messages might have more than 2 messages if there was some retained one. callback2.clear() should be after bclient.unsubscribe([topics[0]])

    def test_unsubscribe(self):
      bclient.connect(host=host, port=port, cleanstart=True)
      bclient.subscribe([topics[0]], [MQTTV5.SubscribeOptions(2)])
      bclient.subscribe([topics[1]], [MQTTV5.SubscribeOptions(2)])
      bclient.subscribe([topics[2]], [MQTTV5.SubscribeOptions(2)])
      time.sleep(1) # wait for any retained messages, hopefully
      # Unsubscribed from one topic
      bclient.unsubscribe([topics[0]])
      callback2.clear()

      aclient.connect(host=host, port=port, cleanstart=True)
      aclient.publish(topics[0], b"topic 0 - unsubscribed", 1, retained=False)
      aclient.publish(topics[1], b"topic 1", 1, retained=False)
      aclient.publish(topics[2], b"topic 2", 1, retained=False)
      time.sleep(2)

      bclient.disconnect()
      aclient.disconnect()
      self.assertEqual(len(callback2.messages), 2, callback2.messages)
icraggs commented 7 years ago

Or cleanRetained() could be called at the beginning of the test.

(When the entire suite of tests is run, cleanRetained() is run at the start, so this shouldn't be a problem unless the test is run in isolation.)

icraggs commented 6 years ago

Fixed now. Thanks.