Fix RE unit test case. (#632) 03/218803/1 accepted/tizen/unified/20191201.221455 submit/tizen/20191128.075235
authorsenthil.gs@samsung.com <senthil.gs@samsung.com>
Thu, 28 Nov 2019 06:34:25 +0000 (12:04 +0530)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Thu, 28 Nov 2019 07:28:51 +0000 (16:28 +0900)
Unit test case which was failing:
RemoteResourceObjectTest.IsCachingReturnsTrueAfterStartCaching()

Reason for failure: Shared pointer reference for RCSRemoteResourceObject
was not released. Due to which, there was a problem in tearing down the test case.

This PR has the fix.
Additionally, application will receive caching callback
from RCSRemoteResourceObject only if remote resource object is available.

https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/632
(cherry-picked from e8fb156d8239ace91f9529830f4781ea16e85bd3)

Change-Id: Idf587d1f5add4360a717a6959dcc839bc7aacb9f
Signed-off-by: Senthil Kumar G S <senthil.gs@samsung.com>
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
service/resource-encapsulation/include/RCSRemoteResourceObject.h
service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp

index 05b189c..19d3e98 100644 (file)
@@ -581,6 +581,7 @@ namespace OIC
             std::vector< std::string > getInterfaces() const;
 
         private:
+            std::weak_ptr< RCSRemoteResourceObject > weakFromThis();
             std::shared_ptr< PrimitiveResource > m_primitiveResource;
             CacheID m_cacheId;
             BrokerID m_brokerId;
index 26d83f9..80c25fe 100644 (file)
@@ -98,27 +98,26 @@ namespace
     {
         SCOPE_LOG_F(DEBUG, TAG);
 
+        std::shared_ptr<RCSRemoteResourceObject> resource = resourcePtr.lock();
+        if(!resource)
+        {
+            OIC_LOG(ERROR, TAG, "Resource object is null");
+            return OC_STACK_OK;
+        }
+
         //If error code is failure then RE Cache module should
         //do clean up for caching flags, maps etc.
         if(eCode > 4)
         {
-            OIC_LOG_V(ERROR, TAG, "Error code: %d",eCode);
+            OIC_LOG_V(ERROR, TAG, "Error code: %d", eCode);
             try
             {
-                std::shared_ptr<RCSRemoteResourceObject> resource = resourcePtr.lock();
-                if(resource)
-                {
-                    resource->stopCaching();
-                }
-                else
-                {
-                    OIC_LOG(ERROR, TAG, "Resource object is null");
-                }
+                resource->stopCaching();
             }
             catch(...)
             {
                 //Exception will be thrown: stack will return OC_STACK_ERROR
-                // if it already stopped observe. This call is reqired for clearing
+                //if it already stopped observe. This call is reqired for clearing
                 //Cache manager.
                 OIC_LOG(DEBUG, TAG, "Cleared Cache");
             }
@@ -217,6 +216,11 @@ namespace OIC
             }
         }
 
+        std::weak_ptr< RCSRemoteResourceObject > RCSRemoteResourceObject::weakFromThis()
+        {
+            return shared_from_this();
+        }
+
         RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource(
                 std::shared_ptr< OC::OCResource > ocResource)
         {
@@ -340,17 +344,16 @@ namespace OIC
                         m_primitiveResource,
                         std::bind(cachingCallback, std::placeholders::_1,
                                   std::placeholders::_2, std::placeholders::_3,
-                                  std::move(cb), shared_from_this()), CACHE_METHOD::OBSERVE_ONLY,
+                                  std::move(cb), weakFromThis()), CACHE_METHOD::OBSERVE_ONLY,
                                   freq, 0);
             }
-
             else if (cb)
             {
                 m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache(
                         m_primitiveResource,
                         std::bind(cachingCallback, std::placeholders::_1,
                                 std::placeholders::_2, std::placeholders::_3,
-                                std::move(cb), shared_from_this()), CACHE_METHOD::ITERATED_GET,
+                                std::move(cb), weakFromThis()), CACHE_METHOD::ITERATED_GET,
                                 freq, 0);
             }
             else