From: jyong2.kim Date: Tue, 23 Feb 2016 08:04:04 +0000 (+0900) Subject: Modified unit test of Resource Hosting. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b2ccd2a8f3c8d0bb594632a2a95ace64897c9c3e;p=contrib%2Fiotivity.git Modified unit test of Resource Hosting. When build with TEST keyword, unit test of RH is executed. Fixed bug about late timing of resource discovery. Change-Id: I43bba1a80cd20e78126887fd5dbd232458ada971 Signed-off-by: jyong2.kim Reviewed-on: https://gerrit.iotivity.org/gerrit/5177 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- diff --git a/service/resource-hosting/unittest/HostingObjectUnitTest.cpp b/service/resource-hosting/unittest/HostingObjectUnitTest.cpp old mode 100644 new mode 100755 index aa98b09..96d7700 --- a/service/resource-hosting/unittest/HostingObjectUnitTest.cpp +++ b/service/resource-hosting/unittest/HostingObjectUnitTest.cpp @@ -30,24 +30,53 @@ using namespace OIC::Service; namespace { - std::string HOSTING_INTERFACETYPE = "oic.r.resourcehosting"; - std::string TEST_ATT_KEY = "Temperature"; - bool isDeleted = false; - void onDestroy() + const std::string HOSTING_RESOURCE_TYPE = "oic.r.resourcehosting"; + const std::string TEST_ATT_KEY = "Temperature"; + + bool isStarted = false; + bool isFinished = false; + + ResourceEncapsulationTestSimulator testObject; + RCSRemoteResourceObject::Ptr remoteObject; + + HostingObject::Ptr instance; + RCSRemoteResourceObject::Ptr discoveredResource; + + void onDestroy(){ } + void onDiscoveryResource(RCSRemoteResourceObject::Ptr){ } + void onUpdatedCache(const RCSResourceAttributes &) { } + + void setup() { - isDeleted = true; + if(!isStarted) + { + testObject.defaultRunSimulator(); + remoteObject = testObject.getRemoteResource(); + + instance = HostingObject::createHostingObject( + remoteObject, &onDestroy); + + testObject.getResourceServer()->setAttribute( + "Temperature", RCSResourceAttributes::Value((int)0)); + + isStarted = true; + } } - void onDiscoveryResource(RCSRemoteResourceObject::Ptr){ } - void onUpdatedCache(const RCSResourceAttributes &) { } + void tearDown() + { + if(isFinished) + { + testObject.destroy(); + instance.reset(); + isStarted = false; + } + } } class HostingObjectTest : public TestWithMock { public: - ResourceEncapsulationTestSimulator::Ptr testObject; - RCSRemoteResourceObject::Ptr remoteObject; - std::mutex mutexForCondition; std::condition_variable responseCon; @@ -56,30 +85,13 @@ protected: void SetUp() { TestWithMock::SetUp(); - - testObject = std::make_shared(); - testObject->defaultRunSimulator(); - remoteObject = testObject->getRemoteResource(); - - isDeleted = false; + setup(); } void TearDown() { TestWithMock::TearDown(); - - if(remoteObject.use_count() > 0) - { - if(remoteObject->isCaching()) - { - remoteObject->stopCaching(); - } - if(remoteObject->isMonitoring()) - { - remoteObject->stopMonitoring(); - } - } - testObject->destroy(); + tearDown(); } public: @@ -98,83 +110,52 @@ public: TEST_F(HostingObjectTest, startCachingAtInitialize) { - HostingObject::Ptr instance = HostingObject::createHostingObject( - remoteObject, &onDestroy); - EXPECT_TRUE(remoteObject->isCaching()); } TEST_F(HostingObjectTest, startMonitoringAtInitialize) { - HostingObject::Ptr instance = HostingObject::createHostingObject( - remoteObject, onDestroy); - ASSERT_TRUE(remoteObject->isMonitoring()); } TEST_F(HostingObjectTest, getRemoteResourceisValid) { - HostingObject::Ptr instance = HostingObject::createHostingObject( - remoteObject, onDestroy); - ASSERT_EQ(remoteObject->getUri(), instance->getRemoteResource()->getUri()); } TEST_F(HostingObjectTest, createMirroredServer) { - int waitForResponse = 2000; + int waitForResponse = 1000; std::string uri = ""; - HostingObject::Ptr instance = HostingObject::createHostingObject(remoteObject, onDestroy); - waitForCondition(); - testObject->getResourceServer()-> - setAttribute(TEST_ATT_KEY, RCSResourceAttributes::Value(int(10))); - std::unique_ptr discoveryTask = { }; + waitForCondition(waitForResponse); mocks.OnCallFunc(onDiscoveryResource).Do( - [this, &uri, &discoveryTask](RCSRemoteResourceObject::Ptr ptr) + [this, &uri, &discoveryTask, &testObject, &discoveredResource] + (RCSRemoteResourceObject::Ptr ptr) { - if(ptr->getUri() == testObject->getHostedServerUri()) + if(ptr->getUri() == testObject.getHostedServerUri()) { uri = ptr->getUri(); + discoveredResource = ptr; discoveryTask->cancel(); notifyCondition(); } }); discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( - RCSAddress::multicast(), HOSTING_INTERFACETYPE, onDiscoveryResource); + RCSAddress::multicast(), "/oic/res", HOSTING_RESOURCE_TYPE, onDiscoveryResource); waitForCondition(waitForResponse); - EXPECT_EQ(testObject->getHostedServerUri(), uri); + EXPECT_EQ(testObject.getHostedServerUri(), uri); } TEST_F(HostingObjectTest, UpdateCachedDataWhenChangedOriginResource) { int waitForResponse = 1000; - HostingObject::Ptr instance = HostingObject::createHostingObject(remoteObject, onDestroy); - std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse}); - - std::unique_ptr discoveryTask = { }; - RCSRemoteResourceObject::Ptr discoveredResource = { }; - - mocks.OnCallFunc(onDiscoveryResource).Do( - [this, &discoveredResource, &discoveryTask](RCSRemoteResourceObject::Ptr ptr) - { - if(ptr->getUri() == testObject->getHostedServerUri()) - { - discoveredResource = ptr; - discoveryTask->cancel(); - notifyCondition(); - } - }); - - discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( - RCSAddress::multicast(), HOSTING_INTERFACETYPE, onDiscoveryResource); - waitForCondition(waitForResponse); - RCSResourceAttributes::Value result = { }; + mocks.OnCallFunc(onUpdatedCache).Do( [this, &result](const RCSResourceAttributes & att) { @@ -186,9 +167,11 @@ TEST_F(HostingObjectTest, UpdateCachedDataWhenChangedOriginResource) std::this_thread::sleep_for(std::chrono::milliseconds {waitForResponse}); RCSResourceAttributes::Value settingValue = 10; - testObject->getResourceServer()->setAttribute(TEST_ATT_KEY, settingValue); + testObject.getResourceServer()->setAttribute(TEST_ATT_KEY, settingValue); waitForCondition(waitForResponse); + isFinished = true; + EXPECT_EQ(result.toString(), settingValue.toString()); } diff --git a/service/resource-hosting/unittest/RequestObjectUnitTest.cpp b/service/resource-hosting/unittest/RequestObjectUnitTest.cpp old mode 100644 new mode 100755 index 48d1025..299e931 --- a/service/resource-hosting/unittest/RequestObjectUnitTest.cpp +++ b/service/resource-hosting/unittest/RequestObjectUnitTest.cpp @@ -21,29 +21,47 @@ #include "UnitTestHelper.h" #include "ResourceEncapsulationTestSimulator.h" -#include "OCResourceRequest.h" #include "RequestObject.h" -#include "RCSRequest.h" using namespace testing; using namespace OIC::Service; namespace { - void setRequestCB( - const RCSResourceAttributes &, int, - const RCSRequest &, RequestObject::Ptr) { } + bool isStarted = false; + bool isFinished = false; + + ResourceEncapsulationTestSimulator testObject; + RCSRemoteResourceObject::Ptr remoteObject; + + void setRequestCB(const RCSResourceAttributes &, int, const RCSRequest &, RequestObject::Ptr) + { + } + + void setup() + { + if(!isStarted) + { + testObject.defaultRunSimulator(); + remoteObject = testObject.getRemoteResource(); + + isStarted = true; + } + } + + void tearDown() + { + if(isFinished) + { + testObject.destroy(); + isStarted = false; + } + } } class RequestObjectTest : public TestWithMock { public: - ResourceEncapsulationTestSimulator::Ptr testObject; - RCSResourceObject::Ptr server; - RCSRemoteResourceObject::Ptr remoteObject; - - RCSResourceAttributes attr; - std::mutex mutexForCondition; std::condition_variable responseCon; @@ -52,27 +70,13 @@ protected: void SetUp() { TestWithMock::SetUp(); - - testObject = std::make_shared(); - testObject->defaultRunSimulator(); - remoteObject = testObject->getRemoteResource(); + setup(); } void TearDown() { TestWithMock::TearDown(); - if(remoteObject) - { - if(remoteObject->isCaching()) - { - remoteObject->stopCaching(); - } - if(remoteObject->isMonitoring()) - { - remoteObject->stopMonitoring(); - } - } - testObject->destroy(); + tearDown(); } public: @@ -102,7 +106,7 @@ TEST_F(RequestObjectTest, invokeRequestExpectCallwithSetter) RCSResourceAttributes att; std::shared_ptr request; - RequestObject::invokeRequest(remoteObject, RCSRequest(testObject->getResourceServer(), request), + RequestObject::invokeRequest(remoteObject, RCSRequest(testObject.getResourceServer(), request), RequestObject::RequestMethod::Set, att, setRequestCB); waitForCondition(); diff --git a/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h b/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h old mode 100644 new mode 100755 index 7bf761b..395bf21 --- a/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h +++ b/service/resource-hosting/unittest/ResourceEncapsulationTestSimulator.h @@ -25,9 +25,10 @@ #include "UnitTestHelper.h" -#include "RCSResourceObject.h" +#include "OCPlatform.h" #include "RCSDiscoveryManager.h" #include "RCSRemoteResourceObject.h" +#include "RCSResourceObject.h" #include "RCSResourceAttributes.h" #include "RCSAddress.h" @@ -37,11 +38,8 @@ using namespace testing; using namespace OIC::Service; class ResourceEncapsulationTestSimulator - : public std::enable_shared_from_this { public: - typedef std::shared_ptr Ptr; - RCSResourceObject::Ptr server; RCSRemoteResourceObject::Ptr remoteResource; @@ -73,7 +71,7 @@ public: { } private: - void onDiscoveryResource_Impl(RCSRemoteResourceObject::Ptr resourceObject) + void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject) { if (remoteResource != nullptr) { @@ -86,18 +84,10 @@ private: } remoteResource = resourceObject; + discoveryTask->cancel(); mutexForDiscovery.unlock(); } - static void onDiscoveryResource(RCSRemoteResourceObject::Ptr resourceObject, - std::weak_ptr rPtr) - { - std::shared_ptr ptr = rPtr.lock(); - if (ptr != nullptr) - { - ptr->onDiscoveryResource_Impl(resourceObject); - } - } void waitForDiscovery() { std::chrono::milliseconds interval(100); @@ -115,7 +105,7 @@ private: { while((remoteResource && !remoteResource.unique()) || (server && !server.unique())) { - std::this_thread::sleep_for(std::chrono::milliseconds{ 100 }); + std::this_thread::sleep_for(std::chrono::milliseconds{ 1000 }); } } @@ -133,11 +123,13 @@ public: waitForDiscovery(); } - void createResource() + void createResource(std::string postUri = "") { - server = RCSResourceObject::Builder(RESOURCEURI, RESOURCETYPE, RESOURCEINTERFACE) - .setDiscoverable(true).setObservable(true).build(); - server->setAttribute(ATTR_KEY, ATTR_VALUE); + HOSTED_RESOURCEURI = HOSTED_RESOURCEURI + postUri; + server = RCSResourceObject::Builder(HOSTED_RESOURCEURI + "/hosting", + RESOURCETYPE, RESOURCEINTERFACE). + setDiscoverable(true).setObservable(true).build(); + server->setAttribute(ATTR_KEY, RCSResourceAttributes::Value(ATTR_VALUE)); } void discoveryResource() @@ -151,8 +143,9 @@ public: { discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( RCSAddress::multicast(), MULTICASTURI, resourceType, - std::bind(onDiscoveryResource, std::placeholders::_1, - std::weak_ptr(shared_from_this()))); + std::bind( + &ResourceEncapsulationTestSimulator::onDiscoveryResource, + this, std::placeholders::_1)); mutexForDiscovery.lock(); } catch(std::exception & e) @@ -180,20 +173,4 @@ public: { return remoteResource; } - - void ChangeAttributeValue() - { - std::chrono::milliseconds interval(100); - if (server != nullptr) - server->setAttribute(ATTR_KEY, ATTR_VALUE + 10); - std::this_thread::sleep_for(interval); - } - - void ChangeResourceState() - { - std::chrono::milliseconds interval(400); - if (server != nullptr) - server = nullptr; - std::this_thread::sleep_for(interval); - } }; diff --git a/service/resource-hosting/unittest/ResourceHostingUnitTest.cpp b/service/resource-hosting/unittest/ResourceHostingUnitTest.cpp old mode 100644 new mode 100755 index 72b93d9..79b1d1d --- a/service/resource-hosting/unittest/ResourceHostingUnitTest.cpp +++ b/service/resource-hosting/unittest/ResourceHostingUnitTest.cpp @@ -29,6 +29,11 @@ using namespace OIC::Service; namespace { + bool isStarted = false; + bool isFinished = false; + + ResourceEncapsulationTestSimulator testObject; + void onDiscoveryResource(RCSRemoteResourceObject::Ptr) { } } @@ -44,17 +49,23 @@ protected: void SetUp() { TestWithMock::SetUp(); + if(!isStarted) + { + testObject.createResource("1"); + + isStarted = true; + } } void TearDown() { TestWithMock::TearDown(); - if (ResourceHosting::getInstance()) + if(isFinished) { - ResourceHosting::getInstance()->stopHosting(); + testObject.destroy(); + isStarted = false; } - } public: @@ -76,20 +87,28 @@ TEST(ResourceHostingSTATICMethodTest, getInstanceAllwaysSameReturnInstance) EXPECT_EQ(ResourceHosting::getInstance(), ResourceHosting::getInstance()); } -TEST_F(ResourceHostingTest, HostingFoundBeforeMakeOriginServer) +TEST_F(ResourceHostingTest, startHosting) { - ResourceEncapsulationTestSimulator::Ptr testObject - = std::make_shared(); - testObject->createResource(); - - ResourceHosting::getInstance()->startHosting(); - std::this_thread::sleep_for(std::chrono::milliseconds{1000}); + try + { + ResourceHosting::getInstance()->startHosting(); + } catch (...) + { + FAIL() << "Non-Expected Exception"; + } +} +TEST_F(ResourceHostingTest, HostingFoundBeforeMakeOriginServer) +{ std::string uri = ""; + testObject.getResourceServer()->setAttribute( + "Temperature", RCSResourceAttributes::Value((int)0)); + waitForCondition(); + mocks.OnCallFunc(onDiscoveryResource).Do( [this, &uri, &testObject](RCSRemoteResourceObject::Ptr ptr) { - if(ptr->getUri() == testObject->getHostedServerUri()) + if(ptr->getUri() == testObject.getHostedServerUri()) { uri = ptr->getUri(); discoveryTask->cancel(); @@ -99,37 +118,16 @@ TEST_F(ResourceHostingTest, HostingFoundBeforeMakeOriginServer) discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( RCSAddress::multicast(), "oic.r.resourcehosting", onDiscoveryResource); - waitForCondition(2000); + waitForCondition(); - std::string mirroredUri = { testObject->getHostedServerUri() }; - - testObject->destroy(); + std::string mirroredUri = { testObject.getHostedServerUri() }; ASSERT_EQ(mirroredUri, uri); } -TEST_F(ResourceHostingTest, startHosting) -{ - ResourceEncapsulationTestSimulator::Ptr testObject - = std::make_shared(); - testObject->createResource(); - - ResourceHosting::getInstance()->startHosting(); - std::this_thread::sleep_for(std::chrono::milliseconds{1000}); - - testObject->destroy(); -} TEST_F(ResourceHostingTest, stopHosting) { - ResourceEncapsulationTestSimulator::Ptr testObject - = std::make_shared(); - testObject->createResource(); - - ResourceHosting::getInstance()->startHosting(); - std::this_thread::sleep_for(std::chrono::milliseconds{1000}); - - testObject->destroy(); - ResourceHosting::getInstance()->stopHosting(); + isFinished = true; } diff --git a/service/resource-hosting/unittest/SConscript b/service/resource-hosting/unittest/SConscript index fd36a9d..49ea8ed 100644 --- a/service/resource-hosting/unittest/SConscript +++ b/service/resource-hosting/unittest/SConscript @@ -81,9 +81,8 @@ Alias("hosting_test", hosting_test) env.AppendTarget('hosting_test') hosting_test_env.InstallTarget(hosting_test, 'hosting_test') -#target_os = env.get('TARGET_OS') -#if target_os == 'linux': -# from tools.scons.RunTest import * -# run_test(hosting_test_env, -# '', -# 'service/resource-hosting/unittest/hosting_test') \ No newline at end of file +if env.get('TEST') == '1': + target_os = env.get('TARGET_OS') + if target_os == 'linux': + from tools.scons.RunTest import * + run_test(hosting_test_env, '', 'service/resource-hosting/unittest/hosting_test') \ No newline at end of file