• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include <chrono>
18 #include "c/thread.h"
19 #include "ffrt_inner.h"
20 #include "../common.h"
21 
22 using namespace ffrt;
23 using namespace std;
24 using namespace testing;
25 #ifdef HWTEST_TESTING_EXT_ENABLE
26 using namespace testing::ext;
27 #endif
28 
29 class CVTest : public testing::Test {
30 protected:
SetUpTestCase()31     static void SetUpTestCase()
32     {
33     }
34 
TearDownTestCase()35     static void TearDownTestCase()
36     {
37     }
38 
SetUp()39     void SetUp() override
40     {
41     }
42 
TearDown()43     void TearDown() override
44     {
45     }
46 };
47 
48 HWTEST_F(CVTest, conditonV_wait_for_test2, TestSize.Level1)
49 {
50     ffrt::condition_variable cond;
51     ffrt::mutex lock_;
52     ffrt::cv_status status;
53 
54     ffrt::submit(
__anon1727e6c60102() 55         [&]() {
56             std::unique_lock lck(lock_);
57             status = cond.wait_for(lck, 100ms);
58             EXPECT_EQ(status, ffrt::cv_status::timeout);
59         },
__anon1727e6c60302() 60         {}, {});
61 
62     ffrt::wait();
63 }
64 
65 HWTEST_F(CVTest, conditonV_wait_for_test3, TestSize.Level1)
66 {
67     ffrt::condition_variable cond;
68     ffrt::mutex lock_;
69     ffrt::cv_status status;
70 
71     std::unique_lock lck(lock_);
72     status = cond.wait_for(lck, 100ms);
73     EXPECT_EQ(status, ffrt::cv_status::timeout);
74 }
75 
76 HWTEST_F(CVTest, conditonV_nullptr_test, TestSize.Level1)
77 {
78     int ret = 0;
79 
80     ret = ffrt_cond_init(nullptr, nullptr);
81     EXPECT_NE(ret, 0);
82     ret = ffrt_cond_signal(nullptr);
83     EXPECT_NE(ret, 0);
84     ret = ffrt_cond_broadcast(nullptr);
85     EXPECT_NE(ret, 0);
86     ret = ffrt_cond_wait(nullptr, nullptr);
87     EXPECT_NE(ret, 0);
88     ret = ffrt_cond_timedwait(nullptr, nullptr, nullptr);
89     EXPECT_NE(ret, 0);
90     ffrt_cond_destroy(nullptr);
91 }
92 
93 class MutexTest : public testing::Test {
94 protected:
SetUpTestCase()95     static void SetUpTestCase()
96     {
97     }
98 
TearDownTestCase()99     static void TearDownTestCase()
100     {
101     }
102 
SetUp()103     void SetUp() override
104     {
105     }
106 
TearDown()107     void TearDown() override
108     {
109     }
110 };
111 
112 HWTEST_F(MutexTest, try_lock_test, TestSize.Level1)
113 {
114     int val = -1;
115     ffrt::mutex lock;
116     lock.lock();
117     val = lock.try_lock();
118     EXPECT_EQ(val, 0);
119     lock.unlock();
120     val = lock.try_lock();
121     EXPECT_EQ(val, 1);
122     lock.unlock();
123     lock.unlock();
124 }
125 
126 HWTEST_F(MutexTest, lock_stress_test, TestSize.Level1)
127 {
128     // trigger lazy init
__anon1727e6c60402() 129     ffrt::submit([&]() {}, {}, {});
130     ffrt::wait();
131 
132     const int N = 10;
133     const int M = 10;
134     const int J = 10;
135     ffrt::mutex lock;
136     // std::mutex lock;
137     int acc = 0;
138     for (int i = 0; i < N; ++i) {
139     ffrt::submit(
__anon1727e6c60702() 140         [&]() {
141         for (int j = 0; j < M; ++j) {
142             lock.lock();
143             acc++;
144             lock.unlock();
145         }
146         },
__anon1727e6c60802() 147         {}, {});
148     }
149 
150     for (int j = 0; j < J; ++j) {
151     lock.lock();
152     acc++;
153     lock.unlock();
154     }
155 
156     ffrt::wait();
157     EXPECT_EQ(acc, (M * N + J));
158 }
159 
160 class SleepTest : public testing::Test {
161 protected:
SetUpTestCase()162     static void SetUpTestCase()
163     {
164     }
165 
TearDownTestCase()166     static void TearDownTestCase()
167     {
168     }
169 
SetUp()170     void SetUp() override
171     {
172     }
173 
TearDown()174     void TearDown() override
175     {
176     }
177 };
178 
thd_func(void * arg)179 void* thd_func(void *arg)
180 {
181     int *counter = (int *)arg;
182     (*counter)++;
183     return arg;
184 }
185 
186 HWTEST_F(SleepTest, thread_test, TestSize.Level1)
187 {
188     int a = 0;
189     ffrt_thread_t thread;
190     ffrt_thread_create(&thread, nullptr, thd_func, &a);
191     void* result = nullptr;
192     ffrt_thread_join(thread, &result);
193     EXPECT_EQ(1, a);
194     EXPECT_EQ(&a, result);
195 }
196 
197 HWTEST_F(SleepTest, thread_test2, TestSize.Level1)
198 {
199     int a = 0;
200     ffrt_thread_t thread;
201     ffrt_thread_create(nullptr, nullptr, thd_func, &a);
202     EXPECT_EQ(0, a);
203 }
204 
205 HWTEST_F(SleepTest, thread_test3, TestSize.Level1)
206 {
207     int a = 0;
208     ffrt_thread_t thread;
209     ffrt_thread_create(&thread, nullptr, thd_func, &a);
210     void* result = nullptr;
211     ffrt_thread_join(nullptr, &result);
212     int ret = 0;
213     ret = ffrt_thread_detach(thread);
214     EXPECT_EQ(ret, 0);
215 }