shithub: openh264

Download patch

ref: 350d36e45942f65bb4fb61dee22a0c26f9522c87
parent: 14725d43561724d2cc4bae1e85f7206aa7d8a527
author: [email protected] <[email protected]>
date: Mon Apr 24 13:06:01 EDT 2017

add a new ut case for list usage in threadpool

--- /dev/null
+++ b/test/common/WelsTaskListTest.cpp
@@ -1,0 +1,87 @@
+#include <gtest/gtest.h>
+#include <string.h>
+#include <string>
+#include <list>
+#include <map>
+
+#include "typedefs.h"
+#include "WelsList.h"
+#include "WelsTask.h"
+#include "WelsThreadPoolTest.h"
+
+
+TEST (CThreadPoolTest, CThreadPoolTest_List) {
+  
+  CWelsList<IWelsTask>* pTaskList;
+  pTaskList = new CWelsList<IWelsTask>();
+  ASSERT_TRUE(NULL != pTaskList);
+  
+  CThreadPoolTest cThreadPoolTest;
+  CThreadPoolTest cThreadPoolTest1;
+  CSimpleTask* aTasks[2];
+  IWelsTask* pCurTask;
+  
+  int32_t  i;
+  aTasks[0] = new CSimpleTask (&cThreadPoolTest);
+  aTasks[1] = new CSimpleTask (&cThreadPoolTest1);
+  pTaskList->push_back(aTasks[0]);
+  pTaskList->push_back(aTasks[1]);
+  EXPECT_TRUE(2 == pTaskList->size());
+  
+  pCurTask = pTaskList->begin();
+  EXPECT_TRUE(pCurTask->GetSink() == &cThreadPoolTest);
+  pCurTask->GetSink()->OnTaskExecuted();//cThreadPoolTest
+  pTaskList->pop_front();
+  
+  EXPECT_TRUE(1 == pTaskList->size());
+  
+  pCurTask = pTaskList->begin();
+  EXPECT_TRUE(pCurTask->GetSink() == &cThreadPoolTest1);
+  pCurTask->GetSink()->OnTaskExecuted();//cThreadPoolTest1
+  pTaskList->pop_front();
+  
+  pTaskList->push_back(aTasks[1]);
+  EXPECT_TRUE(1 == pTaskList->size());
+  
+  pCurTask = pTaskList->begin();
+  EXPECT_TRUE(pCurTask->GetSink() == &cThreadPoolTest1);
+  pCurTask->GetSink()->OnTaskExecuted();//cThreadPoolTest1
+  pTaskList->pop_front();
+  
+  EXPECT_TRUE(1 == cThreadPoolTest.GetTaskCount());
+  EXPECT_TRUE(2 == cThreadPoolTest1.GetTaskCount());
+  
+  EXPECT_TRUE(0 == pTaskList->size());
+  for (i = 0; i < 2; i++) {
+    delete aTasks[i];
+  }
+  
+  aTasks[0] = new CSimpleTask (&cThreadPoolTest1);
+  aTasks[1] = new CSimpleTask (&cThreadPoolTest);
+  pTaskList->push_back(aTasks[0]);
+  pTaskList->push_back(aTasks[1]);
+  pCurTask = pTaskList->begin();
+  EXPECT_TRUE(pCurTask->GetSink() == &cThreadPoolTest1);
+  pCurTask->GetSink()->OnTaskExecuted();//cThreadPoolTest1:3
+  pTaskList->pop_front();
+  EXPECT_TRUE(1 == pTaskList->size());
+
+  pTaskList->push_back(aTasks[0]);
+  EXPECT_TRUE(2 == pTaskList->size());
+  
+  pCurTask = pTaskList->begin();
+  EXPECT_TRUE(pCurTask->GetSink() == &cThreadPoolTest);
+  pCurTask->GetSink()->OnTaskExecuted();//cThreadPoolTest:2
+  pTaskList->pop_front();
+  pCurTask = pTaskList->begin();
+  EXPECT_TRUE(pCurTask->GetSink() == &cThreadPoolTest1);
+  pCurTask->GetSink()->OnTaskExecuted();//cThreadPoolTest1:4
+  pTaskList->pop_front();
+  
+  EXPECT_TRUE(2 == cThreadPoolTest.GetTaskCount());
+  EXPECT_TRUE(4 == cThreadPoolTest1.GetTaskCount());
+  
+  delete pTaskList;
+}
+
+
--- a/test/common/WelsThreadPoolTest.cpp
+++ b/test/common/WelsThreadPoolTest.cpp
@@ -12,28 +12,6 @@
 
 #define  TEST_TASK_NUM  30
 
-class CSimpleTask : public IWelsTask {
- public:
-  static uint32_t id;
-
-  CSimpleTask (WelsCommon::IWelsTaskSink* pSink) : IWelsTask (pSink) {
-    m_uiID = id ++;
-  }
-
-  virtual ~CSimpleTask() {
-  }
-
-  virtual int32_t Execute() {
-    uint32_t uiSleepTime = (m_uiID > 99) ? 10 : m_uiID;
-    WelsSleep (uiSleepTime);
-    //printf ("Task %d executing\n", m_uiID);
-    return cmResultSuccess;
-  }
-
- private:
-  uint32_t m_uiID;
-};
-
 uint32_t CSimpleTask::id = 0;
 
 void* OneCallingFunc() {
--- a/test/common/WelsThreadPoolTest.h
+++ b/test/common/WelsThreadPoolTest.h
@@ -53,5 +53,27 @@
 
 
 
+class CSimpleTask : public IWelsTask {
+ public:
+  static uint32_t id;
+
+  CSimpleTask (WelsCommon::IWelsTaskSink* pSink) : IWelsTask (pSink) {
+    m_uiID = id ++;
+  }
+
+  virtual ~CSimpleTask() {
+  }
+
+  virtual int32_t Execute() {
+    uint32_t uiSleepTime = (m_uiID > 99) ? 10 : m_uiID;
+    WelsSleep (uiSleepTime);
+    //printf ("Task %d executing\n", m_uiID);
+    return cmResultSuccess;
+  }
+
+ private:
+  uint32_t m_uiID;
+};
+
 #endif
 
--- a/test/common/targets.mk
+++ b/test/common/targets.mk
@@ -3,6 +3,7 @@
 	$(COMMON_UNITTEST_SRCDIR)/CWelsListTest.cpp\
 	$(COMMON_UNITTEST_SRCDIR)/ExpandPicture.cpp\
 	$(COMMON_UNITTEST_SRCDIR)/WelsThreadPoolTest.cpp\
+	$(COMMON_UNITTEST_SRCDIR)/WelsTaskListTest.cpp\
 
 COMMON_UNITTEST_OBJS += $(COMMON_UNITTEST_CPP_SRCS:.cpp=.$(OBJ))