Skip to content

Commit

Permalink
Merge pull request #15 from 256dpi/tests
Browse files Browse the repository at this point in the history
Added Tests
  • Loading branch information
256dpi committed Dec 4, 2015
2 parents 3a5d2f4 + efc3242 commit a158faf
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
23 changes: 23 additions & 0 deletions examples/tests/MQTTClient/MQTTClient.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <Bridge.h>
#include <YunClient.h>
#include <MQTTClient.h>
#include <MQTTTest.h>

YunClient net;
MQTTClient client;
MQTTTest<MQTTClient> test;

void setup() {
Bridge.begin();
Serial.begin(9600);

// run a very basic automated test
client.begin("broker.shiftr.io", net);
test.run(&client);
}

void loop() {}

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
test.message(topic, payload);
}
21 changes: 21 additions & 0 deletions examples/tests/YunMQTTClient/YunMQTTClient.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <Bridge.h>
#include <YunMQTTClient.h>
#include <MQTTTest.h>

YunMQTTClient client;
MQTTTest<YunMQTTClient> test;

void setup() {
Bridge.begin();
Serial.begin(9600);

// run a very basic automated test
client.begin("broker.shiftr.io");
test.run(&client);
}

void loop() {}

void messageReceived(String topic, String payload, char * bytes, unsigned int length) {
test.message(topic, payload);
}
86 changes: 86 additions & 0 deletions src/MQTTTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <Arduino.h>

template <class T>
class MQTTTest {
public:
void run(T *client);
void message(String topic, String payload);
private:
T *client;
void printResult(boolean res);
boolean testMessage(const char * topic, const char * payload);
boolean newMessage = false;
boolean passedTest = false;
String testTopic;
String testPayload;
boolean testConnectivity(boolean test);
};

/* Methods */

template <class T>
void MQTTTest<T>::run(T *client) {
this->client = client;

Serial.println("Starting tests...");

Serial.print("[Test 1] Connect: ");
this->printResult(this->client->connect("arduino-mqtt-test", "try", "try") && this->testConnectivity(true));

Serial.print("[Test 2] Subscribe & Publish: ");
this->client->subscribe("arduino-mqtt-test/topic1");
this->client->publish("arduino-mqtt-test/topic1", "test");
this->printResult(this->testMessage("arduino-mqtt-test/topic1", "test"));

Serial.print("[Test 3] Unsubscribe: ");
this->client->subscribe("arduino-mqtt-test/topic2");
this->client->subscribe("arduino-mqtt-test/topic3");
this->client->unsubscribe("arduino-mqtt-test/topic2");
this->client->publish("arduino-mqtt-test/topic2", "test");
this->client->publish("arduino-mqtt-test/topic3", "test");
this->printResult(this->testMessage("arduino-mqtt-test/topic3", "test"));

Serial.print("[Test 4] Disconnect: ");
this->client->disconnect();
this->printResult(this->testConnectivity(false));

Serial.println("Tests finished!");
}

template <class T>
void MQTTTest<T>::message(String topic, String payload) {
this->newMessage = true;
this->passedTest = topic.equals(this->testTopic) && payload.equals(this->testPayload);
}

/* Helpers */

template <class T>
void MQTTTest<T>::printResult(boolean res) {
res ? Serial.println("SUCCESS") : Serial.println("FAILED");
}

template <class T>
boolean MQTTTest<T>::testMessage(const char *topic, const char *payload) {
this->testTopic = String(topic);
this->testPayload = String(payload);

while(!this->newMessage) {
this->client->loop();
}

boolean ret = this->passedTest;
this->newMessage = false;
this->passedTest = false;

return ret;
}

template <class T>
boolean MQTTTest<T>::testConnectivity(boolean test) {
while(this->client->connected() != test) {
this->client->loop();
}

return this->client->connected() == test;
}
9 changes: 6 additions & 3 deletions yun/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ def __init__(self):
self.client = None
self.will_topic = ""
self.will_payload = ""
self.stopped = False

# Bridge Callbacks
def on_connect(self, _, __, ___, rc):
self.send_command("a;" if rc == 0 else "r;")
def on_message(self, _, __, msg):
self.send_command("m:" + msg.topic + ":" + str(len(msg.payload)) + ";" + str(msg.payload))
def on_disconnect(self):
self.client.loop_stop()
def on_disconnect(self, _, __, ___):
self.send_command("e;")
self.stopped = True

# Command Helpers
def parse_command(self, line):
Expand Down Expand Up @@ -51,6 +52,7 @@ def do_connect(self, args):
self.client = mqtt.Client(args[2])
self.client.on_connect = self.on_connect
self.client.on_message = self.on_message
self.client.on_disconnect = self.on_disconnect
if len(args) >= 5:
self.client.username_pw_set(args[3], args[4])
if len(self.will_topic) > 0:
Expand All @@ -68,12 +70,13 @@ def do_unsubscribe(self, args):
def do_publish(self, args):
self.client.publish(args[0], self.read_chunk(int(args[1])))
def do_disconnect(self):
self.client.loop_stop()
self.client.disconnect()

# Main
def run(self):
self.send_command("b;")
while True:
while not self.stopped:
self.parse_command(self.read_until(self.PAYLOAD_SEPERATOR))

# Low Level Helpers
Expand Down

0 comments on commit a158faf

Please sign in to comment.