Skip to content

Commit a4074a1

Browse files
committed
SERVER-24945 added support to ThreadPoolMock for post-initialization function on worker thread.
1 parent 610550f commit a4074a1

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

src/mongo/executor/network_interface_mock_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ namespace {
4747

4848
class NetworkInterfaceMockTest : public mongo::unittest::Test {
4949
public:
50-
NetworkInterfaceMockTest() : _net{}, _executor(&_net, 1), _tearDownCalled(false) {}
50+
NetworkInterfaceMockTest()
51+
: _net{}, _executor(&_net, 1, ThreadPoolMock::Options()), _tearDownCalled(false) {}
5152

5253
NetworkInterfaceMock& net() {
5354
return _net;

src/mongo/executor/thread_pool_mock.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
namespace mongo {
3939
namespace executor {
4040

41-
ThreadPoolMock::ThreadPoolMock(NetworkInterfaceMock* net, int32_t prngSeed)
42-
: _prng(prngSeed), _net(net) {}
41+
ThreadPoolMock::ThreadPoolMock(NetworkInterfaceMock* net, int32_t prngSeed, Options options)
42+
: _options(std::move(options)), _prng(prngSeed), _net(net) {}
4343

4444
ThreadPoolMock::~ThreadPoolMock() {
4545
stdx::unique_lock<stdx::mutex> lk(_mutex);
@@ -64,6 +64,7 @@ void ThreadPoolMock::startup() {
6464
invariant(!_worker.joinable());
6565
_started = true;
6666
_worker = stdx::thread([this] {
67+
_options.onCreateThread();
6768
stdx::unique_lock<stdx::mutex> lk(_mutex);
6869
consumeTasks(&lk);
6970
});

src/mongo/executor/thread_pool_mock.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,20 @@ class NetworkInterfaceMock;
4949
*/
5050
class ThreadPoolMock final : public ThreadPoolInterface {
5151
public:
52+
/**
53+
* Structure used to configure an instance of ThreadPoolMock.
54+
*/
55+
struct Options {
56+
// This function is run before the worker thread begins consuming tasks.
57+
using OnCreateThreadFn = stdx::function<void()>;
58+
OnCreateThreadFn onCreateThread = []() {};
59+
};
60+
5261
/**
5362
* Create an instance that interlocks with "net". "prngSeed" seeds the pseudorandom number
5463
* generator that is used to determine which schedulable task runs next.
5564
*/
56-
ThreadPoolMock(NetworkInterfaceMock* net, int32_t prngSeed);
65+
ThreadPoolMock(NetworkInterfaceMock* net, int32_t prngSeed, Options options);
5766
~ThreadPoolMock();
5867

5968
void startup() override;
@@ -64,6 +73,9 @@ class ThreadPoolMock final : public ThreadPoolInterface {
6473
private:
6574
void consumeTasks(stdx::unique_lock<stdx::mutex>* lk);
6675

76+
// These are the options with which the pool was configured at construction time.
77+
const Options _options;
78+
6779
stdx::mutex _mutex;
6880
stdx::thread _worker;
6981
std::vector<Task> _tasks;

src/mongo/executor/thread_pool_task_executor_test_fixture.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,20 @@ namespace mongo {
3737
namespace executor {
3838

3939
std::unique_ptr<ThreadPoolTaskExecutor> makeThreadPoolTestExecutor(
40-
std::unique_ptr<NetworkInterfaceMock> net) {
40+
std::unique_ptr<NetworkInterfaceMock> net, ThreadPoolMock::Options options) {
4141
auto netPtr = net.get();
42-
return stdx::make_unique<ThreadPoolTaskExecutor>(stdx::make_unique<ThreadPoolMock>(netPtr, 1),
43-
std::move(net));
42+
return stdx::make_unique<ThreadPoolTaskExecutor>(
43+
stdx::make_unique<ThreadPoolMock>(netPtr, 1, std::move(options)), std::move(net));
44+
}
45+
46+
ThreadPoolMock::Options ThreadPoolExecutorTest::makeThreadPoolMockOptions() const {
47+
return ThreadPoolMock::Options();
4448
}
4549

4650
std::unique_ptr<TaskExecutor> ThreadPoolExecutorTest::makeTaskExecutor(
4751
std::unique_ptr<NetworkInterfaceMock> net) {
48-
return makeThreadPoolTestExecutor(std::move(net));
52+
auto options = makeThreadPoolMockOptions();
53+
return makeThreadPoolTestExecutor(std::move(net), std::move(options));
4954
}
5055

5156
} // namespace executor

src/mongo/executor/thread_pool_task_executor_test_fixture.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "mongo/executor/network_interface_mock.h"
3434
#include "mongo/executor/task_executor_test_fixture.h"
35+
#include "mongo/executor/thread_pool_mock.h"
3536
#include "mongo/executor/thread_pool_task_executor.h"
3637

3738
namespace mongo {
@@ -41,13 +42,15 @@ namespace executor {
4142
* Makes a new ThreadPoolTaskExecutor for use in unit tests.
4243
*/
4344
std::unique_ptr<ThreadPoolTaskExecutor> makeThreadPoolTestExecutor(
44-
std::unique_ptr<NetworkInterfaceMock> net);
45+
std::unique_ptr<NetworkInterfaceMock> net,
46+
executor::ThreadPoolMock::Options options = executor::ThreadPoolMock::Options());
4547

4648
/**
4749
* Useful fixture class for tests that use a ThreadPoolTaskExecutor.
4850
*/
4951
class ThreadPoolExecutorTest : public TaskExecutorTest {
5052
private:
53+
virtual ThreadPoolMock::Options makeThreadPoolMockOptions() const;
5154
std::unique_ptr<TaskExecutor> makeTaskExecutor(
5255
std::unique_ptr<NetworkInterfaceMock> net) override;
5356
};

0 commit comments

Comments
 (0)