1 #include <gtest/gtest.h>
2 #include <string.h>
3 #include <string>
4 #include <list>
5 #include <map>
6
7 #include "typedefs.h"
8 #include "WelsThreadLib.h"
9 #include "WelsThreadPool.h"
10 #include "WelsTask.h"
11 #include "WelsThreadPoolTest.h"
12
13 #define TEST_TASK_NUM 30
14
15 uint32_t CSimpleTask::id = 0;
16
OneCallingFunc(void *)17 WELS_THREAD_ROUTINE_TYPE OneCallingFunc(void *) {
18 CThreadPoolTest cThreadPoolTest;
19 CSimpleTask* aTasks[TEST_TASK_NUM];
20 CWelsThreadPool* pThreadPool = (CWelsThreadPool::AddReference());
21 if (pThreadPool == NULL)
22 return 0;
23
24 int32_t i;
25 for (i = 0; i < TEST_TASK_NUM; i++) {
26 aTasks[i] = new CSimpleTask (&cThreadPoolTest);
27 }
28
29 for (i = 0; i < TEST_TASK_NUM; i++) {
30 pThreadPool->QueueTask (aTasks[i]);
31 }
32
33 while (cThreadPoolTest.GetTaskCount() < TEST_TASK_NUM) {
34 WelsSleep (1);
35 }
36
37 for (i = 0; i < TEST_TASK_NUM; i++) {
38 delete aTasks[i];
39 }
40 pThreadPool->RemoveInstance();
41
42 return 0;
43 }
44
45
TEST(CThreadPoolTest,CThreadPoolTest)46 TEST (CThreadPoolTest, CThreadPoolTest) {
47 OneCallingFunc(NULL);
48
49 int iRet = CWelsThreadPool::SetThreadNum (8);
50 EXPECT_EQ (0, iRet);
51 EXPECT_FALSE (CWelsThreadPool::IsReferenced());
52
53 CWelsThreadPool* pThreadPool = (CWelsThreadPool::AddReference());
54 ASSERT_TRUE (pThreadPool != NULL);
55
56 EXPECT_TRUE (pThreadPool->IsReferenced());
57
58 EXPECT_EQ (8, pThreadPool->GetThreadNum());
59
60 iRet = CWelsThreadPool::SetThreadNum (4);
61 EXPECT_TRUE (0 != iRet);
62 EXPECT_EQ (8, pThreadPool->GetThreadNum());
63
64 pThreadPool->RemoveInstance();
65
66 iRet = CWelsThreadPool::SetThreadNum (4);
67 EXPECT_EQ (0, iRet);
68
69 pThreadPool = (CWelsThreadPool::AddReference());
70 EXPECT_TRUE (pThreadPool->IsReferenced());
71 EXPECT_EQ (4, pThreadPool->GetThreadNum());
72 pThreadPool->RemoveInstance();
73
74 EXPECT_FALSE (CWelsThreadPool::IsReferenced());
75 }
76
77
TEST(CThreadPoolTest,CThreadPoolTestMulti)78 TEST (CThreadPoolTest, CThreadPoolTestMulti) {
79 int iCallingNum = 10;
80 WELS_THREAD_HANDLE mThreadID[30];
81 int i = 0;
82 WELS_THREAD_ERROR_CODE rc;
83 for (i = 0; i < iCallingNum; i++) {
84 rc = WelsThreadCreate (& (mThreadID[i]), OneCallingFunc, NULL, 0);
85 ASSERT_TRUE (rc == WELS_THREAD_ERROR_OK);
86 WelsSleep (1);
87 }
88 for (i = iCallingNum; i < iCallingNum * 2; i++) {
89 rc = WelsThreadCreate (& (mThreadID[i]), OneCallingFunc, NULL, 0);
90 ASSERT_TRUE (rc == WELS_THREAD_ERROR_OK);
91 WelsSleep (1);
92 WelsThreadJoin (mThreadID[i]);
93 }
94 for (i = 0; i < iCallingNum; i++) {
95 WelsThreadJoin (mThreadID[i]);
96 }
97 for (i = iCallingNum * 2; i < iCallingNum * 3; i++) {
98 rc = WelsThreadCreate (& (mThreadID[i]), OneCallingFunc, NULL, 0);
99 ASSERT_TRUE (rc == WELS_THREAD_ERROR_OK);
100 WelsSleep (1);
101 WelsThreadJoin (mThreadID[i]);
102 }
103
104 EXPECT_FALSE (CWelsThreadPool::IsReferenced());
105 }
106
107