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 "log.h"
17 #include "uv_status.h"
18 #include <random>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 using namespace testing::ext;
23 using namespace testing;
24
25 namespace Hdc {
26 class HandleTest : public testing::Test {
27 public:
HandleTest()28 HandleTest() {}
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31 void SetUp();
32 void TearDown();
33 public:
34 uv_loop_t mLoop;
35 uv_idle_t mIdle;
36 LoopStatus *mLoopStatus;
37 unsigned int mCallDuration;
38 int mCycles;
39 };
40
IdleCallBack(uv_idle_t * handle)41 static void IdleCallBack(uv_idle_t* handle)
42 {
43 HandleTest *ht = (HandleTest *)handle->data;
44 static int cycles = 0;
45 CALLSTAT_GUARD(*(ht->mLoopStatus), &(ht->mLoop), "IdleCallBack");
46
47 uv_sleep(ht->mCallDuration);
48 if (cycles++ > ht->mCycles) {
49 uv_stop(&(ht->mLoop));
50 }
51 }
52
SetUpTestCase()53 void HandleTest::SetUpTestCase() { }
54
TearDownTestCase()55 void HandleTest::TearDownTestCase() {}
56
SetUp()57 void HandleTest::SetUp()
58 {
59 mCallDuration = 1 * MS_PER_SEC;
60 uv_loop_init(&mLoop);
61 mIdle.data = this;
62 uv_idle_init(&mLoop, &mIdle);
63 uv_idle_start(&mIdle, IdleCallBack);
64 StartLoopMonitor();
65 mLoopStatus = new LoopStatus(&mLoop, "HandleTestLoop");
66 }
67
TearDown()68 void HandleTest::TearDown()
69 {
70 delete mLoopStatus;
71 mLoopStatus = nullptr;
72 }
73
74 HWTEST_F(HandleTest, Handle_NoHungTest_001, TestSize.Level0)
75 {
76 mCycles = 5;
77 mCallDuration = MS_PER_SEC / 2;
78 ASSERT_NO_FATAL_FAILURE(uv_run(&mLoop, UV_RUN_DEFAULT));
79 }
80 HWTEST_F(HandleTest, Handle_HungTest_001, TestSize.Level0)
81 {
82 mCycles = 1;
83 mCallDuration = 6 * MS_PER_SEC;
84 ASSERT_NO_FATAL_FAILURE(uv_run(&mLoop, UV_RUN_DEFAULT));
85 }
86
87 } // namespace Hdc
88