1 /*
2  * Copyright (C) 2024 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 "block_queue.h"
17 
18 #include <gtest/gtest.h>
19 #include <gtest/hwext/gtest-multithread.h>
20 #include <unistd.h>
21 
22 #include <cinttypes>
23 #include <sstream>
24 #include <thread>
25 
26 #include "global.h"
27 
28 namespace OHOS {
29 namespace MiscServices {
30 using namespace testing::ext;
31 using namespace testing::mt;
32 using namespace std::chrono;
33 class ImfBlockQueueTest : public testing::Test {
34 public:
35     static constexpr int32_t MAX_WAIT_TIME = 5000;
36     static constexpr int32_t EACH_THREAD_CIRCULATION_TIME = 100;
37     static void SetUpTestCase(void);
38     static void TearDownTestCase(void);
39     void SetUp();
40     void TearDown();
41     static void TestImfBlockQueue();
42     static int64_t GetThreadId();
43     static bool timeout_;
44 
45 private:
46     static BlockQueue<std::chrono::system_clock::time_point> timeQueue_;
47 };
48 BlockQueue<std::chrono::system_clock::time_point> ImfBlockQueueTest::timeQueue_{ MAX_WAIT_TIME };
49 bool ImfBlockQueueTest::timeout_{ false };
SetUpTestCase(void)50 void ImfBlockQueueTest::SetUpTestCase(void)
51 {
52 }
53 
TearDownTestCase(void)54 void ImfBlockQueueTest::TearDownTestCase(void)
55 {
56 }
57 
SetUp()58 void ImfBlockQueueTest::SetUp()
59 {
60 }
61 
TearDown()62 void ImfBlockQueueTest::TearDown()
63 {
64 }
65 
GetThreadId()66 int64_t ImfBlockQueueTest::GetThreadId()
67 {
68     std::thread::id id = std::this_thread::get_id();
69     std::ostringstream oss;
70     oss << id;
71     std::string idStr = oss.str();
72     return atol(idStr.c_str());
73 }
74 
TestImfBlockQueue()75 void ImfBlockQueueTest::TestImfBlockQueue()
76 {
77     for (int32_t i = 0; i < EACH_THREAD_CIRCULATION_TIME; i++) {
78         if (timeout_) {
79             break;
80         }
81         auto time = std::chrono::system_clock::now();
82         timeQueue_.Push(time);
83         int64_t start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
84         timeQueue_.Wait(time);
85         int64_t end = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
86         auto consume = end - start;
87         auto threadId = GetThreadId();
88         IMSA_HILOGI("consume:%{public}" PRId64 ",threadId:%{public}" PRId64 "", consume, threadId);
89         if (consume >= MAX_WAIT_TIME) {
90             timeout_ = true;
91         }
92         timeQueue_.Pop();
93     }
94 }
95 
96 /**
97  * @tc.name: blockQueueTest_001
98  * @tc.desc:
99  * @tc.type: FUNC
100  * @tc.require:
101  * @tc.author: chenyu
102  */
103 HWTEST_F(ImfBlockQueueTest, blockQueueTest_001, TestSize.Level0)
104 {
105     IMSA_HILOGI("ImfBlockQueueTest blockQueueTest_001 START");
106     SET_THREAD_NUM(5);
107     GTEST_RUN_TASK(TestImfBlockQueue);
108     EXPECT_FALSE(timeout_);
109 }
110 } // namespace MiscServices
111 } // namespace OHOS
112