• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
TearDownTestCase(void)52 void ImfBlockQueueTest::TearDownTestCase(void) { }
53 
SetUp()54 void ImfBlockQueueTest::SetUp() { }
55 
TearDown()56 void ImfBlockQueueTest::TearDown() { }
57 
GetThreadId()58 int64_t ImfBlockQueueTest::GetThreadId()
59 {
60     std::thread::id id = std::this_thread::get_id();
61     std::ostringstream oss;
62     oss << id;
63     std::string idStr = oss.str();
64     return atol(idStr.c_str());
65 }
66 
TestImfBlockQueue()67 void ImfBlockQueueTest::TestImfBlockQueue()
68 {
69     for (int32_t i = 0; i < EACH_THREAD_CIRCULATION_TIME; i++) {
70         if (timeout_) {
71             break;
72         }
73         auto time = std::chrono::system_clock::now();
74         timeQueue_.Push(time);
75         int64_t start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
76         timeQueue_.Wait(time);
77         int64_t end = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
78         auto consume = end - start;
79         auto threadId = GetThreadId();
80         IMSA_HILOGI("consume:%{public}" PRId64 ",threadId:%{public}" PRId64 "", consume, threadId);
81         if (consume >= MAX_WAIT_TIME) {
82             timeout_ = true;
83         }
84         timeQueue_.Pop();
85     }
86 }
87 
88 /**
89  * @tc.name: blockQueueTest_001
90  * @tc.desc:
91  * @tc.type: FUNC
92  * @tc.require:
93  * @tc.author: chenyu
94  */
95 HWTEST_F(ImfBlockQueueTest, blockQueueTest_001, TestSize.Level1)
96 {
97     IMSA_HILOGI("ImfBlockQueueTest blockQueueTest_001 START");
98     SET_THREAD_NUM(5);
99     GTEST_RUN_TASK(TestImfBlockQueue);
100     EXPECT_FALSE(timeout_);
101 }
102 } // namespace MiscServices
103 } // namespace OHOS
104