-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from 256dpi/tests
Added Tests
- Loading branch information
Showing
4 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters