-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathmutex_test.cpp
176 lines (139 loc) · 5.16 KB
/
mutex_test.cpp
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
#include <gtest/gtest.h>
#include <userver/engine/async.hpp>
#include <userver/engine/mutex.hpp>
#include <userver/engine/shared_mutex.hpp>
#include <userver/engine/single_consumer_event.hpp>
#include <userver/engine/single_waiting_task_mutex.hpp>
#include <userver/engine/sleep.hpp>
#include <userver/utest/utest.hpp>
using namespace std::chrono_literals;
USERVER_NAMESPACE_BEGIN
template <class T>
struct Mutex : public ::testing::Test {};
TYPED_UTEST_SUITE_P(Mutex);
TYPED_UTEST_P(Mutex, LockUnlock) {
TypeParam mutex;
mutex.lock();
mutex.unlock();
}
TYPED_UTEST_P(Mutex, LockUnlockDouble) {
TypeParam mutex;
mutex.lock();
mutex.unlock();
mutex.lock();
mutex.unlock();
}
TYPED_UTEST_P(Mutex, WaitAndCancel) {
TypeParam mutex;
std::unique_lock lock(mutex);
auto task = engine::AsyncNoSpan([&mutex]() { std::lock_guard lock(mutex); });
task.WaitFor(std::chrono::milliseconds(50));
EXPECT_FALSE(task.IsFinished());
task.RequestCancel();
task.WaitFor(std::chrono::milliseconds(50));
EXPECT_FALSE(task.IsFinished());
lock.unlock();
task.WaitFor(std::chrono::milliseconds(50));
EXPECT_TRUE(task.IsFinished());
UEXPECT_NO_THROW(task.Get());
}
TYPED_UTEST_P(Mutex, TryLock) {
TypeParam mutex;
EXPECT_TRUE(!!std::unique_lock<TypeParam>(mutex, std::try_to_lock));
EXPECT_TRUE(!!std::unique_lock<TypeParam>(mutex, std::chrono::milliseconds(10)));
EXPECT_TRUE(!!std::unique_lock<TypeParam>(mutex, std::chrono::system_clock::now()));
std::unique_lock lock(mutex);
EXPECT_FALSE(engine::AsyncNoSpan([&mutex] { return !!std::unique_lock<TypeParam>(mutex, std::try_to_lock); }).Get()
);
EXPECT_FALSE(engine::AsyncNoSpan([&mutex] {
return !!std::unique_lock<TypeParam>(mutex, std::chrono::milliseconds(10));
}).Get());
EXPECT_FALSE(engine::AsyncNoSpan([&mutex] {
return !!std::unique_lock<TypeParam>(mutex, std::chrono::system_clock::now());
}).Get());
auto long_waiter =
engine::AsyncNoSpan([&mutex] { return !!std::unique_lock<TypeParam>(mutex, utest::kMaxTestWaitTime); });
engine::Yield();
EXPECT_FALSE(long_waiter.IsFinished());
lock.unlock();
EXPECT_TRUE(long_waiter.Get());
}
namespace {
constexpr size_t kThreads = 4;
} // namespace
TYPED_UTEST_P_MT(Mutex, LockPassing, kThreads) {
static constexpr auto kTestDuration = std::chrono::milliseconds{500};
const auto test_deadline = engine::Deadline::FromDuration(kTestDuration);
TypeParam mutex;
const size_t worker_count = std::is_same_v<TypeParam, engine::SingleWaitingTaskMutex> ? 2 : 4;
const auto work = [&mutex] {
std::unique_lock lock(mutex, std::defer_lock);
ASSERT_TRUE(lock.try_lock_for(utest::kMaxTestWaitTime));
};
while (!test_deadline.IsReached()) {
std::vector<engine::TaskWithResult<void>> tasks;
for (size_t i = 0; i < worker_count; ++i) {
tasks.push_back(engine::AsyncNoSpan(work));
}
for (auto& task : tasks) task.Get();
}
}
TYPED_UTEST_P_MT(Mutex, NotifyAndDeadlineRace, 2) {
if constexpr (std::is_same_v<TypeParam, engine::SingleWaitingTaskMutex>) {
return;
}
constexpr int kTestIterationsCount = 1000;
constexpr auto kSmallWaitTime = 5us;
for (int i = 0; i < kTestIterationsCount; ++i) {
TypeParam mutex;
std::unique_lock lock(mutex);
engine::SingleConsumerEvent lock_acquired;
auto deadline_task = engine::AsyncNoSpan([&] {
if (mutex.try_lock_for(kSmallWaitTime)) {
mutex.unlock();
lock_acquired.Send();
}
});
auto no_deadline_task = engine::AsyncNoSpan([&] {
if (mutex.try_lock_until(engine::Deadline{})) {
mutex.unlock();
lock_acquired.Send();
}
});
engine::SleepFor(kSmallWaitTime);
// After this, if 'deadline_task' has not timed out yet, it should acquire
// the lock. If 'deadline_task' has timed out, 'no_deadline_task' should
// acquire the lock.
//
// A bug could happen if we wake up 'deadline_task' while it's cancelling
// itself due to a deadline. 'deadline_task' will wake up, but not lock
// the mutex.
lock.unlock();
ASSERT_TRUE(lock_acquired.WaitForEventFor(utest::kMaxTestWaitTime));
}
}
UTEST(Mutex, SampleMutex) {
/// [Sample engine::Mutex usage]
engine::Mutex mutex;
constexpr std::string_view kTestData = "Test Data";
{
std::lock_guard<engine::Mutex> lock(mutex);
// accessing data under a mutex
const auto x = kTestData;
ASSERT_EQ(kTestData, x);
}
/// [Sample engine::Mutex usage]
}
REGISTER_TYPED_UTEST_SUITE_P(
Mutex,
LockUnlock,
LockUnlockDouble,
WaitAndCancel,
TryLock,
LockPassing,
NotifyAndDeadlineRace
);
INSTANTIATE_TYPED_UTEST_SUITE_P(EngineMutex, Mutex, engine::Mutex);
INSTANTIATE_TYPED_UTEST_SUITE_P(EngineSharedMutex, Mutex, engine::SharedMutex);
INSTANTIATE_TYPED_UTEST_SUITE_P(EngineSingleWaitingTaskMutex, Mutex, engine::SingleWaitingTaskMutex);
USERVER_NAMESPACE_END