-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathIThreadPoolTest.actor.cpp
167 lines (134 loc) · 4.78 KB
/
IThreadPoolTest.actor.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
/*
* IThreadPoolTest.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* 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.
*/
// Thread naming only works on Linux.
#if defined(__linux__)
#include "flow/IThreadPool.h"
#include <pthread.h>
#include <iostream>
#include "flow/UnitTest.h"
#include "flow/actorcompiler.h" // has to be last include
void forceLinkIThreadPoolTests() {}
struct ThreadNameReceiver final : IThreadPoolReceiver {
void init() override {}
struct GetNameAction final : TypedAction<ThreadNameReceiver, GetNameAction> {
ThreadReturnPromise<std::string> name;
double getTimeEstimate() const override { return 3.; }
};
void action(GetNameAction& a) {
pthread_t t = pthread_self();
const size_t arrayLen = 16;
char name[arrayLen];
int err = pthread_getname_np(t, name, arrayLen);
if (err != 0) {
std::cout << "Get name failed with error code: " << err << std::endl;
a.name.sendError(platform_error());
return;
}
std::string s = name;
ASSERT(a.name.isValid());
a.name.send(std::move(s));
ASSERT(!a.name.isValid());
}
};
TEST_CASE("/flow/IThreadPool/NamedThread") {
noUnseed = true;
state Reference<IThreadPool> pool = createGenericThreadPool();
pool->addThread(new ThreadNameReceiver(), "thread-foo");
// Warning: this action is a little racy with the call to `pthread_setname_np`. In practice,
// ~nothing should depend on the thread name being set instantaneously. If this test ever
// flakes, we can make `startThread` in platform a little bit more complex to clearly order
// the actions.
auto* a = new ThreadNameReceiver::GetNameAction();
auto fut = a->name.getFuture();
pool->post(a);
std::string name = wait(fut);
if (name != "thread-foo") {
std::cout << "Incorrect thread name: " << name << std::endl;
ASSERT(false);
}
wait(pool->stop());
return Void();
}
struct ThreadSafePromiseStreamSender final : IThreadPoolReceiver {
ThreadSafePromiseStreamSender(ThreadReturnPromiseStream<std::string>* notifications)
: notifications(notifications) {}
void init() override {}
struct GetNameAction final : TypedAction<ThreadSafePromiseStreamSender, GetNameAction> {
double getTimeEstimate() const override { return 3.; }
};
void action(GetNameAction& a) {
pthread_t t = pthread_self();
const size_t arrayLen = 16;
char name[arrayLen];
int err = pthread_getname_np(t, name, arrayLen);
if (err != 0) {
std::cout << "Get name failed with error code: " << err << std::endl;
notifications->sendError(platform_error());
return;
}
notifications->send(name);
}
struct FaultyAction final : TypedAction<ThreadSafePromiseStreamSender, FaultyAction> {
double getTimeEstimate() const override { return 3.; }
};
void action(FaultyAction& a) { notifications->sendError(platform_error().asInjectedFault()); }
private:
ThreadReturnPromiseStream<std::string>* notifications;
};
TEST_CASE("/flow/IThreadPool/ThreadReturnPromiseStream") {
noUnseed = true;
state std::unique_ptr<ThreadReturnPromiseStream<std::string>> notifications(
new ThreadReturnPromiseStream<std::string>());
state Reference<IThreadPool> pool = createGenericThreadPool();
pool->addThread(new ThreadSafePromiseStreamSender(notifications.get()), "thread-foo");
// Warning: this action is a little racy with the call to `pthread_setname_np`. In practice,
// ~nothing should depend on the thread name being set instantaneously. If this test ever
// flakes, we can make `startThread` in platform a little bit more complex to clearly order
// the actions.
state int num = 3;
for (int i = 0; i < num; ++i) {
auto* a = new ThreadSafePromiseStreamSender::GetNameAction();
pool->post(a);
}
state FutureStream<std::string> futs = notifications->getFuture();
state int n = 0;
while (n < num) {
std::string name = waitNext(futs);
if (name != "thread-foo") {
std::cout << "Incorrect thread name: " << name << std::endl;
ASSERT(false);
}
++n;
}
ASSERT(n == num);
auto* faultyAction = new ThreadSafePromiseStreamSender::FaultyAction();
pool->post(faultyAction);
try {
std::string name = waitNext(futs);
ASSERT(false);
} catch (Error& e) {
ASSERT(e.isInjectedFault());
}
wait(pool->stop());
return Void();
}
#else
void forceLinkIThreadPoolTests() {}
#endif