Skip to content

Commit a158faf

Browse files
committed
Merge pull request 256dpi#15 from 256dpi/tests
Added Tests
2 parents 3a5d2f4 + efc3242 commit a158faf

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <Bridge.h>
2+
#include <YunClient.h>
3+
#include <MQTTClient.h>
4+
#include <MQTTTest.h>
5+
6+
YunClient net;
7+
MQTTClient client;
8+
MQTTTest<MQTTClient> test;
9+
10+
void setup() {
11+
Bridge.begin();
12+
Serial.begin(9600);
13+
14+
// run a very basic automated test
15+
client.begin("broker.shiftr.io", net);
16+
test.run(&client);
17+
}
18+
19+
void loop() {}
20+
21+
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
22+
test.message(topic, payload);
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <Bridge.h>
2+
#include <YunMQTTClient.h>
3+
#include <MQTTTest.h>
4+
5+
YunMQTTClient client;
6+
MQTTTest<YunMQTTClient> test;
7+
8+
void setup() {
9+
Bridge.begin();
10+
Serial.begin(9600);
11+
12+
// run a very basic automated test
13+
client.begin("broker.shiftr.io");
14+
test.run(&client);
15+
}
16+
17+
void loop() {}
18+
19+
void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
20+
test.message(topic, payload);
21+
}

src/MQTTTest.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <Arduino.h>
2+
3+
template <class T>
4+
class MQTTTest {
5+
public:
6+
void run(T *client);
7+
void message(String topic, String payload);
8+
private:
9+
T *client;
10+
void printResult(boolean res);
11+
boolean testMessage(const char * topic, const char * payload);
12+
boolean newMessage = false;
13+
boolean passedTest = false;
14+
String testTopic;
15+
String testPayload;
16+
boolean testConnectivity(boolean test);
17+
};
18+
19+
/* Methods */
20+
21+
template <class T>
22+
void MQTTTest<T>::run(T *client) {
23+
this->client = client;
24+
25+
Serial.println("Starting tests...");
26+
27+
Serial.print("[Test 1] Connect: ");
28+
this->printResult(this->client->connect("arduino-mqtt-test", "try", "try") && this->testConnectivity(true));
29+
30+
Serial.print("[Test 2] Subscribe & Publish: ");
31+
this->client->subscribe("arduino-mqtt-test/topic1");
32+
this->client->publish("arduino-mqtt-test/topic1", "test");
33+
this->printResult(this->testMessage("arduino-mqtt-test/topic1", "test"));
34+
35+
Serial.print("[Test 3] Unsubscribe: ");
36+
this->client->subscribe("arduino-mqtt-test/topic2");
37+
this->client->subscribe("arduino-mqtt-test/topic3");
38+
this->client->unsubscribe("arduino-mqtt-test/topic2");
39+
this->client->publish("arduino-mqtt-test/topic2", "test");
40+
this->client->publish("arduino-mqtt-test/topic3", "test");
41+
this->printResult(this->testMessage("arduino-mqtt-test/topic3", "test"));
42+
43+
Serial.print("[Test 4] Disconnect: ");
44+
this->client->disconnect();
45+
this->printResult(this->testConnectivity(false));
46+
47+
Serial.println("Tests finished!");
48+
}
49+
50+
template <class T>
51+
void MQTTTest<T>::message(String topic, String payload) {
52+
this->newMessage = true;
53+
this->passedTest = topic.equals(this->testTopic) && payload.equals(this->testPayload);
54+
}
55+
56+
/* Helpers */
57+
58+
template <class T>
59+
void MQTTTest<T>::printResult(boolean res) {
60+
res ? Serial.println("SUCCESS") : Serial.println("FAILED");
61+
}
62+
63+
template <class T>
64+
boolean MQTTTest<T>::testMessage(const char *topic, const char *payload) {
65+
this->testTopic = String(topic);
66+
this->testPayload = String(payload);
67+
68+
while(!this->newMessage) {
69+
this->client->loop();
70+
}
71+
72+
boolean ret = this->passedTest;
73+
this->newMessage = false;
74+
this->passedTest = false;
75+
76+
return ret;
77+
}
78+
79+
template <class T>
80+
boolean MQTTTest<T>::testConnectivity(boolean test) {
81+
while(this->client->connected() != test) {
82+
this->client->loop();
83+
}
84+
85+
return this->client->connected() == test;
86+
}

yun/bridge.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ def __init__(self):
1111
self.client = None
1212
self.will_topic = ""
1313
self.will_payload = ""
14+
self.stopped = False
1415

1516
# Bridge Callbacks
1617
def on_connect(self, _, __, ___, rc):
1718
self.send_command("a;" if rc == 0 else "r;")
1819
def on_message(self, _, __, msg):
1920
self.send_command("m:" + msg.topic + ":" + str(len(msg.payload)) + ";" + str(msg.payload))
20-
def on_disconnect(self):
21-
self.client.loop_stop()
21+
def on_disconnect(self, _, __, ___):
2222
self.send_command("e;")
23+
self.stopped = True
2324

2425
# Command Helpers
2526
def parse_command(self, line):
@@ -51,6 +52,7 @@ def do_connect(self, args):
5152
self.client = mqtt.Client(args[2])
5253
self.client.on_connect = self.on_connect
5354
self.client.on_message = self.on_message
55+
self.client.on_disconnect = self.on_disconnect
5456
if len(args) >= 5:
5557
self.client.username_pw_set(args[3], args[4])
5658
if len(self.will_topic) > 0:
@@ -68,12 +70,13 @@ def do_unsubscribe(self, args):
6870
def do_publish(self, args):
6971
self.client.publish(args[0], self.read_chunk(int(args[1])))
7072
def do_disconnect(self):
73+
self.client.loop_stop()
7174
self.client.disconnect()
7275

7376
# Main
7477
def run(self):
7578
self.send_command("b;")
76-
while True:
79+
while not self.stopped:
7780
self.parse_command(self.read_until(self.PAYLOAD_SEPERATOR))
7881

7982
# Low Level Helpers

0 commit comments

Comments
 (0)