def test_real_publishing(self, mqtt_settings, client, broker, transport): log.info("Broker running: %s", broker) self.published = [] self.subscribed = False def on_message(client: Client, userdata, msg: MQTTMessage): msg = msg.payload.decode('utf-8').strip() client.publish(mqtt_settings.topic_resp, "GOT: {m}".format(m=msg).encode('utf-8')) def on_subscribe(client, data, mid, granted_qos): self.subscribed = True def on_publish(client, userdata, mid): self.published.append(mid) client.on_publish = on_publish replier = CustomTlsCustomClient(mqtt_settings, on_subscribe=on_subscribe, on_message=on_message) replier.connect() transport.start() replier.subscribe(mqtt_settings.topic_req) assert replier.loop_start() != MQTT_ERR_INVAL wait_for(lambda x: self.subscribed, what="confirming subscription", timeout=3) reply = transport.communicate(TEST_MSG, timeout=3) wait_for(lambda x: self.published, what="confirming publish", timeout=3) assert len(self.published) == 1 log.debug("Received reply: %s", reply) assert reply == "GOT: {msg}".format(msg=TEST_MSG)
def test_real_publishing(self): test_mqtt_settings = self.mqtt_settings() self.published = [] def on_message(client: Client, userdata, msg: MQTTMessage): msg = msg.payload.decode('utf-8').strip() client.publish(test_mqtt_settings.topic_resp, "GOT: {msg}".format(msg=msg).encode('utf-8')) replier = CustomTlsCustomClient(test_mqtt_settings) replier.on_message = on_message replier.connect() def on_publish(client, userdata, mid): self.published.append(mid) client = CustomTlsCustomClient(test_mqtt_settings) client.on_publish = on_publish msg = "TEST MESSAGE at %s" % datetime.now() transport = create_transport(ssl_config=None, mqtt_settings=test_mqtt_settings, mqtt_client=client) try: replier.subscribe(test_mqtt_settings.topic_req) assert replier.loop_start() != MQTT_ERR_INVAL reply = transport.communicate(msg) wait_for(lambda x: self.published, what="confirming publish") finally: del transport replier.loop_stop() del replier assert len(self.published) == 1 assert reply == "GOT: {msg}".format(**locals())
def test_timeout_raises_nicely(self): context = FakeSettings() with raises(Exception) as e: wait_for(lambda x: sleep(1.1), 1, "Doing things", context) assert "Failed \"Doing things\"" in str(e) # assert str(context) in str(e) assert "after 1.2 seconds" in str(e)
def start(self): def connected(client, userdata, flags, rc): print_d("Connected to {}. Subscribing to {}", self.client, self.resp_topic) self.client.subscribe(self.resp_topic, qos=1) self.client.on_connect = connected assert self.client.loop_start() != MQTT_ERR_INVAL self.client.connect() wait_for(lambda s: s.is_connected, what="connection", context=self)
def communicate(self, raw: str, wait=True) -> str: data = raw.strip() + '\n' num_lines = data.count('\n') self._clear() ret = self.client.publish(self.req_topic, data.encode('utf-8'), qos=1 if wait else 0) if not wait: return None ret.wait_for_publish() if ret.rc != MQTT_ERR_SUCCESS: raise Error("Error publishing message: {}", error_string(ret.rc)) print_d("Published to {topic} OK. Waiting for {num} line(s)...", topic=self.req_topic, num=num_lines) wait_for(lambda s: len(s.response_lines) >= num_lines, context=self, what="response from mqtt-squeeze", timeout=5) return "\n".join(m.decode('utf-8') for m in self.response_lines)
def start(self): def connected(client, userdata, flags, rc): print_d("Connected to {client}. Subscribing to {topic}", client=self.client, topic=self.resp_topic) result, mid = self.client.subscribe(self.resp_topic, qos=1) if result != MQTT_ERR_SUCCESS: raise Error("Couldn't subscribe to '{topic}'", self.resp_topic) def disconnected(client, userdata, rc): print_d("Disconnected from {client}", client=self.client) self.is_connected = False self.is_connected = self.client.connected if self.is_connected: print_d("Already connected, great!") return self.client.on_connect = connected self.client.on_disconnect = disconnected assert self.client.loop_start() != MQTT_ERR_INVAL self.client.connect() wait_for(lambda s: s.is_connected, what="connection", context=self) return self
def test_over_connect(self, broker, client, transport): transport.start() wait_for(lambda t: t.is_connected, context=transport) transport.start() transport.start() assert client.connections == 1, "Over connected to MQTT"