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)
34       : thread_(thread), fd_(eventfd(0, 0)), is_same_thread_checked_(false) {
35     EXPECT_NE(fd_, 0);
36   }
37 
~SampleReactable()38   ~SampleReactable() { close(fd_); }
39 
OnReadReady()40   void OnReadReady() {
41     EXPECT_TRUE(thread_->IsSameThread());
42     is_same_thread_checked_ = true;
43     uint64_t val;
44     eventfd_read(fd_, &val);
45   }
46 
IsSameThreadCheckDone()47   bool IsSameThreadCheckDone() { return is_same_thread_checked_; }
48 
49   Thread* thread_;
50   int fd_;
51   bool is_same_thread_checked_;
52 };
53 
54 class ThreadTest : public ::testing::Test {
55 protected:
SetUp()56   void SetUp() override { thread = new Thread("test", Thread::Priority::NORMAL); }
57 
TearDown()58   void TearDown() override { delete thread; }
59   Thread* thread = nullptr;
60 };
61 
TEST_F(ThreadTest,just_stop_no_op)62 TEST_F(ThreadTest, just_stop_no_op) { thread->Stop(); }
63 
TEST_F(ThreadTest,thread_name)64 TEST_F(ThreadTest, thread_name) { EXPECT_EQ(thread->GetThreadName(), "test"); }
65 
TEST_F(ThreadTest,thread_to_string)66 TEST_F(ThreadTest, thread_to_string) {
67   EXPECT_NE(thread->ToString().find("test"), std::string::npos);
68 }
69 
TEST_F(ThreadTest,not_same_thread)70 TEST_F(ThreadTest, not_same_thread) { EXPECT_FALSE(thread->IsSameThread()); }
71 
TEST_F(ThreadTest,same_thread)72 TEST_F(ThreadTest, same_thread) {
73   Reactor* reactor = thread->GetReactor();
74   SampleReactable sample_reactable(thread);
75   auto* reactable = reactor->Register(
76           sample_reactable.fd_,
77           common::Bind(&SampleReactable::OnReadReady, common::Unretained(&sample_reactable)),
78           common::Closure());
79   int fd = sample_reactable.fd_;
80   int write_result = eventfd_write(fd, kCheckIsSameThread);
81   EXPECT_EQ(write_result, 0);
82   while (!sample_reactable.IsSameThreadCheckDone()) {
83     std::this_thread::yield();
84   }
85   reactor->Unregister(reactable);
86 }
87 
88 }  // namespace
89 }  // namespace os
90 }  // namespace bluetooth
91