From ebea15b1767e1d545a2e7ac7745216b1f0fba666 Mon Sep 17 00:00:00 2001 From: coderhyme Date: Mon, 21 Sep 2015 21:21:38 -0700 Subject: [PATCH] Fix broken tests of resource-encapsulation. DiscoveryManagerTest is moved. ResourceClientTest is modified to conform to the modified discovery manager. Change-Id: I55b1fcfa00e4a606796f44ee4c84351dfaebc4c7 Signed-off-by: coderhyme Reviewed-on: https://gerrit.iotivity.org/gerrit/2907 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../src/resourceClient/unittests/SConscript | 112 --------------------- .../unittests/DiscoveryManagerTest.cpp | 13 +-- .../unittests/ResourceClientTest.cpp | 43 ++------ .../resource-encapsulation/unittests/SConscript | 48 ++++----- 4 files changed, 40 insertions(+), 176 deletions(-) delete mode 100644 service/resource-encapsulation/src/resourceClient/unittests/SConscript rename service/resource-encapsulation/{src/resourceClient => }/unittests/DiscoveryManagerTest.cpp (93%) diff --git a/service/resource-encapsulation/src/resourceClient/unittests/SConscript b/service/resource-encapsulation/src/resourceClient/unittests/SConscript deleted file mode 100644 index badcff0..0000000 --- a/service/resource-encapsulation/src/resourceClient/unittests/SConscript +++ /dev/null @@ -1,112 +0,0 @@ -#****************************************************************** -# -# Copyright 2015 Samsung Electronics All Rights Reserved. -# -#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - -## -# ResourceClient Unit Test build script -## -import os -Import('env') - -if env.get('RELEASE'): - env.AppendUnique(CCFLAGS = ['-Os']) - env.AppendUnique(CPPDEFINES = ['NDEBUG']) -else: - env.AppendUnique(CCFLAGS = ['-g']) - -if env.get('LOGGING'): - env.AppendUnique(CPPDEFINES = ['TB_LOG']) - -# Add third party libraries -lib_env = env.Clone() -SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') - -target_os = env.get('TARGET_OS') -if target_os == 'linux': - # Verify that 'google unit test' library is installed. If not, - # get it and install it - SConscript(env.get('SRC_DIR') + '/extlibs/gtest/SConscript') - - # Verify that 'hippomocks' mocking code is installed. If not, - # get it and install it - SConscript(env.get('SRC_DIR') + '/extlibs/hippomocks.scons') - -DiscoveryManager_gtest_env = lib_env.Clone() - -###################################################################### -#unit test setting -###################################################################### -src_dir = lib_env.get('SRC_DIR') -gtest_dir = src_dir + '/extlibs/gtest/gtest-1.7.0' - -###################################################################### -# Build flags -###################################################################### -gtest = File(gtest_dir + '/lib/.libs/libgtest.a') -gtest_main = File(gtest_dir + '/lib/.libs/libgtest_main.a') - -DiscoveryManager_gtest_env.AppendUnique( - CPPPATH = [ - src_dir + '/extlibs/hippomocks-master', - src_dir + '/extlibs/gtest/gtest-1.7.0/include', - '../../src/common/utils/include', - '../../src/serverBuilder/include', - '../../src/common/primitiveResource/include', - '../../common/utils/include/', - '../../../include/', - '../../common/primitiveResource/include', - '../../../../../resource/include', - '../../common/expiryTimer/include' - ]) - -if target_os not in ['windows', 'winrt']: - DiscoveryManager_gtest_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall']) - if target_os != 'android': - DiscoveryManager_gtest_env.AppendUnique(CXXFLAGS = ['-pthread']) - DiscoveryManager_gtest_env.AppendUnique(LIBS = ['pthread']) - -DiscoveryManager_gtest_env.PrependUnique(LIBS = [ - 'rcs_client', - 'rcs_server', - 'rcs_common', - 'oc', - 'octbstack', - 'oc_logger', - 'oc_logger_core', - 'connectivity_abstraction', - gtest, - gtest_main]) - -DiscoveryManager_gtest_env.AppendUnique(LIBS = ['dl']) - -###################################################################### -# Build Test -###################################################################### -DiscoveryManager_gtest_src = env.Glob('./*.cpp') - -DiscoveryManager_test = DiscoveryManager_gtest_env.Program('DiscoveryManagerTest', DiscoveryManager_gtest_src) -Alias("DiscoveryManagerTest", DiscoveryManager_test) -env.AppendTarget('DiscoveryManager_test') - -if env.get('TEST') == '1': - target_os = env.get('TARGET_OS') - if target_os == 'linux': - from tools.scons.RunTest import * - run_test(DiscoveryManager_gtest_env, '', - 'service/resource-encapsulation/src/resourceClient/DiscoveryManagerTest') diff --git a/service/resource-encapsulation/src/resourceClient/unittests/DiscoveryManagerTest.cpp b/service/resource-encapsulation/unittests/DiscoveryManagerTest.cpp similarity index 93% rename from service/resource-encapsulation/src/resourceClient/unittests/DiscoveryManagerTest.cpp rename to service/resource-encapsulation/unittests/DiscoveryManagerTest.cpp index 1a1e8b1..0cbd045 100644 --- a/service/resource-encapsulation/src/resourceClient/unittests/DiscoveryManagerTest.cpp +++ b/service/resource-encapsulation/unittests/DiscoveryManagerTest.cpp @@ -17,19 +17,21 @@ // limitations under the License. // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -#include -#include "OCPlatform.h" + #include "UnitTestHelper.h" + #include "RCSRemoteResourceObject.h" #include "RCSDiscoveryManager.h" #include "RCSResourceObject.h" -#include "PrimitiveResource.h" +#include "RCSAddress.h" + +#include "OCPlatform.h" using namespace OIC::Service; using namespace OC::OCPlatform; constexpr char RESOURCEURI[]{ "/a/TemperatureSensor" }; -constexpr char RESOURCETYPE[]{ "Resource.Hosting" }; +constexpr char RESOURCETYPE[]{ "resource.type" }; constexpr char RESOURCEINTERFACE[]{ "oic.if.baseline" }; constexpr int DiscoveryTaskDELAYTIME = 7; @@ -46,9 +48,8 @@ public: void startDiscovery() { const std::string uri = "/oic/res"; - const std::string type = "Resource.Hosting"; discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), - uri, type, &resourceDiscovered); + uri, RESOURCETYPE, &resourceDiscovered); } void cancelDiscovery() diff --git a/service/resource-encapsulation/unittests/ResourceClientTest.cpp b/service/resource-encapsulation/unittests/ResourceClientTest.cpp index 9f62ea6..a0e5274 100644 --- a/service/resource-encapsulation/unittests/ResourceClientTest.cpp +++ b/service/resource-encapsulation/unittests/ResourceClientTest.cpp @@ -19,19 +19,20 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "UnitTestHelper.h" + #include "RCSRemoteResourceObject.h" #include "RCSDiscoveryManager.h" #include "RCSResourceObject.h" -#include "PrimitiveResource.h" -#include +#include "RCSAddress.h" +#include #include using namespace OIC::Service; using namespace OC; constexpr char RESOURCEURI[]{ "/a/TemperatureSensor" }; -constexpr char RESOURCETYPE[]{ "Resource.Hosting" }; +constexpr char RESOURCETYPE[]{ "resource.type" }; constexpr char RESOURCEINTERFACE[]{ "oic.if.baseline" }; constexpr char ATTR_KEY[]{ "Temperature" }; @@ -49,7 +50,6 @@ class RemoteResourceObjectTest: public TestWithMock public: RCSResourceObject::Ptr server; RCSRemoteResourceObject::Ptr object; - std::shared_ptr< bool > finished; public: void Proceed() @@ -68,8 +68,6 @@ protected: { TestWithMock::SetUp(); - finished = std::make_shared< bool >(false); - CreateResource(); WaitUntilDiscovered(); @@ -83,8 +81,6 @@ protected: // This method is to make sure objects disposed. WaitForPtrBeingUnique(); - - *finished = true; } private: @@ -94,21 +90,15 @@ private: server->setAttribute(ATTR_KEY, ATTR_VALUE); } - bool checkObject() - { - std::lock_guard lock{ mutexForObject }; - return object == nullptr; - } - void WaitUntilDiscovered() { - for (int i=0; i<10 && checkObject(); ++i) + for (int i=0; i<10 && !object; ++i) { const std::string uri = "/oic/res"; - const std::string type = "Resource.Hosting"; auto discoveryTask = RCSDiscoveryManager::getInstance()->discoverResourceByType( - RCSAddress::multicast(), uri, type, std::bind(resourceDiscovered, this, finished, - std::placeholders::_1)); + RCSAddress::multicast(), uri, RESOURCETYPE, + std::bind(&RemoteResourceObjectTest::resourceDiscovered, this, + std::placeholders::_1)); Wait(1000); } } @@ -121,27 +111,16 @@ private: } } - // This callback is to protect crash from crashes caused by delayed callbacks - static void resourceDiscovered(RemoteResourceObjectTest* test, - std::shared_ptr< bool > finished, RCSRemoteResourceObject::Ptr resourceObject) + void resourceDiscovered(RCSRemoteResourceObject::Ptr resourceObject) { - if (*finished) return; - - { - std::lock_guard< std::mutex > lock{ test->mutexForObject }; - - if (test->object) return; - - test->object = resourceObject; - } + object = resourceObject; - test->Proceed(); + Proceed(); } private: std::condition_variable cond; std::mutex mutex; - std::mutex mutexForObject; }; TEST_F(RemoteResourceObjectTest, GetRemoteAttributesDoesNotAllowEmptyFunction) diff --git a/service/resource-encapsulation/unittests/SConscript b/service/resource-encapsulation/unittests/SConscript index 9db0a05..67855cf 100644 --- a/service/resource-encapsulation/unittests/SConscript +++ b/service/resource-encapsulation/unittests/SConscript @@ -25,13 +25,13 @@ import os Import('env') if env.get('RELEASE'): - env.AppendUnique(CCFLAGS = ['-Os']) - env.AppendUnique(CPPDEFINES = ['NDEBUG']) + env.AppendUnique(CCFLAGS = ['-Os']) + env.AppendUnique(CPPDEFINES = ['NDEBUG']) else: - env.AppendUnique(CCFLAGS = ['-g']) + env.AppendUnique(CCFLAGS = ['-g']) if env.get('LOGGING'): - env.AppendUnique(CPPDEFINES = ['TB_LOG']) + env.AppendUnique(CPPDEFINES = ['TB_LOG']) # Add third party libraries lib_env = env.Clone() @@ -39,15 +39,15 @@ SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env') target_os = env.get('TARGET_OS') if target_os == 'linux': - # Verify that 'google unit test' library is installed. If not, - # get it and install it - SConscript(env.get('SRC_DIR') + '/extlibs/gtest/SConscript') + # Verify that 'google unit test' library is installed. If not, + # get it and install it + SConscript(env.get('SRC_DIR') + '/extlibs/gtest/SConscript') - # Verify that 'hippomocks' mocking code is installed. If not, - # get it and install it - SConscript(env.get('SRC_DIR') + '/extlibs/hippomocks.scons') + # Verify that 'hippomocks' mocking code is installed. If not, + # get it and install it + SConscript(env.get('SRC_DIR') + '/extlibs/hippomocks.scons') -ResourceClient_gtest_env = lib_env.Clone() +rcs_test_env = lib_env.Clone() ###################################################################### #unit test setting @@ -61,24 +61,21 @@ gtest_dir = src_dir + '/extlibs/gtest/gtest-1.7.0' gtest = File(gtest_dir + '/lib/.libs/libgtest.a') gtest_main = File(gtest_dir + '/lib/.libs/libgtest_main.a') -ResourceClient_gtest_env.AppendUnique( +rcs_test_env.AppendUnique( CPPPATH = [ src_dir + '/extlibs/hippomocks-master', src_dir + '/extlibs/gtest/gtest-1.7.0/include', '../include', '../src/common/utils/include', - '../src/serverBuilder/include', - '../src/common/primitiveResource/include', - '../src/common/include/expiryTimer' ]) if target_os not in ['windows', 'winrt']: - ResourceClient_gtest_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall']) + rcs_test_env.AppendUnique(CXXFLAGS = ['-std=c++0x', '-Wall']) if target_os != 'android': - ResourceClient_gtest_env.AppendUnique(CXXFLAGS = ['-pthread']) - ResourceClient_gtest_env.AppendUnique(LIBS = ['pthread']) + rcs_test_env.AppendUnique(CXXFLAGS = ['-pthread']) + rcs_test_env.AppendUnique(LIBS = ['pthread']) -ResourceClient_gtest_env.PrependUnique(LIBS = [ +rcs_test_env.PrependUnique(LIBS = [ 'rcs_client', 'rcs_server', 'rcs_common', @@ -90,20 +87,19 @@ ResourceClient_gtest_env.PrependUnique(LIBS = [ gtest, gtest_main]) -ResourceClient_gtest_env.AppendUnique(LIBS = ['dl']) +rcs_test_env.AppendUnique(LIBS = ['dl']) ###################################################################### # Build Test ###################################################################### -ResourceClient_gtest_src = env.Glob('./*.cpp') +rcs_test_src = env.Glob('./*.cpp') -ResourceClient_test = ResourceClient_gtest_env.Program('ResourceClientTest', ResourceClient_gtest_src) -Alias("ResourceClientTest", ResourceClient_test) -env.AppendTarget('ResourceClient_test') +rcs_client_test = rcs_test_env.Program('rcs_client_test', rcs_test_src) +Alias("rcs_client_test", rcs_client_test) +env.AppendTarget('rcs_client_test') if env.get('TEST') == '1': target_os = env.get('TARGET_OS') if target_os == 'linux': from tools.scons.RunTest import * - run_test(ResourceClient_gtest_env, '', - 'service/resource-encapsulation/unittests/ResourceClientTest') \ No newline at end of file + run_test(rcs_test_env, '', 'service/resource-encapsulation/unittests/rcs_client_test') \ No newline at end of file -- 2.7.4