forked from spakai/connection-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionPoolTest.cpp
More file actions
74 lines (64 loc) · 2.51 KB
/
ConnectionPoolTest.cpp
File metadata and controls
74 lines (64 loc) · 2.51 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "gmock/gmock.h"
#include "DummyConnection.hpp"
class Pool: public testing::Test {
public:
std::shared_ptr<DummyConnectionFactory> connection_factory;
std::shared_ptr<ConnectionPool<DummyConnection>> pool;
ConnectionPoolStats stat;
void SetUp() override {
connection_factory = std::make_shared<DummyConnectionFactory>();
pool = std::make_shared<ConnectionPool<DummyConnection>>(2, connection_factory);
}
};
TEST_F(Pool, CheckPoolSize) {
stat = pool->getStats();
ASSERT_THAT(stat.pool_size, testing::Eq(0)); // we are using lazy population of the pool
}
TEST_F(Pool, BorrowAllConnections) {
std::unique_ptr<DummyConnection> conn1 = pool->borrow();
std::unique_ptr<DummyConnection> conn2 = pool->borrow();
stat = pool->getStats();
ASSERT_THAT(stat.borrowed_size, testing::Eq(2));
ASSERT_THAT(stat.pool_size, testing::Eq(0));
}
TEST_F(Pool, BorrowAndReleaseAllConnections) {
std::unique_ptr<DummyConnection> conn1 = pool->borrow();
std::unique_ptr<DummyConnection> conn2 = pool->borrow();
pool->return_connection(std::move(conn1));
pool->return_connection(std::move(conn2));
stat = pool->getStats();
ASSERT_THAT(stat.borrowed_size, testing::Eq(0));
ASSERT_THAT(stat.pool_size, testing::Eq(2));
}
TEST_F(Pool, BorrowAllReleaseOneandReBorrow) {
std::unique_ptr<DummyConnection> conn1 = pool->borrow();
std::unique_ptr<DummyConnection> conn2 = pool->borrow();
pool->return_connection(std::move(conn1));
std::unique_ptr<DummyConnection> conn3 = pool->borrow();
stat = pool->getStats();
ASSERT_THAT(stat.borrowed_size, testing::Eq(2));
ASSERT_THAT(stat.pool_size, testing::Eq(0));
}
TEST_F(Pool, DirtyPoolCreation) {
{
std::unique_ptr<DummyConnection> conn1 = pool->borrow();
std::unique_ptr<DummyConnection> conn2 = pool->borrow();
pool->report_broken_connection();
pool->report_broken_connection();
}
std::unique_ptr<DummyConnection> conn1 = pool->borrow();
std::unique_ptr<DummyConnection> conn2 = pool->borrow();
stat = pool->getStats();
ASSERT_THAT(stat.borrowed_size, testing::Eq(2));
ASSERT_THAT(stat.pool_size, testing::Eq(0));
}
TEST_F(Pool, BorrowOne) {
std::unique_ptr<DummyConnection> conn1 = pool->borrow();
stat = pool->getStats();
ASSERT_THAT(stat.borrowed_size, testing::Eq(1));
ASSERT_THAT(stat.pool_size, testing::Eq(0));
pool->return_connection(std::move(conn1));
stat = pool->getStats();
ASSERT_THAT(stat.borrowed_size, testing::Eq(0));
ASSERT_THAT(stat.pool_size, testing::Eq(1));
}