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
base / android / java_handler_thread_unittest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/android/java_handler_thread.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/sequence_manager/sequence_manager_impl.h"
#include "base/task/task_observer.h"
#include "base/test/android/java_handler_thread_helpers.h"
#include "base/test/bind.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class JavaHandlerThreadForTest : public android::JavaHandlerThread {
public:
explicit JavaHandlerThreadForTest(
const char* name,
base::ThreadType thread_type = base::ThreadType::kDefault)
: android::JavaHandlerThread(name, thread_type) {}
using android::JavaHandlerThread::state;
using android::JavaHandlerThread::State;
};
class DummyTaskObserver : public TaskObserver {
public:
explicit DummyTaskObserver(int num_tasks)
: num_tasks_started_(0), num_tasks_processed_(0), num_tasks_(num_tasks) {}
DummyTaskObserver(int num_tasks, int num_tasks_started)
: num_tasks_started_(num_tasks_started),
num_tasks_processed_(0),
num_tasks_(num_tasks) {}
DummyTaskObserver(const DummyTaskObserver&) = delete;
DummyTaskObserver& operator=(const DummyTaskObserver&) = delete;
~DummyTaskObserver() override = default;
void WillProcessTask(const PendingTask& /* pending_task */,
bool /* was_blocked_or_low_priority */) override {
num_tasks_started_++;
EXPECT_LE(num_tasks_started_, num_tasks_);
EXPECT_EQ(num_tasks_started_, num_tasks_processed_ + 1);
}
void DidProcessTask(const PendingTask& pending_task) override {
num_tasks_processed_++;
EXPECT_LE(num_tasks_started_, num_tasks_);
EXPECT_EQ(num_tasks_started_, num_tasks_processed_);
}
int num_tasks_started() const { return num_tasks_started_; }
int num_tasks_processed() const { return num_tasks_processed_; }
private:
int num_tasks_started_;
int num_tasks_processed_;
const int num_tasks_;
};
void PostNTasks(int posts_remaining) {
if (posts_remaining > 1) {
SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, BindOnce(&PostNTasks, posts_remaining - 1));
}
}
} // namespace
class JavaHandlerThreadTest : public ::testing::Test {};
void RunTest_AbortDontRunMoreTasks(bool delayed, bool init_java_first) {
WaitableEvent test_done_event;
std::unique_ptr<android::JavaHandlerThread> java_thread;
if (init_java_first) {
java_thread = android::JavaHandlerThreadHelpers::CreateJavaFirst();
} else {
java_thread = std::make_unique<android::JavaHandlerThread>(
"JavaHandlerThreadForTesting from AbortDontRunMoreTasks");
}
java_thread->Start();
java_thread->ListenForUncaughtExceptionsForTesting();
auto target =
BindOnce(&android::JavaHandlerThreadHelpers::ThrowExceptionAndAbort,
&test_done_event);
if (delayed) {
java_thread->task_runner()->PostDelayedTask(FROM_HERE, std::move(target),
Milliseconds(10));
} else {
java_thread->task_runner()->PostTask(FROM_HERE, std::move(target));
java_thread->task_runner()->PostTask(FROM_HERE,
MakeExpectedNotRunClosure(FROM_HERE));
}
test_done_event.Wait();
java_thread->Stop();
android::ScopedJavaLocalRef<jthrowable> exception =
java_thread->GetUncaughtExceptionIfAny();
ASSERT_TRUE(
android::JavaHandlerThreadHelpers::IsExceptionTestException(exception));
}
TEST_F(JavaHandlerThreadTest, JavaExceptionAbort) {
constexpr bool delayed = false;
constexpr bool init_java_first = false;
RunTest_AbortDontRunMoreTasks(delayed, init_java_first);
}
TEST_F(JavaHandlerThreadTest, DelayedJavaExceptionAbort) {
constexpr bool delayed = true;
constexpr bool init_java_first = false;
RunTest_AbortDontRunMoreTasks(delayed, init_java_first);
}
TEST_F(JavaHandlerThreadTest, JavaExceptionAbortInitJavaFirst) {
constexpr bool delayed = false;
constexpr bool init_java_first = true;
RunTest_AbortDontRunMoreTasks(delayed, init_java_first);
}
TEST_F(JavaHandlerThreadTest, RunTasksWhileShuttingDownJavaThread) {
const int kNumPosts = 6;
DummyTaskObserver observer(kNumPosts, 1);
auto java_thread = std::make_unique<JavaHandlerThreadForTest>("test");
java_thread->Start();
sequence_manager::internal::SequenceManagerImpl* sequence_manager =
static_cast<sequence_manager::internal::SequenceManagerImpl*>(
java_thread->state()->sequence_manager.get());
java_thread->task_runner()->PostTask(
FROM_HERE, BindLambdaForTesting([&] {
sequence_manager->AddTaskObserver(&observer);
SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, MakeExpectedNotRunClosure(FROM_HERE), Days(1));
java_thread->StopSequenceManagerForTesting();
PostNTasks(kNumPosts);
}));
java_thread->JoinForTesting();
java_thread.reset();
EXPECT_EQ(kNumPosts, observer.num_tasks_started());
EXPECT_EQ(kNumPosts, observer.num_tasks_processed());
}
} // namespace base