huangcm
2025-09-01 53d8e046ac1bf2ebe94f671983e3d3be059df91a
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright 2018 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 "message_loop_thread.h"
 
#include <condition_variable>
#include <memory>
#include <mutex>
 
#include <gtest/gtest.h>
 
#include <base/bind.h>
#include <base/threading/platform_thread.h>
#include <sys/capability.h>
#include <syscall.h>
 
using bluetooth::common::MessageLoopThread;
 
/**
 * Unit tests to verify MessageLoopThread. Must have CAP_SYS_NICE capability.
 */
class MessageLoopThreadTest : public ::testing::Test {
 public:
  void ShouldNotHappen() { FAIL() << "Should not happen"; }
 
  void GetThreadId(std::promise<base::PlatformThreadId> thread_id_promise) {
    thread_id_promise.set_value(base::PlatformThread::CurrentId());
  }
 
  void GetLinuxTid(std::promise<pid_t> tid_promise) {
    tid_promise.set_value(static_cast<pid_t>(syscall(SYS_gettid)));
  }
 
  void GetName(std::promise<std::string> name_promise) {
    char my_name[256];
    pthread_getname_np(pthread_self(), my_name, sizeof(my_name));
    name_promise.set_value(my_name);
  }
 
  void GetSchedulingPolicyAndPriority(int* scheduling_policy,
                                      int* schedule_priority,
                                      std::promise<void> execution_promise) {
    *scheduling_policy = sched_getscheduler(0);
    struct sched_param param = {};
    ASSERT_EQ(sched_getparam(0, &param), 0);
    *schedule_priority = param.sched_priority;
    execution_promise.set_value();
  }
 
  void SleepAndGetName(std::promise<std::string> name_promise, int sleep_ms) {
    std::this_thread::sleep_for(std::chrono::milliseconds(sleep_ms));
    GetName(std::move(name_promise));
  }
 
 protected:
  static bool CanSetCurrentThreadPriority() {
    struct __user_cap_header_struct linux_user_header = {
        .version = _LINUX_CAPABILITY_VERSION_3};
    struct __user_cap_data_struct linux_user_data = {};
    if (capget(&linux_user_header, &linux_user_data) != 0) {
      LOG(ERROR) << "Failed to get capability for current thread, error: "
                 << strerror(errno);
      // Log record in XML
      RecordProperty("MessageLoopThreadTestCannotGetCapabilityReason",
                     strerror(errno));
      return false;
    }
    return ((linux_user_data.permitted >> CAP_SYS_NICE) & 0x1) != 0;
  }
};
 
TEST_F(MessageLoopThreadTest, get_weak_ptr) {
  base::WeakPtr<MessageLoopThread> message_loop_thread_ptr;
  {
    MessageLoopThread message_loop_thread("test_thread");
    message_loop_thread_ptr = message_loop_thread.GetWeakPtr();
    ASSERT_NE(message_loop_thread_ptr, nullptr);
  }
  ASSERT_EQ(message_loop_thread_ptr, nullptr);
}
 
TEST_F(MessageLoopThreadTest, test_running_thread) {
  MessageLoopThread message_loop_thread("test_thread");
  message_loop_thread.StartUp();
  ASSERT_GE(message_loop_thread.GetThreadId(), 0);
  ASSERT_TRUE(message_loop_thread.IsRunning());
  message_loop_thread.ShutDown();
  ASSERT_LT(message_loop_thread.GetThreadId(), 0);
  ASSERT_FALSE(message_loop_thread.IsRunning());
}
 
TEST_F(MessageLoopThreadTest, test_not_self) {
  MessageLoopThread message_loop_thread("test_thread");
  message_loop_thread.StartUp();
  ASSERT_GE(message_loop_thread.GetThreadId(), 0);
  ASSERT_NE(message_loop_thread.GetThreadId(),
            base::PlatformThread::CurrentId());
}
 
TEST_F(MessageLoopThreadTest, test_shutdown_without_start) {
  MessageLoopThread message_loop_thread("test_thread");
  message_loop_thread.ShutDown();
  ASSERT_LT(message_loop_thread.GetThreadId(), 0);
}
 
TEST_F(MessageLoopThreadTest, test_do_in_thread_before_start) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  ASSERT_FALSE(message_loop_thread.DoInThread(
      FROM_HERE, base::Bind(&MessageLoopThreadTest::ShouldNotHappen,
                            base::Unretained(this))));
}
 
TEST_F(MessageLoopThreadTest, test_do_in_thread_after_shutdown) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  message_loop_thread.ShutDown();
  ASSERT_FALSE(message_loop_thread.DoInThread(
      FROM_HERE, base::Bind(&MessageLoopThreadTest::ShouldNotHappen,
                            base::Unretained(this))));
}
 
TEST_F(MessageLoopThreadTest, test_name) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  ASSERT_GE(message_loop_thread.GetThreadId(), 0);
  std::promise<std::string> name_promise;
  std::future<std::string> name_future = name_promise.get_future();
  message_loop_thread.DoInThread(
      FROM_HERE,
      base::BindOnce(&MessageLoopThreadTest::GetName, base::Unretained(this),
                     std::move(name_promise)));
  std::string my_name = name_future.get();
  ASSERT_EQ(name, my_name);
  ASSERT_EQ(name, message_loop_thread.GetName());
}
 
TEST_F(MessageLoopThreadTest, test_thread_id) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  base::PlatformThreadId thread_id = message_loop_thread.GetThreadId();
  ASSERT_GE(thread_id, 0);
  std::promise<base::PlatformThreadId> thread_id_promise;
  std::future<base::PlatformThreadId> thread_id_future =
      thread_id_promise.get_future();
  message_loop_thread.DoInThread(
      FROM_HERE,
      base::BindOnce(&MessageLoopThreadTest::GetThreadId,
                     base::Unretained(this), std::move(thread_id_promise)));
  base::PlatformThreadId my_thread_id = thread_id_future.get();
  ASSERT_EQ(thread_id, my_thread_id);
}
 
TEST_F(MessageLoopThreadTest, test_set_realtime_priority_fail_before_start) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  ASSERT_FALSE(message_loop_thread.EnableRealTimeScheduling());
}
 
TEST_F(MessageLoopThreadTest, test_set_realtime_priority_success) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  bool ret = message_loop_thread.EnableRealTimeScheduling();
  if (!ret) {
    if (CanSetCurrentThreadPriority()) {
      FAIL() << "Cannot set real time priority even though we have permission";
    } else {
      LOG(WARNING) << "Allowing EnableRealTimeScheduling to fail because we"
                      " don't have CAP_SYS_NICE capability";
      // Log record in XML
      RecordProperty("MessageLoopThreadTestConditionalSuccess",
                     "Mark test as success even though EnableRealTimeScheduling"
                     " failed because we don't have CAP_SYS_NICE capability");
      // Quit early since further verification is no longer needed
      return;
    }
  }
  std::promise<void> execution_promise;
  std::future<void> execution_future = execution_promise.get_future();
  int scheduling_policy = -1;
  int scheduling_priority = -1;
  message_loop_thread.DoInThread(
      FROM_HERE,
      base::BindOnce(&MessageLoopThreadTest::GetSchedulingPolicyAndPriority,
                     base::Unretained(this), &scheduling_policy,
                     &scheduling_priority, std::move(execution_promise)));
  execution_future.wait();
  ASSERT_EQ(scheduling_policy, SCHED_FIFO);
  // Internal implementation verified here
  ASSERT_EQ(scheduling_priority, 1);
  std::promise<pid_t> tid_promise;
  std::future<pid_t> tid_future = tid_promise.get_future();
  message_loop_thread.DoInThread(
      FROM_HERE,
      base::BindOnce(&MessageLoopThreadTest::GetLinuxTid,
                     base::Unretained(this), std::move(tid_promise)));
  pid_t linux_tid = tid_future.get();
  ASSERT_GT(linux_tid, 0);
  ASSERT_EQ(sched_getscheduler(linux_tid), SCHED_FIFO);
  struct sched_param param = {};
  ASSERT_EQ(sched_getparam(linux_tid, &param), 0);
  // Internal implementation verified here
  ASSERT_EQ(param.sched_priority, 1);
}
 
TEST_F(MessageLoopThreadTest, test_message_loop_null_before_start) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  ASSERT_EQ(message_loop_thread.message_loop(), nullptr);
}
 
TEST_F(MessageLoopThreadTest, test_message_loop_not_null_start) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  ASSERT_NE(message_loop_thread.message_loop(), nullptr);
}
 
TEST_F(MessageLoopThreadTest, test_message_loop_null_after_stop) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  ASSERT_NE(message_loop_thread.message_loop(), nullptr);
  message_loop_thread.ShutDown();
  ASSERT_EQ(message_loop_thread.message_loop(), nullptr);
}
 
TEST_F(MessageLoopThreadTest, test_to_string_method) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  std::string thread_string_before_start = message_loop_thread.ToString();
  ASSERT_FALSE(thread_string_before_start.empty());
  LOG(INFO) << "Before start: " << message_loop_thread;
  message_loop_thread.StartUp();
  std::string thread_string_running = message_loop_thread.ToString();
  ASSERT_FALSE(thread_string_running.empty());
  LOG(INFO) << "Running: " << message_loop_thread;
  // String representation should look different when thread is not running
  ASSERT_STRNE(thread_string_running.c_str(),
               thread_string_before_start.c_str());
  message_loop_thread.ShutDown();
  std::string thread_string_after_shutdown = message_loop_thread.ToString();
  LOG(INFO) << "After shutdown: " << message_loop_thread;
  // String representation should look the same when thread is not running
  ASSERT_STREQ(thread_string_after_shutdown.c_str(),
               thread_string_before_start.c_str());
}
 
// Verify the message loop thread will shutdown after callback finishes
TEST_F(MessageLoopThreadTest, shut_down_while_in_callback) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  std::promise<std::string> name_promise;
  std::future<std::string> name_future = name_promise.get_future();
  uint32_t delay_ms = 5;
  message_loop_thread.DoInThread(
      FROM_HERE, base::BindOnce(&MessageLoopThreadTest::SleepAndGetName,
                                base::Unretained(this), std::move(name_promise),
                                delay_ms));
  message_loop_thread.ShutDown();
  std::string my_name = name_future.get();
  ASSERT_EQ(name, my_name);
}
 
// Verify the message loop thread will shutdown after callback finishes
TEST_F(MessageLoopThreadTest, shut_down_while_in_callback_check_lock) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  message_loop_thread.DoInThread(
      FROM_HERE,
      base::BindOnce([](MessageLoopThread* thread) { thread->IsRunning(); },
                     &message_loop_thread));
  message_loop_thread.ShutDown();
}
 
// Verify multiple threads try shutdown, no deadlock/crash
TEST_F(MessageLoopThreadTest, shut_down_multi_thread) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  auto thread = std::thread(&MessageLoopThread::ShutDown, &message_loop_thread);
  message_loop_thread.ShutDown();
  thread.join();
}
 
// Verify multiple threads try startup, no deadlock/crash
TEST_F(MessageLoopThreadTest, start_up_multi_thread) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  auto thread = std::thread(&MessageLoopThread::StartUp, &message_loop_thread);
  thread.join();
}
 
// Verify multiple threads try startup/shutdown, no deadlock/crash
TEST_F(MessageLoopThreadTest, start_up_shut_down_multi_thread) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  auto thread = std::thread(&MessageLoopThread::ShutDown, &message_loop_thread);
  thread.join();
}
 
// Verify multiple threads try shutdown/startup, no deadlock/crash
TEST_F(MessageLoopThreadTest, shut_down_start_up_multi_thread) {
  std::string name = "test_thread";
  MessageLoopThread message_loop_thread(name);
  message_loop_thread.StartUp();
  message_loop_thread.ShutDown();
  auto thread = std::thread(&MessageLoopThread::StartUp, &message_loop_thread);
  thread.join();
}