• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gtest/gtest.h"
22 #include "os/reactor.h"
23 
24 namespace bluetooth {
25 namespace os {
26 namespace {
27 
28 constexpr int kCheckIsSameThread = 1;
29 
30 class SampleReactable {
31  public:
SampleReactable(Thread * thread)32   explicit SampleReactable(Thread* thread) : thread_(thread), fd_(eventfd(0, 0)), is_same_thread_checked_(false) {
33     EXPECT_NE(fd_, 0);
34   }
35 
~SampleReactable()36   ~SampleReactable() {
37     close(fd_);
38   }
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() {
48     return is_same_thread_checked_;
49   }
50 
51   Thread* thread_;
52   int fd_;
53   bool is_same_thread_checked_;
54 };
55 
56 class ThreadTest : public ::testing::Test {
57  protected:
SetUp()58   void SetUp() override {
59     thread = new Thread("test", Thread::Priority::NORMAL);
60   }
61 
TearDown()62   void TearDown() override {
63     delete thread;
64   }
65   Thread* thread = nullptr;
66 };
67 
TEST_F(ThreadTest,just_stop_no_op)68 TEST_F(ThreadTest, just_stop_no_op) {
69   thread->Stop();
70 }
71 
TEST_F(ThreadTest,thread_name)72 TEST_F(ThreadTest, thread_name) {
73   EXPECT_EQ(thread->GetThreadName(), "test");
74 }
75 
TEST_F(ThreadTest,thread_to_string)76 TEST_F(ThreadTest, thread_to_string) {
77   EXPECT_NE(thread->ToString().find("test"), std::string::npos);
78 }
79 
TEST_F(ThreadTest,not_same_thread)80 TEST_F(ThreadTest, not_same_thread) {
81   EXPECT_FALSE(thread->IsSameThread());
82 }
83 
TEST_F(ThreadTest,same_thread)84 TEST_F(ThreadTest, same_thread) {
85   Reactor* reactor = thread->GetReactor();
86   SampleReactable sample_reactable(thread);
87   auto* reactable =
88       reactor->Register(sample_reactable.fd_, std::bind(&SampleReactable::OnReadReady, &sample_reactable), nullptr);
89   int fd = sample_reactable.fd_;
90   int write_result = eventfd_write(fd, kCheckIsSameThread);
91   EXPECT_EQ(write_result, 0);
92   while (!sample_reactable.IsSameThreadCheckDone()) std::this_thread::yield();
93   reactor->Unregister(reactable);
94 }
95 
96 }  // namespace
97 }  // namespace os
98 }  // namespace bluetooth
99