1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "os/thread.h"
18 
19 #include <sys/eventfd.h>
20 
21 #include "common/bind.h"
22 #include "gtest/gtest.h"
23 #include "os/reactor.h"
24 
25 namespace bluetooth {
26 namespace os {
27 namespace {
28 
29 constexpr int kCheckIsSameThread = 1;
30 
31 class SampleReactable {
32  public:
SampleReactable(Thread * thread)33   explicit SampleReactable(Thread* thread) : thread_(thread), fd_(eventfd(0, 0)), is_same_thread_checked_(false) {
34     EXPECT_NE(fd_, 0);
35   }
36 
~SampleReactable()37   ~SampleReactable() {
38     close(fd_);
39   }
40 
OnReadReady()41   void OnReadReady() {
42     EXPECT_TRUE(thread_->IsSameThread());
43     is_same_thread_checked_ = true;
44     uint64_t val;
45     eventfd_read(fd_, &val);
46   }
47 
IsSameThreadCheckDone()48   bool IsSameThreadCheckDone() {
49     return is_same_thread_checked_;
50   }
51 
52   Thread* thread_;
53   int fd_;
54   bool is_same_thread_checked_;
55 };
56 
57 class ThreadTest : public ::testing::Test {
58  protected:
SetUp()59   void SetUp() override {
60     thread = new Thread("test", Thread::Priority::NORMAL);
61   }
62 
TearDown()63   void TearDown() override {
64     delete thread;
65   }
66   Thread* thread = nullptr;
67 };
68 
TEST_F(ThreadTest,just_stop_no_op)69 TEST_F(ThreadTest, just_stop_no_op) {
70   thread->Stop();
71 }
72 
TEST_F(ThreadTest,thread_name)73 TEST_F(ThreadTest, thread_name) {
74   EXPECT_EQ(thread->GetThreadName(), "test");
75 }
76 
TEST_F(ThreadTest,thread_to_string)77 TEST_F(ThreadTest, thread_to_string) {
78   EXPECT_NE(thread->ToString().find("test"), std::string::npos);
79 }
80 
TEST_F(ThreadTest,not_same_thread)81 TEST_F(ThreadTest, not_same_thread) {
82   EXPECT_FALSE(thread->IsSameThread());
83 }
84 
TEST_F(ThreadTest,same_thread)85 TEST_F(ThreadTest, same_thread) {
86   Reactor* reactor = thread->GetReactor();
87   SampleReactable sample_reactable(thread);
88   auto* reactable = reactor->Register(
89       sample_reactable.fd_,
90       common::Bind(&SampleReactable::OnReadReady, common::Unretained(&sample_reactable)),
91       common::Closure());
92   int fd = sample_reactable.fd_;
93   int write_result = eventfd_write(fd, kCheckIsSameThread);
94   EXPECT_EQ(write_result, 0);
95   while (!sample_reactable.IsSameThreadCheckDone()) std::this_thread::yield();
96   reactor->Unregister(reactable);
97 }
98 
99 }  // namespace
100 }  // namespace os
101 }  // namespace bluetooth
102