-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask_dispatching_framework_test.cc
170 lines (146 loc) · 6.73 KB
/
task_dispatching_framework_test.cc
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
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <utility>
#include <base/bind.h>
#include <base/time/time.h>
#include <base/threading/thread.h>
#include <base/threading/thread_task_runner_handle.h>
#include <brillo/dbus/dbus_method_response.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "libhwsec/task_dispatching_framework.h"
using ResponseSender = ::dbus::ExportedObject::ResponseSender;
using DBusMethodResponse = brillo::dbus_utils::DBusMethodResponse<>;
using ThreadSafeDBusMethodResponse = hwsec::ThreadSafeDBusMethodResponse<>;
using DBusMethodResponseCallback = std::unique_ptr<DBusMethodResponse>;
constexpr base::TimeDelta kTestTimeout = base::TimeDelta::FromSeconds(5);
class DBusMethodResponseWrapperTestBase : public testing::Test {
public:
DBusMethodResponseWrapperTestBase()
: dbus_thread_("dbus_thread"),
worker_thread_("worker_thread"),
finished_event_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
void SetUp() override {
dbus_thread_.StartAndWaitForTesting();
worker_thread_.StartAndWaitForTesting();
finished_event_.Reset();
}
// Create a mock DbusMethodResponse callback which will run |check_function|
// after it is called.
DBusMethodResponseCallback CreateMockDBusMethodResponse(
base::Closure check_function) {
// Make a fake method call.
std::shared_ptr<dbus::MethodCall> method_call =
std::make_shared<dbus::MethodCall>("com.example.Interface",
"MockMethod");
// Set a value to bypass the checks in dbus libraray.
method_call->SetSerial(5);
// TODO(crbug/1094927): Change to base::BindOnce after uprev.
ResponseSender sender = base::Bind(
[](std::shared_ptr<dbus::MethodCall> method_call, base::Closure check,
std::unique_ptr<dbus::Response> response) { check.Run(); },
method_call, check_function);
return std::make_unique<DBusMethodResponse>(method_call.get(),
std::move(sender));
}
void CheckIfDbusCallbackIsCalledOnDbusThread() {
EXPECT_EQ(base::PlatformThread::CurrentId(), dbus_thread_.GetThreadId());
finished_event_.Signal();
}
// Create the mocked DbusMethodResponse which is wrapping by
// |callback_decorator| and pass the callback to |handler| and run on the
// |thread_of_handler|.
void CreateCallbackAndCallOnHandler(
const base::Callback<DBusMethodResponseCallback(
DBusMethodResponseCallback)> callback_decorator,
base::Thread* thread_of_handler,
base::Callback<void(DBusMethodResponseCallback)> handler) {
auto checker = base::Bind(&DBusMethodResponseWrapperTestBase::
CheckIfDbusCallbackIsCalledOnDbusThread,
base::Unretained(this));
// Create the raw DbusMethodResponse callback with |checker|.
DBusMethodResponseCallback raw_callback =
CreateMockDBusMethodResponse(checker);
// Post-process the DbusMethodResponse callback with custom decorator.
DBusMethodResponseCallback final_callback =
callback_decorator.Run(std::move(raw_callback));
// Run the |handler| on thread |thread_of_handler| with customized dbus
// callback.
thread_of_handler->task_runner()->PostTask(
FROM_HERE,
base::Bind(handler, base::Passed(std::move(final_callback))));
}
// Run CreateCallbackAndCallOnHandler on dbus thread.
void DoCreateCallbackAndCallOnHandler(
const base::Callback<DBusMethodResponseCallback(
DBusMethodResponseCallback)> callback_decorator,
base::Callback<void(DBusMethodResponseCallback)> handler,
bool run_handler_on_the_different_thread) {
base::Thread* target_thread =
run_handler_on_the_different_thread ? &worker_thread_ : &dbus_thread_;
dbus_thread_.task_runner()->PostTask(
FROM_HERE,
base::Bind(
&DBusMethodResponseWrapperTestBase::CreateCallbackAndCallOnHandler,
base::Unretained(this), callback_decorator,
base::Unretained(target_thread), handler));
}
protected:
base::Thread dbus_thread_;
base::Thread worker_thread_;
base::WaitableEvent finished_event_;
};
class ThreadSafeDBusMethodResponseTest
: public DBusMethodResponseWrapperTestBase,
public ::testing::WithParamInterface<bool> {
public:
ThreadSafeDBusMethodResponseTest() : on_worker_thread_(GetParam()) {}
protected:
bool on_worker_thread_;
};
INSTANTIATE_TEST_SUITE_P(TestedOnWorkerThread,
ThreadSafeDBusMethodResponseTest,
::testing::Values(false, true));
TEST_P(ThreadSafeDBusMethodResponseTest, Return) {
auto dbus_handler = base::Bind(
[](DBusMethodResponseCallback callback) { callback->Return(); });
auto callback_decorator =
base::Bind(ThreadSafeDBusMethodResponse::MakeThreadSafe);
DoCreateCallbackAndCallOnHandler(callback_decorator, dbus_handler,
on_worker_thread_);
EXPECT_TRUE(finished_event_.TimedWait(kTestTimeout));
}
TEST_P(ThreadSafeDBusMethodResponseTest, ReplyWithErrorWithRawPointer) {
auto dbus_handler = base::Bind([](DBusMethodResponseCallback callback) {
auto error_ptr = brillo::Error::Create(FROM_HERE, "error_domain",
"error_code", "error_message");
callback->ReplyWithError(error_ptr.get());
});
auto callback_decorator =
base::Bind(ThreadSafeDBusMethodResponse::MakeThreadSafe);
DoCreateCallbackAndCallOnHandler(callback_decorator, dbus_handler,
on_worker_thread_);
EXPECT_TRUE(finished_event_.TimedWait(kTestTimeout));
}
TEST_P(ThreadSafeDBusMethodResponseTest, ReplyWithErrorWithStrings) {
auto dbus_handler = base::Bind([](DBusMethodResponseCallback callback) {
callback->ReplyWithError(FROM_HERE, "error_domain", "error_code",
"error_message");
});
auto callback_decorator =
base::Bind(ThreadSafeDBusMethodResponse::MakeThreadSafe);
DoCreateCallbackAndCallOnHandler(callback_decorator, dbus_handler,
on_worker_thread_);
EXPECT_TRUE(finished_event_.TimedWait(kTestTimeout));
}
TEST_P(ThreadSafeDBusMethodResponseTest, Destruct) {
auto dbus_handler = base::Bind([](DBusMethodResponseCallback callback) {});
auto callback_decorator =
base::Bind(ThreadSafeDBusMethodResponse::MakeThreadSafe);
DoCreateCallbackAndCallOnHandler(callback_decorator, dbus_handler,
on_worker_thread_);
EXPECT_TRUE(finished_event_.TimedWait(kTestTimeout));
}