1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <atomic>
17 #include <chrono>
18 #include <gtest/gtest.h>
19 #include <thread>
20
21 #include "i_semaphore.h"
22
23 namespace {
24 using namespace testing::ext;
25
26 class SemaphoreTest : public testing::Test {
27 protected:
SetUpTestCase()28 static void SetUpTestCase() {}
TearDownTestCase()29 static void TearDownTestCase() {}
30 };
31
32 /**
33 * @tc.name: SemaphoreTest
34 * @tc.desc: CtorDtor.
35 * @tc.type: FUNC
36 */
HWTEST_F(SemaphoreTest, CtorDtor, TestSize.Level1)37 HWTEST_F(SemaphoreTest, CtorDtor, TestSize.Level1)
38 {
39 auto semaphore = GetSemaphoreFactory().Create(1);
40 EXPECT_NE(semaphore, nullptr);
41 EXPECT_TRUE(semaphore->TryWait());
42 EXPECT_FALSE(semaphore->TimedWait(1, 0));
43 }
44
45 /**
46 * @tc.name: SemaphoreTest
47 * @tc.desc: Wait.
48 * @tc.type: FUNC
49 */
HWTEST_F(SemaphoreTest, Wait, TestSize.Level1)50 HWTEST_F(SemaphoreTest, Wait, TestSize.Level1)
51 {
52 auto semaphore = GetSemaphoreFactory().Create(1);
53 ASSERT_NE(semaphore, nullptr);
54 EXPECT_TRUE(semaphore->Wait());
55 EXPECT_FALSE(semaphore->TryWait());
56 }
57
58 /**
59 * @tc.name: SemaphoreTest
60 * @tc.desc: Post.
61 * @tc.type: FUNC
62 */
HWTEST_F(SemaphoreTest, Post, TestSize.Level1)63 HWTEST_F(SemaphoreTest, Post, TestSize.Level1)
64 {
65 auto semaphore = GetSemaphoreFactory().Create(0);
66 ASSERT_NE(semaphore, nullptr);
67 EXPECT_TRUE(semaphore->Post());
68 }
69
70 /**
71 * @tc.name: SemaphoreTest
72 * @tc.desc: Post.
73 * @tc.type: FUNC
74 */
HWTEST_F(SemaphoreTest, WaitPost, TestSize.Level1)75 HWTEST_F(SemaphoreTest, WaitPost, TestSize.Level1)
76 {
77 auto readySem = GetSemaphoreFactory().Create(0);
78 auto finishSem = GetSemaphoreFactory().Create(0);
79 ASSERT_NE(readySem, nullptr);
80 ASSERT_NE(finishSem, nullptr);
81
82 auto done = std::make_shared<bool>(false);
83 ASSERT_NE(done, nullptr);
84
85 std::thread bgThread([=]() {
86 readySem->Wait();
87 *done = true;
88 finishSem->Post();
89 });
90
91 EXPECT_TRUE(readySem->Post());
92 EXPECT_TRUE(finishSem->Wait());
93 EXPECT_TRUE(*done);
94
95 bgThread.join();
96 }
97
98 /**
99 * @tc.name: SemaphoreTest
100 * @tc.desc: CreatePosixSemaphoreFactory.
101 * @tc.type: FUNC
102 */
HWTEST_F(SemaphoreTest, CreatePosixSemaphoreFactory, TestSize.Level1)103 HWTEST_F(SemaphoreTest, CreatePosixSemaphoreFactory, TestSize.Level1)
104 {
105 auto semaphore = GetSemaphoreFactory(POSIX_SEMAPHORE_FACTORY).Create(0);
106 EXPECT_NE(semaphore, nullptr);
107 }
108
109 /**
110 * @tc.name: SemaphoreTest
111 * @tc.desc: CreatePtheadSemaphoreFactory.
112 * @tc.type: FUNC
113 */
HWTEST_F(SemaphoreTest, CreatePtheadSemaphoreFactory, TestSize.Level1)114 HWTEST_F(SemaphoreTest, CreatePtheadSemaphoreFactory, TestSize.Level1)
115 {
116 auto semaphore = GetSemaphoreFactory(PTHREAD_SEMAPHORE_FACTORY).Create(0);
117 EXPECT_NE(semaphore, nullptr);
118 }
119 } // namespace