• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <thread>
17 #include <chrono>
18 #include <mutex>
19 #include <gtest/gtest.h>
20 #include "kv_store_thread_pool.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS::DistributedKv;
24 
25 class KvStoreThreadPoolTest : public testing::Test {
26 public:
27     static void SetUpTestCase(void);
28     static void TearDownTestCase(void);
29     void SetUp();
30     void TearDown();
31 };
32 
SetUpTestCase(void)33 void KvStoreThreadPoolTest::SetUpTestCase(void)
34 {}
35 
TearDownTestCase(void)36 void KvStoreThreadPoolTest::TearDownTestCase(void)
37 {}
38 
SetUp(void)39 void KvStoreThreadPoolTest::SetUp(void)
40 {}
41 
TearDown(void)42 void KvStoreThreadPoolTest::TearDown(void)
43 {}
44 
45 /**
46   * @tc.name: TestApplyTask001
47   * @tc.desc: test if task can be done asynchronous.
48   * @tc.type: FUNC
49   * @tc.require: AR000CQS31
50   * @tc.author: liqiao
51   */
52 HWTEST_F(KvStoreThreadPoolTest, TestApplyTask001, TestSize.Level1)
53 {
54     auto pool = KvStoreThreadPool::GetPool(8, "Task001", true);
55     int var = 0;
56     auto start = std::chrono::system_clock::now();
__anon18534f4c0102()57     pool->AddTask(KvStoreTask([&var](){
58         std::this_thread::sleep_for(std::chrono::milliseconds(100));
59         var++;
60     }));
61     auto end = std::chrono::system_clock::now();
62     std::chrono::duration<double> returnTime = end - start;
63     EXPECT_LT(returnTime.count(), 0.05);
64     EXPECT_EQ(var, 0);
65     std::this_thread::sleep_for(std::chrono::milliseconds(150));
66     EXPECT_EQ(var, 1);
67 }
68 
69 /**
70   * @tc.name: TestApplyTask002
71   * @tc.desc: test if task can be done in different thread.
72   * @tc.type: FUNC
73   * @tc.require: AR000CQS31
74   * @tc.author: liqiao
75   */
76 HWTEST_F(KvStoreThreadPoolTest, TestApplyTask002, TestSize.Level2)
77 {
78     auto pool = KvStoreThreadPool::GetPool(2, "Task002", false);
79     int var = 0;
80     std::mutex varMutex;
81     auto start = std::chrono::system_clock::now();
__anon18534f4c0202()82     KvStoreTask task([&](){
83         std::this_thread::sleep_for(std::chrono::milliseconds(500));
84         std::lock_guard<std::mutex> lock(varMutex);
85         var++;
86     });
87     for (int i = 0; i < 8; i++) {
88         pool->AddTask(KvStoreTask(task));
89     }
90     auto end = std::chrono::system_clock::now();
91     std::chrono::duration<double> returnTime = end - start;
92     EXPECT_LT(returnTime.count(), 0.1);
93     EXPECT_EQ(var, 0);
94     std::this_thread::sleep_for(std::chrono::milliseconds(700));
95     EXPECT_EQ(var, 2);
96     std::this_thread::sleep_for(std::chrono::milliseconds(500));
97     EXPECT_EQ(var, 4);
98     std::this_thread::sleep_for(std::chrono::milliseconds(500));
99     EXPECT_EQ(var, 6);
100     std::this_thread::sleep_for(std::chrono::milliseconds(500));
101     EXPECT_EQ(var, 8);
102     std::this_thread::sleep_for(std::chrono::milliseconds(500));
103     EXPECT_EQ(var, 8);
104     pool->Stop();
105 }
106 
107 /**
108   * @tc.name: TestApplyTask003
109   * @tc.desc: test whether task can be done if they are not scheduled when calling Stop().
110   * @tc.type: FUNC
111   * @tc.require: AR000CQS31
112   * @tc.author: liqiao
113   */
114 HWTEST_F(KvStoreThreadPoolTest, TestApplyTask003, TestSize.Level1)
115 {
116     auto pool = KvStoreThreadPool::GetPool(2, "Task003", false);
117     int var = 0;
118     std::mutex varMutex;
__anon18534f4c0302()119     KvStoreTask task([&](){
120         std::this_thread::sleep_for(std::chrono::milliseconds(100));
121         std::lock_guard<std::mutex> lock(varMutex);
122         var++;
123     });
124     for (int i = 0; i < 8; i++) {
125         pool->AddTask(KvStoreTask(task));
126     }
127     EXPECT_EQ(var, 0);
128     pool->Stop();
129     EXPECT_EQ(var, 8);
130 }
131