From e916299ceb0ed8290835f732a704816d5e9e60c5 Mon Sep 17 00:00:00 2001 From: chrisparker126 Date: Sun, 13 Apr 2014 21:52:53 +0000 Subject: [PATCH] - reviving nxs tests for gtest framework - removed double update bug thunder found git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7275 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/services/p3idservice.cc | 5 - .../gxs/nxs_test/nxsdummyservices.cc | 59 ++++ .../gxs/nxs_test/nxsdummyservices.h | 101 ++++++ .../gxs/nxs_test/nxsgrpsync_test.cc | 96 ++++++ .../gxs/nxs_test/nxsgrpsync_test.h | 40 +++ .../gxs/nxs_test/nxsmsgsync_test.cc | 10 + .../gxs/nxs_test/nxsmsgsync_test.h | 51 +++ .../libretroshare/gxs/nxs_test/nxstesthub.cc | 295 +++++++++++------- .../libretroshare/gxs/nxs_test/nxstesthub.h | 124 ++++---- .../gxs/nxs_test/nxstestscenario.cc | 177 ----------- .../gxs/nxs_test/nxstestscenario.h | 105 ++----- .../gxs/nxs_test/rsgxsnetservice_test.cc | 43 +-- 12 files changed, 629 insertions(+), 477 deletions(-) create mode 100644 tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.cc create mode 100644 tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.h create mode 100644 tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc create mode 100644 tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h create mode 100644 tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc create mode 100644 tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h delete mode 100644 tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.cc diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 3a65491b2..370db3de7 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -748,11 +748,6 @@ bool p3IdService::updateGroup(uint32_t& token, RsGxsIdGroup &group) RsGenExchange::updateGroup(token, item); - - RsGxsGroupUpdateMeta updateMeta(RsGxsGroupId(id.toStdString())); - RsGenExchange::updateGroup(token, item); - - // if its in the cache - clear it. { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.cc new file mode 100644 index 000000000..6066b0a8a --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.cc @@ -0,0 +1,59 @@ +/* + * nxsdummyservices.cc + * + * Created on: 13 Apr 2014 + * Author: crispy + */ + + +#include "nxsdummyservices.h" + + + +rs_nxs_test::RsNxsSimpleDummyCircles::RsNxsSimpleDummyCircles( + std::list& membership, bool cached) { +} + +bool rs_nxs_test::RsNxsSimpleDummyCircles::isLoaded( + const RsGxsCircleId& circleId) { + return true; +} + +bool rs_nxs_test::RsNxsSimpleDummyCircles::loadCircle( + const RsGxsCircleId& circleId) { + return true; +} + +int rs_nxs_test::RsNxsSimpleDummyCircles::canSend(const RsGxsCircleId& circleId, + const RsPgpId& id) { + return true; +} + +int rs_nxs_test::RsNxsSimpleDummyCircles::canReceive( + const RsGxsCircleId& circleId, const RsPgpId& id) { + return true; +} + +bool rs_nxs_test::RsNxsSimpleDummyCircles::recipients( + const RsGxsCircleId& circleId, std::list& friendlist) { + return true; +} + +rs_nxs_test::RsNxsSimpleDummyReputation::RsNxsSimpleDummyReputation( + RepMap& repMap, bool cached) { +} + +bool rs_nxs_test::RsNxsSimpleDummyReputation::haveReputation( + const RsGxsId& id) { + return true; +} + +bool rs_nxs_test::RsNxsSimpleDummyReputation::loadReputation(const RsGxsId& id, + const std::list& peers) { + return true; +} + +bool rs_nxs_test::RsNxsSimpleDummyReputation::getReputation(const RsGxsId& id, + GixsReputation& rep) { + return true; +} diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.h new file mode 100644 index 000000000..206887d58 --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsdummyservices.h @@ -0,0 +1,101 @@ +/* + * nxsdummyservices.h + * + * Created on: 13 Apr 2014 + * Author: crispy + */ + +#ifndef NXSDUMMYSERVICES_H_ +#define NXSDUMMYSERVICES_H_ + + +namespace rs_nxs_test +{ + + + /*! + * This dummy circles implementation + * allow instantiation with simple membership + * list for a given circle + */ + class RsNxsSimpleDummyCircles : public RsGcxs + { + public: + + + typedef std::map > Membership; + + /*! + * + * @param membership + * @param cached + */ + RsNxsSimpleDummyCircles(std::list& membership, bool cached) + + /* GXS Interface - for working out who can receive */ + bool isLoaded(const RsGxsCircleId &circleId); + bool loadCircle(const RsGxsCircleId &circleId); + + int canSend(const RsGxsCircleId &circleId, const RsPgpId &id); + int canReceive(const RsGxsCircleId &circleId, const RsPgpId &id); + bool recipients(const RsGxsCircleId &circleId, std::list &friendlist); + + private: + + std::list mMembership; + }; + + /*! + * This dummy reputation allows you to set the + * reputations of peers + */ + class RsNxsSimpleDummyReputation : public RsGixsReputation + { + public: + + typedef std::map RepMap; + + + /*! + * Instantiate the dummy rep service with + * a reputation map + * @param repMap should contain the reputations of a set of ids + * @param cached this means initial call for an ids \n + * rep will return false, until a request has been made to load it + */ + RsNxsSimpleDummyReputation(RepMap& repMap, bool cached ); + + bool haveReputation(const RsGxsId &id); + bool loadReputation(const RsGxsId &id, const std::list& peers); + bool getReputation(const RsGxsId &id, GixsReputation &rep); + + private: + + RepMap mRepMap; + }; + + + /*! + * Very simple net manager + */ + class RsNxsNetDummyMgr : public RsNxsNetMgr + { + + public: + + RsNxsNetDummyMgr(RsPeerId ownId, std::set peers) : mOwnId(ownId), mPeers(peers) { + + } + + const RsPeerId& getOwnId() { return mOwnId; } + void getOnlineList(uint32_t serviceId, std::set& ssl_peers) { ssl_peers = mPeers; } + + private: + + RsPeerId mOwnId; + std::set mPeers; + + }; +} + +#endif /* NXSDUMMYSERVICES_H_ */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc new file mode 100644 index 000000000..65bed674b --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.cc @@ -0,0 +1,96 @@ +/* + * nxsgrpsync_test.cc + * + * Created on: 13 Apr 2014 + * Author: crispy + */ + +#include "nxsgrpsync_test.h" +#include "retroshare/rstypes.h" +#include "gxs/rsdataservice.h" +#include "nxsdummyservices.h" + +using namespace rs_nxs_test; + +template +void copy_all_but(T& ex, const std::list& s, std::list d) +{ + std::list::const_iterator cit = s.begin(); + for(; cit != s.end(); cit++) + if(*cit != ex) + d.push_back(*cit); +} + +NxsGrpSync::NxsGrpSync() +{ + // first choose ids + + int numPeers = 2; + + for(int i =0; i < numPeers; i++) + { + + + RsPeerId id = RsPeerId::random(); + mPeerIds.push_back(id); + } + + + std::list::iterator it = mPeerIds.begin(); + for(; it != mPeerIds.end(); it++) + { + // data stores + RsGeneralDataService* ds = new RsDataService("./", "grp_store" + + it->toStdString(), mServType, NULL, "key"); + mDataServices.insert(*it, ds); + + // net managers + std::list otherPeers; + copy_all_but(*it, mPeerIds, otherPeers); + RsNxsNetMgr* mgr = new RsNxsNetDummyMgr(*it, otherPeers); + mNxsNetMgrs.insert(std::make_pair(*it, mgr)); + + // now reputation service + mRep = new RsNxsSimpleDummyReputation(); + mCircles = new RsNxsSimpleDummyCircles(); + + } +} + + +void NxsGrpSync::getPeers(std::list& peerIds) +{ + peerIds = mPeerIds; +} + + + +RsGeneralDataService* NxsGrpSync::getDataService(const RsPeerId& peerId) +{ + return mDataServices[peerId]; +} + +bool NxsGrpSync::checkTestPassed() +{ + // look at data store of peer1 an compare to peer 2 + return true; +} + +RsNxsNetMgr* NxsGrpSync::getDummyNetManager(const RsPeerId& peerId) +{ + return mNxsNetMgrs[peerId]; +} + +RsGcxs* NxsGrpSync::getDummyCircles(const RsPeerId& peerId) +{ + return mCircles; +} + +RsGixsReputation* NxsGrpSync::getDummyReputations(const RsPeerId& peerId) +{ + return mRep; +} + +uint16_t NxsGrpSync::getServiceType() +{ +} diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h new file mode 100644 index 000000000..33c6751ff --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsgrpsync_test.h @@ -0,0 +1,40 @@ +/* + * nxsgrpsync_test.h + * + * Created on: 13 Apr 2014 + * Author: crispy + */ + +#ifndef NXSGRPSYNC_TEST_H_ +#define NXSGRPSYNC_TEST_H_ + + +#include "nxstestscenario.h" + +class NxsGrpSync : public NxsTestScenario +{ +public: + + NxsGrpSync(); + + void getPeers(std::list& peerIds); + RsGeneralDataService* getDataService(const RsPeerId& peerId); + bool checkTestPassed(); + RsNxsNetMgr* getDummyNetManager(const RsPeerId& peerId); + RsGcxs* getDummyCircles(const RsPeerId& peerId); + RsGixsReputation* getDummyReputations(const RsPeerId& peerId); + uint16_t getServiceType(); + +private: + + std::list mPeerIds; + std::map mDataServices; + std::map mNxsNetMgrs; + RsGixsReputation* mRep; + RsGcxs* mCircles; + + int mServType; +}; + + +#endif /* NXSGRPSYNC_TEST_H_ */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc new file mode 100644 index 000000000..ae894ccd3 --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.cc @@ -0,0 +1,10 @@ +/* + * nxsmsgsync_test.cc + * + * Created on: 13 Apr 2014 + * Author: crispy + */ + + + + diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h new file mode 100644 index 000000000..49b06329f --- /dev/null +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxsmsgsync_test.h @@ -0,0 +1,51 @@ +/* + * nxsmsgsync_test.h + * + * Created on: 13 Apr 2014 + * Author: crispy + */ + +#ifndef NXSMSGSYNC_TEST_H_ +#define NXSMSGSYNC_TEST_H_ + + + + + class NxsMessageTest : public NxsTestScenario + { + + public: + + NxsMessageTest(uint16_t servtype); + virtual ~NxsMessageTest(); + std::string getTestName(); + uint16_t getServiceType(); + RsGeneralDataService* getDataService(const RsPeerId& peer); + + /*! + * Call to remove files created + * in the test directory + */ + void cleanUp(); + + bool testPassed(); + + private: + void setUpDataBases(); + void populateStore(RsGeneralDataService* dStore); + + private: + + std::string mTestName; + std::map mPeerStoreMap; + std::set mStoreNames; + uint16_t mServType; + + RsMutex mMsgTestMtx; + + }; + + + + +#endif /* NXSMSGSYNC_TEST_H_ */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.cc index 62df3b50f..6acb8fe0e 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.cc +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.cc @@ -1,130 +1,193 @@ #include "nxstesthub.h" -NxsTestHub::NxsTestHub(NxsTestScenario * nts, std::set &peers) : mTestScenario(nts) + + +class NotifyWithPeerId : public RsNxsObserver { +public: - std::set::iterator sit = peers.begin(); + NotifyWithPeerId(RsPeerId val, NxsTestHub& hub) + : mPeerId(val), mTestHub(hub){ - for(; sit != peers.end(); sit++) - { - std::set msgPeers = peers; - - // add peers all peers except one iterator currently points to - msgPeers.erase(*sit); - NxsNetDummyMgr* dummyMgr = new NxsNetDummyMgr(*sit, msgPeers); - RsGeneralDataService* ds = mTestScenario->getDataService(*sit); - NxsMessageTestObserver* obs = new NxsMessageTestObserver(ds); - - RsServiceInfo info; - RsGxsNetService* netService = - new RsGxsNetService(mTestScenario->getServiceType(), - ds, dummyMgr, obs, info); - - - mNetServices.insert(std::make_pair(*sit, netService)); - mObservers.insert(std::make_pair(*sit, obs)); - } - - sit = peers.begin(); - - // launch net services - for(; sit != peers.end(); sit++) - { - RsGxsNetService* n = mNetServices[*sit]; - createThread(*n); - mServices.insert(std::make_pair(*sit, n)); - } -} - -NxsTestHub::~NxsTestHub() -{ - std::map::iterator mit = mNetServices.begin(); - - for(; mit != mNetServices.end(); mit++) - delete mit->second; -} - - -void NxsTestHub::run() -{ - double timeDelta = .2; - - while(isRunning()){ - - // make thread sleep for a bit - #ifndef WINDOWS_SYS - usleep((int) (timeDelta * 1000000)); - #else - Sleep((int) (timeDelta * 1000)); - #endif - - - std::map::iterator mit = mServices.begin(); - - for(; mit != mServices.end(); mit++) - { - p3Service* s = mit->second; - s->tick(); - } - - mit = mServices.begin(); - - // collect msgs to send to peers from peers - for(; mit != mServices.end(); mit++) - { - const RsPeerId& peer = mit->first; - p3Service* s = mit->second; - - // first store all the sends from all services - RsItem* item = NULL; - - while((item = s->send()) != NULL){ - - const RsPeerId peerToReceive = item->PeerId(); - - // set the peer this item comes from - item->PeerId(peer); - mPeerQueues[peerToReceive].push_back(item); - } - - - } - - // now route items to peers - std::map >::iterator mit_queue = mPeerQueues.begin(); - - for(; mit_queue != mPeerQueues.end(); mit_queue++) - { - std::vector& queueV = mit_queue->second; - std::vector::iterator vit = queueV.begin(); - const RsPeerId peerToReceive = mit_queue->first; - for(; vit != queueV.end(); vit++) - { - - RsItem* item = *vit; - p3Service* service = mServices[peerToReceive]; - - service->receive(dynamic_cast(item)); - } - queueV.clear(); - } } -} -void NxsTestHub::cleanUp() -{ - std::map::iterator mit = mNetServices.begin(); - for(; mit != mNetServices.end(); mit++) + void notifyNewMessages(std::vector& messages) { - RsGxsNetService* n = mit->second; - n->join(); + mTestHub.notifyNewMessages(mPeerId, messages); } - // also shut down this net service peers if this goes down - mTestScenario->cleanUp(); + void notifyNewGroups(std::vector& groups) + { + mTestHub.notifyNewGroups(mPeerId, groups); + } + +private: + + RsPeerId mPeerId; + rs_nxs_test::NxsTestHub& mTestHub; +}; + +rs_nxs_test::NxsTestHub::NxsTestHub(NxsTestScenario* testScenario) + : mTestScenario(testScenario) +{ + std::list peers; + mTestScenario->getPeers(peers); + + // for each peer get initialise a nxs net instance + // and pass this to the simulator + + std::list::const_iterator cit = peers.begin(); + + for(; cit != peers.end(); cit++) + { + RsGxsNetService* ns = new RsGxsNetService(mTestScenario->getServiceType(), + mTestScenario->getDataService(*cit), mTestScenario->getDummyNetManager(*cit), + new NotifyWithPeerId(*cit, *this), + mTestScenario->getServiceInfo(), mTestScenario->getDummyReputations(*cit), + mTestScenario->getDummyCircles(*cit), true); + + mPeerNxsMap.insert(std::make_pair(*cit, ns)); + } +} + + +rs_nxs_test::NxsTestHub::~NxsTestHub() { +} + + +bool rs_nxs_test::NxsTestHub::testsPassed() +{ + return mTestScenario->checkTestPassed(); +} + + +void rs_nxs_test::NxsTestHub::run() +{ + bool running = isRunning(); + double timeDelta = .2; + while(running) + { +#ifndef WINDOWS_SYS + usleep((int) (timeDelta * 1000000)); +#else + Sleep((int) (timeDelta * 1000)); +#endif + + tick(); + + running = isRunning(); + } + +} + + +void rs_nxs_test::NxsTestHub::StartTest() +{ + // get all services up and running + PeerNxsMap::iterator mit = mPeerNxsMap.begin(); + for(; mit != mPeerNxsMap.end(); mit++) + { + createThread(*(mit->second)); + } + + createThread(*this); +} + + +void rs_nxs_test::NxsTestHub::EndTest() +{ + // then stop this thread + join(); + + // stop services + PeerNxsMap::iterator mit = mPeerNxsMap.begin(); + for(; mit != mPeerNxsMap.end(); mit++) + { + mit->second->join(); + } + } -bool NxsTestHub::testsPassed() +void rs_nxs_test::NxsTestHub::notifyNewMessages(const RsPeerId& pid, + std::vector& messages) { - return false; + + std::map toStore; + std::vector::iterator it = messages.begin(); + for(; it != messages.end(); it++) + { + RsNxsMsg* msg = *it; + RsGxsMsgMetaData* meta = new RsGxsMsgMetaData(); + bool ok = meta->deserialise(msg->meta.bin_data, msg->meta.bin_len); + toStore.insert(std::make_pair(msg, meta)); + } + + RsDataService* ds = mTestScenario->getDataService(pid); + ds->storeMessage(toStore); +} + + +void rs_nxs_test::NxsTestHub::notifyNewGroups(const RsPeerId& pid, std::vector& groups) +{ + std::map toStore; + std::vector::iterator it = groups.begin(); + for(; it != groups.end(); it++) + { + RsNxsGrp* grp = *it; + RsGxsGrpMetaData* meta = new RsGxsGrpMetaData(); + bool ok = meta->deserialise(grp->meta.bin_data, grp->meta.bin_len); + toStore.insert(std::make_pair(grp, meta)); + } + + RsDataService* ds = mTestScenario->getDataService(pid); + ds->storeGroup(toStore); } + +void rs_nxs_test::NxsTestHub::Wait(int seconds) { + +#ifndef WINDOWS_SYS + usleep((int) (1000000)); +#else + Sleep((int) (1000)); +#endif +} + +void rs_nxs_test::NxsTestHub::tick() +{ + // for each nxs instance pull out all items from each and then move to destination peer + + typedef std::map > DestMap; + + PeerNxsMap::iterator it = mPeerNxsMap.begin(); + DestMap destMap; + for(; it != mPeerNxsMap.end(); it++) + { + RsGxsNetService* s = *it; + s->tick(); + RsItem* item = s->recvItem(); + + if(item != NULL) + destMap[item->PeerId()].push_back(item); + } + + DestMap::iterator dit = destMap.begin(); + + for(; dit != destMap.end(); dit++ ) + { + std::list payload = dit->second; + const RsPeerId& pid = dit->first; + + if(mPeerNxsMap.count(pid) > 0) + { + std::list::iterator pit = payload.begin(); + for(; pit != payload.end(); pit) + mPeerNxsMap[pid]->recvItem(*pit); + } + else + { + std::cerr << "Could not find peer: " << pid << std::endl; + } + } +} + + diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.h b/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.h index b4e93b46a..ea713f105 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.h +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxstesthub.h @@ -11,79 +11,83 @@ // of peers -class NxsNetDummyMgr : public RsNxsNetMgr + +namespace rs_nxs_test { -public: - - NxsNetDummyMgr(RsPeerId ownId, std::set peers) : mOwnId(ownId), mPeers(peers) { - - } - - const RsPeerId& getOwnId() { return mOwnId; } - void getOnlineList(uint32_t serviceId, std::set& ssl_peers) { ssl_peers = mPeers; } - -private: - - RsPeerId mOwnId; - std::set mPeers; - -}; - - -/*! - * Testing of nxs services occurs through use of two services - * When a service sends this class can interrogate the send and the receives of - * - * NxsScenario stores the type of synchronisation to be tested - * Operation: - * First NxsTestHub needs to be instantiated with a test scenario - * * The scenario contains two databases to be used on the communicating pair of RsGxsNetService instances (net instances) - * The Test hub has a ticker service for the p3Services which allows the netservices to search what groups and messages they have - * and synchronise according to their subscriptions. The default is to subscribe to all groups held by other peer - * The threads for both net instances are started which begins their processing of transactions - */ -class NxsTestHub : public RsThread -{ -public: - - /*! - * This construct the test hub - * for a give scenario in mind + * Testing of nxs services occurs through use of two services + * When a service sends this class can interrogate the send and the receives of + * + * NxsScenario stores the type of synchronisation to be tested + * Operation: + * First NxsTestHub needs to be instantiated with a test scenario + * * The scenario contains two databases to be used on the communicating pair of RsGxsNetService instances (net instances) + * The Test hub has a ticker service for the p3Services which allows the netservices to search what groups and messages they have + * and synchronise according to their subscriptions. The default is to subscribe to all groups held by other peer + * The threads for both net instances are started which begins their processing of transactions */ - NxsTestHub(NxsTestScenario*, std::set& peers); + class NxsTestHub : public RsThread + { + public: - /*! - * - */ - virtual ~NxsTestHub(); + /*! + * This constructs the test hub + * for a give scenario in mind + */ + NxsTestHub(NxsTestScenario* testScenario); - /*! - * To be called only after this thread has - * been shutdown - */ - bool testsPassed(); + /*! + * This cleans up what ever testing resources are left + * including the test scenario + */ + virtual ~NxsTestHub(); - /*! - * This simulates the p3Service ticker and calls both gxs net services tick methods - * Also enables transport of messages between both services - */ - void run(); + /*! + * To be called only after end test is called + * otherwise undefined + */ + bool testsPassed(); + /*! + * This simulates the p3Service ticker and calls both gxs net services tick methods + * Also enables transport of messages between both services + */ + void run(); - void cleanUp(); -private: + /*! + * Begings test, equivalent to CreateThread(this) + */ + void StartTest(); - std::map mServices; - std::map mNetServices; - std::map mObservers; + /*! + * Gracefully ends the test + */ + void EndTest(); - std::map > mPeerQueues; + /*! + * @param messages messages are deleted after function returns + */ + void notifyNewMessages(const RsPeerId&, std::vector& messages); - NxsTestScenario *mTestScenario; + /*! + * @param messages messages are deleted after function returns + */ + void notifyNewGroups(const RsPeerId&, std::vector& groups); -}; + static void Wait(int seconds); + private: + + void tick(); + + private: + + typedef std::map PeerNxsMap ; + PeerNxsMap mPeerNxsMap; + NxsTestScenario *mTestScenario; + + }; +} #endif // NXSTESTHUB_H diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.cc b/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.cc deleted file mode 100644 index c4cf5c666..000000000 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.cc +++ /dev/null @@ -1,177 +0,0 @@ -/* - * nxstestscenario.cc - * - * Created on: 10 Jul 2012 - * Author: crispy - */ - -#include "nxstestscenario.h" -#include "gxs/rsdataservice.h" -#include "retroshare/rsgxsflags.h" -#include "data_support.h" -#include - -NxsMessageTest::NxsMessageTest(uint16_t servtype) -: mServType(servtype), mMsgTestMtx("mMsgTestMtx") -{ - -} - -std::string NxsMessageTest::getTestName() -{ - return std::string("Nxs Message Test!"); -} - -NxsMessageTest::~NxsMessageTest(){ - - std::map::iterator mit = mPeerStoreMap.begin(); - - for(; mit != mPeerStoreMap.end(); mit++) - { - delete mit->second; - } - - std::set::iterator sit = mStoreNames.begin(); - - // remove db file - for(; sit != mStoreNames.end(); sit++) - { - const std::string& name = *sit; - remove(name.c_str()); - } -} -RsGeneralDataService* NxsMessageTest::getDataService(const std::string& peer) -{ - if(mPeerStoreMap.find(peer) != mPeerStoreMap.end()) return NULL; - - RsDataService* dStore = new RsDataService("./", peer, mServType); - mStoreNames.insert(peer); - mPeerStoreMap.insert(std::make_pair(peer, dStore)); - populateStore(dStore); - - return dStore; -} - -uint16_t NxsMessageTest::getServiceType() -{ - return mServType; -} - -void NxsMessageTest::populateStore(RsGeneralDataService* dStore) -{ - - int nGrp = (rand()%2)+1; - std::vector grpIdList; - std::map grps; - RsNxsGrp* grp = NULL; - RsGxsGrpMetaData* grpMeta =NULL; - for(int i = 0; i < nGrp; i++) - { - std::pair p; - grp = new RsNxsGrp(mServType); - grpMeta = new RsGxsGrpMetaData(); - p.first = grp; - p.second = grpMeta; - init_item(*grp); - init_item(grpMeta); - grpMeta->mGroupId = grp->grpId; - grps.insert(p); - grpIdList.push_back(grp->grpId); - grpMeta = NULL; - grp = NULL; - } - - dStore->storeGroup(grps); - - int nMsgs = rand()%23; - std::map msgs; - RsNxsMsg* msg = NULL; - RsGxsMsgMetaData* msgMeta = NULL; - - for(int i=0; i p(msg, msgMeta); - - // pick a grp at random to associate the msg to - const std::string& grpId = grpIdList[rand()%nGrp]; - msgMeta->mMsgId = msg->msgId; - msgMeta->mGroupId = msg->grpId = grpId; - - msg = NULL; - msgMeta = NULL; - - msgs.insert(p); - } - - - dStore->storeMessage(msgs); - - return; -} - -void NxsMessageTest::cleanUp() -{ - - std::map::iterator mit = mPeerStoreMap.begin(); - - for(; mit != mPeerStoreMap.end(); mit++) - { - RsGeneralDataService* d = mit->second; - d->resetDataStore(); - } - - return; -} - -bool NxsMessageTest::testPassed(){ - return false; -} - -/*******************************/ - -NxsMessageTestObserver::NxsMessageTestObserver(RsGeneralDataService *dStore) - :mStore(dStore) -{ - -} - -void NxsMessageTestObserver::notifyNewGroups(std::vector &groups) -{ - std::vector::iterator vit = groups.begin(); - std::map grps; - - for(; vit != groups.end(); vit++) - { - RsNxsGrp* grp = *vit; - RsGxsGrpMetaData* meta = new RsGxsGrpMetaData(); - meta->deserialise(grp->meta.bin_data, grp->meta.bin_len); - meta->mSubscribeFlags |= GXS_SERV::GROUP_SUBSCRIBE_SUBSCRIBED; - meta->mGroupId = grp->grpId; - grps.insert(std::make_pair(grp, meta)); - } - - mStore->storeGroup(grps); -} - -void NxsMessageTestObserver::notifyNewMessages(std::vector &messages) -{ - - std::vector::iterator vit = messages.begin(); - std::map msgs; - - for(; vit != messages.end(); vit++) - { - RsNxsMsg* msg = *vit; - RsGxsMsgMetaData* meta = new RsGxsMsgMetaData(); - meta->mGroupId = msg->grpId; - meta->mMsgId = msg->msgId; - msgs.insert(std::make_pair(msg, meta)); - } - - mStore->storeMessage(msgs); -} - diff --git a/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h b/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h index dd9fd5850..861dff6b6 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h +++ b/tests/unittests/libretroshare/gxs/nxs_test/nxstestscenario.h @@ -9,98 +9,31 @@ #define NXSTESTSCENARIO_H_ #include +#include "retroshare/rstypes.h" #include "gxs/rsdataservice.h" #include "gxs/rsnxsobserver.h" -/*! - * This scenario module provides data resources - */ -class NxsTestScenario +namespace rs_nxs_test { + /*! + * This scenario module provides data resources + */ + class NxsTestScenario + { + public: -public: + virtual ~NxsTestScenario() {} - virtual std::string getTestName() = 0; - - /*! - * @param peer - * @param namePath - * @return data service with populated with random grp/msg data, null if peer or pathname exists - */ - virtual RsGeneralDataService* getDataService(const RsPeerId& peer) = 0; - - - virtual bool testPassed() = 0; - /*! - * Service type for this test - * should correspond to serialiser service type - */ - virtual uint16_t getServiceType() = 0; - - /*! - * Call to remove files created - * in the test directory - */ - virtual void cleanUp() = 0; - - -}; - -class NxsMessageTestObserver : public RsNxsObserver -{ -public: - - NxsMessageTestObserver(RsGeneralDataService* dStore); - - /*! - * @param messages messages are deleted after function returns - */ - void notifyNewMessages(std::vector& messages); - - /*! - * @param messages messages are deleted after function returns - */ - void notifyNewGroups(std::vector& groups); - -private: - - RsGeneralDataService* mStore; - -}; - -class NxsMessageTest : public NxsTestScenario -{ - -public: - - NxsMessageTest(uint16_t servtype); - virtual ~NxsMessageTest(); - std::string getTestName(); - uint16_t getServiceType(); - RsGeneralDataService* getDataService(const RsPeerId& peer); - - /*! - * Call to remove files created - * in the test directory - */ - void cleanUp(); - - bool testPassed(); - -private: - void setUpDataBases(); - void populateStore(RsGeneralDataService* dStore); - -private: - - std::string mTestName; - std::map mPeerStoreMap; - std::set mStoreNames; - uint16_t mServType; - - RsMutex mMsgTestMtx; - -}; + virtual void getPeers(std::list& peerIds) = 0; + virtual RsGeneralDataService* getDataService(const RsPeerId& peerId) = 0; + virtual bool checkTestPassed() = 0; + virtual RsNxsNetMgr* getDummyNetManager(const RsPeerId& peerId) = 0; + virtual RsGcxs* getDummyCircles(const RsPeerId& peerId) = 0; + virtual RsGixsReputation* getDummyReputations(const RsPeerId& peerId) = 0; + virtual uint16_t getServiceType() = 0; + virtual RsServiceInfo getServiceInfo() = 0; + }; +} #endif /* NXSTESTSCENARIO_H_ */ diff --git a/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc b/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc index ddcc2f6c0..e31e62010 100644 --- a/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc +++ b/tests/unittests/libretroshare/gxs/nxs_test/rsgxsnetservice_test.cc @@ -5,42 +5,19 @@ * Author: crispy */ -#include "util/utest.h" +#include + +#include "nxsgrpsync_test.h" #include "nxstesthub.h" -#include "nxstestscenario.h" - -INITTEST(); -int main() +TEST(libretroshare_gxs, gxs_grp_sync) { + NxsGrpSync* gsync_test = new NxsGrpSync(); + rs_nxs_test::NxsTestHub tHub(gsync_test); + tHub.StartTest(); + rs_nxs_test::NxsTestHub::wait(10); + tHub.EndTest(); - // first setup - NxsMessageTest msgTest(RS_SERVICE_TYPE_PLUGIN_SIMPLE_FORUM); - std::set peers; - peers.insert("PeerA"); - peers.insert("PeerB"); - NxsTestHub hub(&msgTest, peers); - - // now get things started - createThread(hub); - - double timeDelta = 50; - - // put this thread to sleep for 10 secs - // make thread sleep for a bit -#ifndef WINDOWS_SYS - usleep((int) (timeDelta * 1000000)); -#else - Sleep((int) (timeDelta * 1000)); -#endif - - hub.join(); - CHECK(hub.testsPassed()); - - hub.cleanUp(); - - FINALREPORT("RsGxsNetService Tests"); - - return TESTRESULT(); + ASSERT_TRUE(tHub.testsPassed()); }