aboutsummaryrefslogtreecommitdiff
path: root/gd/os/linux_generic/thread_unittest.cc
blob: f0b8f246b6fba51cd8899372613ab822c5862803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "os/thread.h"

#include <sys/eventfd.h>

#include "common/bind.h"
#include "gtest/gtest.h"
#include "os/reactor.h"

namespace bluetooth {
namespace os {
namespace {

constexpr int kCheckIsSameThread = 1;

class SampleReactable {
 public:
  explicit SampleReactable(Thread* thread) : thread_(thread), fd_(eventfd(0, 0)), is_same_thread_checked_(false) {
    EXPECT_NE(fd_, 0);
  }

  ~SampleReactable() {
    close(fd_);
  }

  void OnReadReady() {
    EXPECT_TRUE(thread_->IsSameThread());
    is_same_thread_checked_ = true;
    uint64_t val;
    eventfd_read(fd_, &val);
  }

  bool IsSameThreadCheckDone() {
    return is_same_thread_checked_;
  }

  Thread* thread_;
  int fd_;
  bool is_same_thread_checked_;
};

class ThreadTest : public ::testing::Test {
 protected:
  void SetUp() override {
    thread = new Thread("test", Thread::Priority::NORMAL);
  }

  void TearDown() override {
    delete thread;
  }
  Thread* thread = nullptr;
};

TEST_F(ThreadTest, just_stop_no_op) {
  thread->Stop();
}

TEST_F(ThreadTest, thread_name) {
  EXPECT_EQ(thread->GetThreadName(), "test");
}

TEST_F(ThreadTest, thread_to_string) {
  EXPECT_NE(thread->ToString().find("test"), std::string::npos);
}

TEST_F(ThreadTest, not_same_thread) {
  EXPECT_FALSE(thread->IsSameThread());
}

TEST_F(ThreadTest, same_thread) {
  Reactor* reactor = thread->GetReactor();
  SampleReactable sample_reactable(thread);
  auto* reactable =
      reactor->Register(sample_reactable.fd_,
                        common::Bind(&SampleReactable::OnReadReady, common::Unretained(&sample_reactable)), Closure());
  int fd = sample_reactable.fd_;
  int write_result = eventfd_write(fd, kCheckIsSameThread);
  EXPECT_EQ(write_result, 0);
  while (!sample_reactable.IsSameThreadCheckDone()) std::this_thread::yield();
  reactor->Unregister(reactable);
}

}  // namespace
}  // namespace os
}  // namespace bluetooth