Update consumer service termination logic.
authorKIM JungYong <jyong2.kim@samsung.com>
Fri, 9 Sep 2016 02:27:00 +0000 (11:27 +0900)
committerUze Choi <uzchoi@samsung.com>
Mon, 12 Sep 2016 01:48:09 +0000 (01:48 +0000)
When consumer service termination or stopped,
memory deallocation of thread, mutex, list object is added.

unit test of NSConsumerGetProvider is added.

Change-Id: I61c30b46645d5a88eeac7f4ed6ca06d5b3d1036e
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/11615
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
service/notification/src/consumer/NSConsumerCommon.c
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerInternalTaskController.h
service/notification/src/consumer/NSConsumerScheduler.c
service/notification/unittest/NSConsumerTest.cpp
service/notification/unittest/NSProviderSimulator.h

index 3ddd7c9..d00895a 100644 (file)
@@ -98,6 +98,15 @@ bool * NSGetBoneIsStartedConsumer()
 void NSSetIsStartedConsumer(bool setValue)
 {
     * NSGetBoneIsStartedConsumer() = setValue;
+
+    if (setValue == false)
+    {
+        pthread_mutex_destroy(*NSGetStackMutex());
+        *NSGetStackMutex() = NULL;
+
+        NSOICFree(*NSGetConsumerId());
+        *NSGetConsumerId() = NULL;
+    }
 }
 
 bool NSIsStartedConsumer()
@@ -147,6 +156,8 @@ void NSProviderChanged(NSProvider * provider, NSProviderState response)
 
     NSConsumerThread * thread = NSThreadInit(NSProviderChangedFunc, (void *) data);
     NS_VERIFY_NOT_NULL_V(thread);
+
+    NSDestroyThreadHandle(thread);
 }
 
 NSSyncInfoReceivedCallback * NSGetBoneNotificationSyncCb()
@@ -177,6 +188,8 @@ void NSNotificationSync(NSSyncInfo * sync)
 
     NSConsumerThread * thread = NSThreadInit(NSNotificationSyncFunc, (void *) retSync);
     NS_VERIFY_NOT_NULL_V(thread);
+
+    NSDestroyThreadHandle(thread);
 }
 
 NSMessageReceivedCallback  * NSGetBoneMessagePostedCb()
@@ -211,6 +224,8 @@ void NSMessagePost(NSMessage * msg)
 
     NSConsumerThread * thread = NSThreadInit(NSMessagePostFunc, (void *) retMsg);
     NS_VERIFY_NOT_NULL_V(thread);
+
+    NSDestroyThreadHandle(thread);
 }
 
 NSTask * NSMakeTask(NSTaskType type, void * data)
index 1cd5772..1bb3afb 100644 (file)
@@ -45,7 +45,7 @@ typedef struct
 } NSMessageStateList;
 
 // Mutex of MessageState storage
-pthread_mutex_t * NSGetMessageListMutex();
+pthread_mutex_t ** NSGetMessageListMutex();
 void NSLockMessageListMutex();
 void NSUnlockMessageListMutex();
 
@@ -68,7 +68,7 @@ void NSSetProviderCacheList(NSCacheList * cache)
     *(NSGetProviderCacheList()) = cache;
 }
 
-void NSDestroyProviderCacheList()
+void NSDestroyInternalCachedList()
 {
     NSCacheList * cache = *(NSGetProviderCacheList());
     if (cache)
@@ -77,6 +77,10 @@ void NSDestroyProviderCacheList()
     }
 
     NSSetProviderCacheList(NULL);
+
+    NSDestroyMessageStateList();
+    pthread_mutex_destroy(*NSGetMessageListMutex());
+    *NSGetMessageListMutex() = NULL;
 }
 
 NSProvider_internal * NSProviderCacheFind(const char * providerId)
@@ -174,8 +178,6 @@ void NSCancelAllSubscription()
 
         NSConsumerPushEvent(task);
     }
-
-    NSDestroyMessageStateList();
 }
 
 void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
@@ -499,7 +501,7 @@ void NSConsumerInternalTaskProcessing(NSTask * task)
 }
 
 // implements of MessageState function
-pthread_mutex_t * NSGetMessageListMutex()
+pthread_mutex_t ** NSGetMessageListMutex()
 {
     static pthread_mutex_t * g_mutex = NULL;
     if (g_mutex == NULL)
@@ -509,19 +511,19 @@ pthread_mutex_t * NSGetMessageListMutex()
 
         pthread_mutex_init(g_mutex, NULL);
     }
-    return g_mutex;
+    return g_mutex;
 }
 
 void NSLockMessageListMutex()
 {
     NS_LOG_V(DEBUG, "%s", __func__);
-    pthread_mutex_lock(NSGetMessageListMutex());
+    pthread_mutex_lock(*NSGetMessageListMutex());
 }
 
 void NSUnlockMessageListMutex()
 {
     NS_LOG_V(DEBUG, "%s", __func__);
-    pthread_mutex_unlock(NSGetMessageListMutex());
+    pthread_mutex_unlock(*NSGetMessageListMutex());
 }
 
 NSMessageStateList * NSGetMessageStateList()
index cbe4cfa..b12265b 100644 (file)
@@ -42,7 +42,7 @@ void NSSetProviderCacheList(NSCacheList *);
 
 void NSDestroyMessageCacheList();
 
-void NSDestroyProviderCacheList();
+void NSDestroyInternalCachedList();
 
 NSMessage * NSMessageCacheFind(const char *);
 
index 170769f..c7340a9 100644 (file)
@@ -120,11 +120,14 @@ void NSConsumerMessageHandlerExit()
 
     NSConsumerListenerTermiate();
     NSCancelAllSubscription();
+
     NSThreadStop(*(NSGetMsgHandleThreadHandle()));
+    NSSetMsgHandleThreadHandle(NULL);
+
     NSDestroyQueue(*(NSGetMsgHandleQueue()));
     NSSetMsgHandleQueue(NULL);
 
-    NSDestroyProviderCacheList();
+    NSDestroyInternalCachedList();
 }
 
 void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
index d841b51..8eb2153 100644 (file)
@@ -524,6 +524,26 @@ TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
     EXPECT_EQ(NS_SYNC_DELETED, state);
 }
 
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithValidProviderId)
+{
+    NSProvider * provider = NSConsumerGetProvider(g_provider->providerId);
+    int ret = strcmp(provider->providerId, g_provider->providerId);
+    EXPECT_EQ(0, ret);
+    free(provider);
+}
+
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithInvalidProviderId)
+{
+    NSProvider * provider = NSConsumerGetProvider("123456789012345678901234567890123457");
+    EXPECT_EQ(provider, (void*)NULL);
+}
+
+TEST_F(NotificationConsumerTest, ExpectGetProviderSuccessWithNULL)
+{
+    NSProvider * provider = NSConsumerGetProvider(NULL);
+    EXPECT_EQ(provider, (void*)NULL);
+}
+
 TEST_F(NotificationConsumerTest, ExpectGetTopicListIsNULL)
 {
     NSTopicLL * currentTopics = NSConsumerGetTopicList(g_provider->providerId);
index 49d1c33..bdc44e3 100644 (file)
@@ -381,6 +381,8 @@ public:
         OC::OCPlatform::unregisterResource(m_messageHandle);
         OC::OCPlatform::unregisterResource(m_syncHandle);
         OC::OCPlatform::unregisterResource(m_topicHandle);
+        m_allowedTopicList.clear();
+        m_topicList.clear();
     }
 
     void createNotificationResource()