-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter_tests.cpp
More file actions
132 lines (105 loc) · 4.66 KB
/
Copy pathadapter_tests.cpp
File metadata and controls
132 lines (105 loc) · 4.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
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
#include "test_utils.hpp"
#include "universal_connection_pool/adapters/fake.hpp"
#include "universal_connection_pool/adapters.hpp"
#include "universal_connection_pool/connection_pool.hpp"
#include <atomic>
#include <chrono>
#include <string>
namespace {
struct adapter_client {
std::string endpoint;
bool healthy = true;
int reset_count = 0;
};
struct adapter_traits {
static adapter_client create(const ucp::endpoint_config& endpoint) {
return adapter_client{endpoint.id, true, 0};
}
static void reset(adapter_client& client) {
++client.reset_count;
}
static void close(adapter_client& client) {
client.healthy = false;
}
static ucp::health_result check(adapter_client& client) {
return client.healthy ? ucp::health_result::healthy()
: ucp::health_result::broken("adapter client unhealthy");
}
static std::string describe(const adapter_client& client) {
return "adapter@" + client.endpoint;
}
};
} // namespace
UCP_TEST(AdapterTraits_CreateFactoryAndHealthChecker_WorkWithPool) {
ucp::pool_options options;
options.max_size = 1;
options.reaper_interval = std::chrono::milliseconds(0);
options.endpoints = {ucp::endpoint_config{.id = "adapter-endpoint"}};
auto factory = ucp::make_adapter_factory<adapter_client, adapter_traits>();
auto checker = ucp::make_adapter_health_checker<adapter_client, adapter_traits>();
ucp::connection_pool<adapter_client> pool(factory, checker, options);
{
auto client = pool.borrow();
UCP_EXPECT_EQ(client->endpoint, std::string("adapter-endpoint"));
UCP_EXPECT_TRUE(client->healthy);
}
const auto stats = pool.stats();
UCP_EXPECT_EQ(stats.returned_total, 1u);
UCP_EXPECT_EQ(stats.idle_connections, 1u);
}
UCP_TEST(AdapterMetadata_RegistryGroupsAdaptersByKindAndMaturity) {
const auto registry = ucp::adapters::fake::registry();
const auto* database = registry.find("fake_database");
UCP_EXPECT_TRUE(database != nullptr);
UCP_EXPECT_EQ(database->name, std::string("fake_database"));
UCP_EXPECT_EQ(static_cast<int>(database->kind), static_cast<int>(ucp::adapter_kind::database));
UCP_EXPECT_EQ(static_cast<int>(database->maturity),
static_cast<int>(ucp::adapter_maturity::experimental));
UCP_EXPECT_TRUE(database->contract.reusable);
UCP_EXPECT_TRUE(database->contract.resettable);
UCP_EXPECT_TRUE(!database->not_recommended_for.empty());
const auto stable = registry.by_maturity(ucp::adapter_maturity::stable);
UCP_EXPECT_EQ(stable.size(), 1u);
UCP_EXPECT_EQ(stable.front().name, std::string("fake_http_keepalive"));
const auto devices = registry.by_kind(ucp::adapter_kind::device);
UCP_EXPECT_EQ(devices.size(), 1u);
UCP_EXPECT_FALSE(devices.front().contract.reusable);
}
UCP_TEST(AdapterTraits_ApplyProtocolContractAndResetCallbacks) {
using traits = ucp::adapters::fake::database_traits;
auto options = ucp::apply_adapter_contract<traits>(ucp::pool_options{});
options.max_size = 1;
options.reaper_interval = std::chrono::milliseconds(0);
options.endpoints = {ucp::endpoint_config{.id = "db-primary"}};
auto factory = ucp::make_adapter_factory<ucp::adapters::fake::client, traits>();
auto checker = ucp::make_adapter_health_checker<ucp::adapters::fake::client, traits>();
ucp::connection_pool<ucp::adapters::fake::client> pool(factory, checker, options);
{
auto client = pool.borrow();
client->transaction_open = true;
}
const auto stats = pool.stats();
UCP_EXPECT_EQ(stats.protocol_reset_total, 1u);
UCP_EXPECT_EQ(stats.idle_connections, 1u);
auto reused = pool.borrow();
UCP_EXPECT_FALSE(reused->transaction_open);
UCP_EXPECT_EQ(reused->reset_count, 1);
}
UCP_TEST(AdapterTraits_InspectReturnCanQuarantineAdapterConnection) {
using traits = ucp::adapters::fake::http_keepalive_traits;
auto options = ucp::apply_adapter_contract<traits>(ucp::pool_options{});
options.max_size = 1;
options.reaper_interval = std::chrono::milliseconds(0);
options.endpoints = {ucp::endpoint_config{.id = "api"}};
auto factory = ucp::make_adapter_factory<ucp::adapters::fake::client, traits>();
auto checker = ucp::make_adapter_health_checker<ucp::adapters::fake::client, traits>();
ucp::connection_pool<ucp::adapters::fake::client> pool(factory, checker, options);
{
auto client = pool.borrow();
client->pipeline_dirty = true;
}
const auto stats = pool.stats();
UCP_EXPECT_EQ(stats.protocol_quarantine_total, 1u);
UCP_EXPECT_EQ(stats.protocol_close_total, 1u);
UCP_EXPECT_EQ(stats.idle_connections, 0u);
}