BugFix of Wildcard of Topic Issue #40

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2016-10-25 21:10:22 +09:00
parent 3c9b7d283b
commit 19c4d8208e
20 changed files with 667 additions and 183 deletions

View File

@@ -16,7 +16,10 @@
#include <string.h>
#include <cassert>
#include "TestProcessFramework.h"
#include "TestProcess.h"
#include "TestTopics.h"
#include "TestQue.h"
#include "TestTopicIdMap.h"
#include "MQTTSNGWProcess.h"
#include "MQTTSNGWClient.h"
#include "MQTTSNGWPacket.h"
@@ -31,18 +34,18 @@ using namespace MQTTSNGW;
const char* currentDateTime(void);
TestProcessFramework::TestProcessFramework()
TestProcess::TestProcess()
{
theMultiTaskProcess = this;
theProcess = this;
}
TestProcessFramework::~TestProcessFramework()
TestProcess::~TestProcess()
{
}
void TestProcessFramework::initialize(int argc, char** argv)
void TestProcess::initialize(int argc, char** argv)
{
MultiTaskProcess::initialize(argc, argv);
assert(0 == strcmp(CONFDIR, getConfigDirName()->c_str()));
@@ -50,63 +53,28 @@ void TestProcessFramework::initialize(int argc, char** argv)
resetRingBuffer();
}
void TestProcessFramework::run(void)
void TestProcess::run(void)
{
char value[256];
int* v = 0;
int i = 0;
Timer tm;
TestQue que;
/* Test command line parameter */
assert(1 == getArgc() || 3 == getArgc() );
assert(0 == strcmp(ARGV, *getArgv()));
getParam("BrokerName", value);
assert(0 == strcmp("iot.eclipse.org", value));
/* Test RingBuffer */
for ( i = 0; i < 1000; i++)
{
putLog("Test RingBuffer %d ", 1234567890);
}
putLog("\n\nRingBuffer Test complieted.\n");
for ( i = 0; i < 10; i++ )
{
v = new int(i);
que.post(v);
}
assert( 10 == que.size());
for ( i = 0; i < 10; i++ )
{
assert(i == *que.front());
int* p = que.front();
if ( p )
{
assert(i == *p);
que.pop();
delete p;
}
}
assert(0 == que.front());
assert(0 == que.size());
que.setMaxSize(5);
for ( i = 0; i < 10; i++ )
{
v = new int(i);
que.post(v);
assert( 5 >= que.size());
}
for ( i = 0; i < 10; i++ )
{
int* p = que.front();
if ( p )
{
que.pop();
delete p;
}
}
putLog("\n\nRingBuffer Test complieted.\n\n");
/* Test Timer */
printf("Timer Test start\n");
printf("%s Timer start\n", currentDateTime());
tm.start(1000);
while (!tm.isTimeup());
@@ -115,7 +83,26 @@ void TestProcessFramework::run(void)
tm.start();
while (!tm.isTimeup(1000));
printf("%s Timer 1sec\n", currentDateTime());
printf("Timer Test completed\n\n");
/* Test Que */
TestQue* tque = new TestQue();
tque->test();
delete tque;
/* Test TopicTable */
TestTopics* testTopic = new TestTopics();
testTopic->test();
delete testTopic;
/* Test TopicIdMap */
TestTopicIdMap* testMap = new TestTopicIdMap();
testMap->test();
delete testMap;
/* Test EventQue */
printf("EventQue test start.\n");
Client* client = new Client();
_evQue.setMaxSize(EVENT_CNT);
@@ -128,40 +115,7 @@ void TestProcessFramework::run(void)
_evQue.post(ev);
}
MultiTaskProcess::run();
printf("ProcessFramework test complited.\n");
}
TestQue::TestQue()
{
}
TestQue::~TestQue()
{
}
int* TestQue::front(void)
{
return _que.front();
}
void TestQue::pop(void)
{
_que.pop();
}
int TestQue::size(void)
{
return _que.size();
}
void TestQue::setMaxSize(int maxsize)
{
_que.setMaxSize(maxsize);
}
void TestQue::post(int* val)
{
_que.post(val);
printf("\n\nAll Tests completed.\n");
}

View File

@@ -13,18 +13,18 @@
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#ifndef TESTPROCESSFRAMEWORK_H_
#define TESTPROCESSFRAMEWORK_H_
#ifndef TESTPROCESS_H_
#define TESTPROCESS_H_
#include "MQTTSNGWProcess.h"
#include "MQTTSNGateway.h"
#define EVENT_CNT 10
namespace MQTTSNGW
{
class TestProcessFramework: public MultiTaskProcess{
class TestProcess: public MultiTaskProcess{
public:
TestProcessFramework();
~TestProcessFramework();
TestProcess();
~TestProcess();
virtual void initialize(int argc, char** argv);
void run(void);
EventQue* getEventQue(void) { return &_evQue; }
@@ -33,19 +33,6 @@ private:
EventQue _evQue;
};
class TestQue
{
public:
TestQue();
~TestQue();
void post(int*);
int* front(void);
void pop(void);
int size(void);
void setMaxSize(int maxsize);
private:
Que<int> _que;
};
}
#endif /* TESTPROCESSFRAMEWORK_H_ */
#endif /* TESTPROCESS_H_ */

View File

@@ -0,0 +1,101 @@
/**************************************************************************************
* Copyright (c) 2016, Tomoaki Yamaguchi
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#include <string.h>
#include <tests/TestProcess.h>
#include <cassert>
#include "TestQue.h"
using namespace std;
using namespace MQTTSNGW;
TestQue::TestQue()
{
}
TestQue::~TestQue()
{
}
void TestQue::test(void)
{
int* v = 0;
int i = 0;
printf("Que Test start.\n");
for ( i = 0; i < 10; i++ )
{
v = new int(i);
this->post(v);
}
assert( 10 == this->size());
for ( i = 0; i < 10; i++ )
{
assert(i == *this->front());
int* p = this->front();
if ( p )
{
assert(i == *p);
this->pop();
delete p;
}
}
assert(0 == this->front());
assert(0 == this->size());
this->setMaxSize(5);
for ( i = 0; i < 10; i++ )
{
v = new int(i);
this->post(v);
assert( 5 >= this->size());
}
for ( i = 0; i < 10; i++ )
{
int* p = this->front();
if ( p )
{
this->pop();
delete p;
}
}
printf("Que test completed.\n\n");
}
int* TestQue::front(void)
{
return _que.front();
}
void TestQue::pop(void)
{
_que.pop();
}
int TestQue::size(void)
{
return _que.size();
}
void TestQue::setMaxSize(int maxsize)
{
_que.setMaxSize(maxsize);
}
void TestQue::post(int* val)
{
_que.post(val);
}

View File

@@ -0,0 +1,41 @@
/**************************************************************************************
* Copyright (c) 2016, Tomoaki Yamaguchi
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_
#define MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_
#include "MQTTSNGWProcess.h"
namespace MQTTSNGW
{
class TestQue
{
public:
TestQue();
~TestQue();
void post(int*);
int* front(void);
void pop(void);
int size(void);
void setMaxSize(int maxsize);
void test(void);
private:
Que<int> _que;
};
}
#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_ */

View File

@@ -17,11 +17,11 @@
#include <cassert>
#include "TestTask.h"
#include "Threading.h"
#include "TestProcess.h"
using namespace std;
using namespace MQTTSNGW;
TestTask::TestTask(TestProcessFramework* proc)
TestTask::TestTask(TestProcess* proc)
{
proc->attach((Thread*)this);
_proc = proc;
@@ -39,23 +39,24 @@ void TestTask::initialize(int argc, char** argv)
void TestTask::run(void)
{
int evcnt = 0;
EventQue* evQue = _proc->getEventQue();
uint16_t duration = 0;
int cnt = 0;
while (true)
{
Event* ev = evQue->timedwait(5000);
evcnt++;
if ( ev->getEventType() == EtTimeout )
{
assert(EVENT_CNT == cnt);
assert(EVENT_CNT + 1 == evcnt);
delete ev;
printf("EventQue test complete.\n\n");
break;
}
cnt++;
MQTTSNPacket* packet = ev->getMQTTSNPacket();
packet->getDISCONNECT(&duration);
printf("Event %d\n", duration);
delete ev;
}

View File

@@ -16,8 +16,8 @@
#ifndef TESTTASK_H_
#define TESTTASK_H_
#include "TestProcess.h"
#include "Threading.h"
#include "TestProcessFramework.h"
namespace MQTTSNGW
{
@@ -27,13 +27,13 @@ class TestTask: public Thread
MAGIC_WORD_FOR_THREAD;
;
public:
TestTask(TestProcessFramework* proc);
TestTask(TestProcess* proc);
~TestTask();
void initialize(int argc, char** argv);
void run(void);
private:
TestProcessFramework* _proc;
TestProcess* _proc;
};
}

View File

@@ -0,0 +1,125 @@
/**************************************************************************************
* Copyright (c) 2016, Tomoaki Yamaguchi
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <cassert>
#include "TestTopicIdMap.h"
using namespace std;
using namespace MQTTSNGW;
TestTopicIdMap::TestTopicIdMap()
{
_map = new TopicIdMap();
}
TestTopicIdMap::~TestTopicIdMap()
{
delete _map;
}
#define MAXID 30
void TestTopicIdMap::test(void)
{
uint16_t id[MAXID];
printf("Test TopicIdMat start.\n");
for ( int i = 0; i < MAXID; i++ )
{
id[i] = i + 1;
}
for ( int i = 0; i < MAXID; i++ )
{
_map->add(id[i], id[i], MQTTSN_TOPIC_TYPE_NORMAL);
}
for ( int i = 0; i < MAXID; i++ )
{
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT;
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
assert((i <= MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == i) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0));
}
//printf("\n");
for ( int i = 0; i < 5; i++ )
{
_map->erase(i);
}
for ( int i = 0; i < MAXID; i++ )
{
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT;
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
assert((i < 5 && topicId == 0) || (i >= 5 && topicId != 0) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0) );
}
_map->clear();
//printf("\n");
for ( int i = 0; i < MAXID; i++ )
{
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT;
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
assert( topicId == 0 );
}
for ( int i = 0; i < MAXID; i++ )
{
_map->add(id[i], id[i], MQTTSN_TOPIC_TYPE_SHORT);
}
for ( int i = 0; i < MAXID; i++ )
{
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL;
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
assert((i <= MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == i) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0));
}
//printf("\n");
for ( int i = 0; i < 5; i++ )
{
_map->erase(i);
}
for ( int i = 0; i < MAXID; i++ )
{
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL;
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
assert((i < 5 && topicId == 0) || (i >= 5 && topicId != 0) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0) );
}
_map->clear();
//printf("\n");
for ( int i = 0; i < MAXID; i++ )
{
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL;
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
assert( topicId == 0 );
}
printf("Test TopicIdMat completed.\n\n");
}

View File

@@ -0,0 +1,32 @@
/**************************************************************************************
* Copyright (c) 2016, Tomoaki Yamaguchi
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_
#define MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_
#include "MQTTSNGWClient.h"
class TestTopicIdMap
{
public:
TestTopicIdMap();
~TestTopicIdMap();
void test(void);
private:
TopicIdMap* _map;
};
#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_ */

View File

@@ -0,0 +1,183 @@
/**************************************************************************************
* Copyright (c) 2016, Tomoaki Yamaguchi
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <cassert>
#include "TestTopics.h"
using namespace std;
using namespace MQTTSNGW;
TestTopics::TestTopics()
{
_topics = new Topics();
}
TestTopics::~TestTopics()
{
delete _topics;
}
void TestTopics::test(void)
{
printf("Topics Test start.\n");
MQTTSN_topicid topic[12];
char tp[12][10];
/* create Topic */
strcpy(tp[0], "Topic/+");
tp[0][7] = 0;
topic[0].type = MQTTSN_TOPIC_TYPE_NORMAL;
topic[0].data.long_.len = strlen(tp[0]);
topic[0].data.long_.name = tp[0];
for ( int i = 1; i < 10 ; i++ )
{
strcpy(tp[i], "Topic/+/");
tp[i][8] = 0x30 + i;
tp[i][9] = 0;
topic[i].type = MQTTSN_TOPIC_TYPE_NORMAL;
topic[i].data.long_.len = strlen(tp[i]);
topic[i].data.long_.name = tp[i];
}
strcpy(tp[10], "TOPIC/#");
tp[10][7] = 0;
topic[10].type = MQTTSN_TOPIC_TYPE_NORMAL;
topic[10].data.long_.len = strlen(tp[10]);
topic[10].data.long_.name = tp[10];
strcpy(tp[11], "+/0/#");
tp[11][7] = 0;
topic[11].type = MQTTSN_TOPIC_TYPE_NORMAL;
topic[11].data.long_.len = strlen(tp[11]);
topic[11].data.long_.name = tp[11];
/* Add Topic to Topics */
for ( int i = 0; i < 12; i++ )
{
MQTTSN_topicid pos = topic[i];
Topic* t = _topics->add(&pos);
//printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId());
assert(t !=0);
}
for ( int i = 0; i < 5; i++ )
{
string str = "Test/";
str += 0x30 + i;
Topic* t = _topics->add(&str);
//printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId());
assert(t !=0);
}
/* Get Topic by MQTTSN_topicid */
for ( int i = 0; i < 12; i++ )
{
Topic* t = _topics->getTopic(&topic[i]);
//printf("Topic=%s ID=%d ID=%d\n", t->getTopicName()->c_str(), t->getTopicId(),_topics->getTopicId(&topic[i]));
assert(t->getTopicId() == i + 1);
}
/* Get TopicId by MQTTSN_topicid */
for ( int i = 0; i < 12; i++ )
{
uint16_t id = _topics->getTopicId(&topic[i]);
//printf("ID=%d \n", id);
assert(id == i + 1);
}
/* Test Wilecard */
for ( int i = 0; i < 10 ; i++ )
{
MQTTSN_topicid tp1;
char tp0[10];
strcpy(tp0, "Topic/");
tp0[6] = 0x30 + i;
tp0[7] = '/';
tp0[8] = 0x30 + i;
tp0[9] = 0;
tp1.type = MQTTSN_TOPIC_TYPE_NORMAL;
tp1.data.long_.len = strlen(tp0);
tp1.data.long_.name = tp0;
Topic* t = _topics->match(&tp1);
/*
if (t)
{
printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str());
}
else
{
printf("Topic=%s unmatch\n", tp0);
}
*/
assert(t != 0);
}
for ( int i = 0; i < 10 ; i++ )
{
MQTTSN_topicid tp1;
char tp0[10];
strcpy(tp0, "Topic/");
tp0[6] = 0x30 + i;
tp0[7] = 0;
tp1.type = MQTTSN_TOPIC_TYPE_NORMAL;
tp1.data.long_.len = strlen(tp0);
tp1.data.long_.name = tp0;
Topic* t = _topics->match(&tp1);
/*
if (t)
{
printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str());
}
else
{
printf("Topic=%s unmatch\n", tp0);
}
*/
assert(t != 0);
}
for ( int i = 0; i < 10 ; i++ )
{
MQTTSN_topicid tpid1;
char tp0[10];
strcpy(tp0, "TOPIC/");
tp0[6] = 0x30 + i;
tp0[7] = '/';
tp0[8] = 0x30 + i;
tp0[9] = 0;
tpid1.type = MQTTSN_TOPIC_TYPE_NORMAL;
tpid1.data.long_.len = strlen(tp0);
tpid1.data.long_.name = tp0;
Topic* t = _topics->match(&tpid1);
/*
if (t)
{
printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str());
}
else
{
printf("Topic=%s unmatch\n", tp0);
}
*/
assert( t != 0);
}
printf("Topics Test complete.\n\n");
}

View File

@@ -0,0 +1,32 @@
/**************************************************************************************
* Copyright (c) 2016, Tomoaki Yamaguchi
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_
#define MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_
#include "MQTTSNGWClient.h"
class TestTopics
{
public:
TestTopics();
~TestTopics();
void test(void);
private:
Topics* _topics;
};
#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_ */

View File

@@ -13,19 +13,19 @@
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
**************************************************************************************/
#include "TestProcessFramework.h"
#include "TestProcess.h"
#include "TestTask.h"
using namespace MQTTSNGW;
TestProcessFramework* proc = new TestProcessFramework();
TestTask* task = new TestTask(proc);
TestProcess* test = new TestProcess();
TestTask* task = new TestTask(test);
int main(int argc, char** argv)
{
proc->initialize(argc, argv);
proc->run();
delete proc;
test->initialize(argc, argv);
test->run();
delete test;
return 0;
}