forked from spakai/connection-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadConnectionPoolTest.cpp
More file actions
56 lines (46 loc) · 1.66 KB
/
MultiThreadConnectionPoolTest.cpp
File metadata and controls
56 lines (46 loc) · 1.66 KB
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
#include "gmock/gmock.h"
#include "DummyConnection.hpp"
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
class PoolDispatchedWithMultipleThreads: public testing::Test {
public:
std::vector<std::shared_ptr<std::thread>> threads;
std::shared_ptr<DummyConnectionFactory> connection_factory;
std::shared_ptr<ConnectionPool<DummyConnection>> pool;
ConnectionPoolStats stat;
std::condition_variable wasExecuted;
std::mutex m;
unsigned int count{0};
void incrementCountAndNotify() {
std::unique_lock<std::mutex> lock(m);
++count;
wasExecuted.notify_all();
}
void waitForCountAndFailOnTimeout(unsigned int expectedCount,
const std::chrono::milliseconds& time=std::chrono::milliseconds(500)) {
std::unique_lock<std::mutex> lock(m);
ASSERT_THAT(wasExecuted.wait_for(lock, time, [&] { return expectedCount == count; }), testing::Eq(true));
}
void SetUp() override {
connection_factory = std::make_shared<DummyConnectionFactory>();
pool = std::make_shared<ConnectionPool<DummyConnection>>(200, connection_factory);
}
void TearDown() override {
for (auto& t: threads) t->join();
}
};
TEST_F(PoolDispatchedWithMultipleThreads, HoldsUpUnderClientStress) {
unsigned int NumberOfThreads{300};
for (unsigned int i{0}; i < NumberOfThreads; i++)
threads.push_back(
std::make_shared<std::thread>([&] {
auto conn = pool->borrow();
pool->return_connection(std::move(conn));
incrementCountAndNotify();
})
);
waitForCountAndFailOnTimeout(NumberOfThreads);
}