-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_tests.cpp
More file actions
233 lines (190 loc) · 8.45 KB
/
Copy pathstream_tests.cpp
File metadata and controls
233 lines (190 loc) · 8.45 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "test_utils.hpp"
#include "universal_connection_pool/connection_pool.hpp"
#include <atomic>
#include <chrono>
#include <string>
namespace {
struct stream_connection {
int id = 0;
std::string endpoint_id;
bool open = true;
int reset_count = 0;
};
ucp::connection_factory<stream_connection> make_stream_factory(std::atomic<int>& created,
std::atomic<int>& closed) {
ucp::connection_factory<stream_connection> factory;
factory.create_for_endpoint = [&](const ucp::endpoint_config& endpoint) {
return stream_connection{++created, endpoint.id, true, 0};
};
factory.reset = [](stream_connection& connection) {
++connection.reset_count;
};
factory.close = [&](stream_connection& connection) {
connection.open = false;
++closed;
};
return factory;
}
ucp::pool_options stream_options(std::size_t max_size, std::size_t max_streams) {
ucp::pool_options options;
options.max_size = max_size;
options.reaper_interval = std::chrono::milliseconds(0);
options.borrow_timeout = std::chrono::milliseconds(50);
options.streams.max_streams_per_connection = max_streams;
options.endpoints = {ucp::endpoint_config{.id = "grpc-a"}};
return options;
}
} // namespace
UCP_TEST(StreamBorrow_AllowsMultipleLeasesOnOnePhysicalConnection) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed),
stream_options(1, 2));
auto first = pool.borrow_stream();
auto second = pool.borrow_stream();
UCP_EXPECT_EQ(first->id, second->id);
UCP_EXPECT_EQ(created.load(), 1);
UCP_EXPECT_EQ(pool.stats().active_connections, 1u);
UCP_EXPECT_EQ(pool.stats().active_streams, 2u);
UCP_EXPECT_EQ(pool.stats().borrowed_stream_total, 2u);
UCP_EXPECT_EQ(pool.stats().wait_time_sample_total, 2u);
UCP_EXPECT_EQ(pool.stats().wait_time_histogram.count, 2u);
UCP_EXPECT_TRUE(pool.dump_json().find("\"active_streams\":2") != std::string::npos);
UCP_EXPECT_TRUE(pool.dump_json().find("\"wait_time_sample_total\":2") != std::string::npos);
UCP_EXPECT_TRUE(pool.to_prometheus("streams").find("ucp_active_streams") != std::string::npos);
UCP_EXPECT_TRUE(pool.to_prometheus("streams").find("ucp_wait_time_sample_total") !=
std::string::npos);
auto third = pool.borrow_stream_for(std::chrono::milliseconds(10));
UCP_EXPECT_FALSE(third.has_value());
first.reset();
UCP_EXPECT_EQ(pool.stats().active_connections, 1u);
UCP_EXPECT_EQ(pool.stats().active_streams, 1u);
UCP_EXPECT_EQ(pool.stats().idle_connections, 0u);
second.reset();
UCP_EXPECT_EQ(pool.stats().active_connections, 0u);
UCP_EXPECT_EQ(pool.stats().active_streams, 0u);
UCP_EXPECT_EQ(pool.stats().idle_connections, 1u);
UCP_EXPECT_EQ(pool.stats().returned_stream_total, 2u);
}
UCP_TEST(StreamBorrowForZeroTimeout_UsesExistingStreamSlot) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed),
stream_options(1, 2));
auto first = pool.borrow_stream();
auto second = pool.borrow_stream_for(std::chrono::milliseconds(0));
UCP_EXPECT_TRUE(second.has_value());
UCP_EXPECT_EQ((*second)->id, first->id);
UCP_EXPECT_EQ(created.load(), 1);
UCP_EXPECT_EQ(pool.stats().active_streams, 2u);
}
UCP_TEST(StreamBorrowForZeroTimeout_UsesIdlePhysicalConnectionWithoutCreating) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed),
stream_options(1, 2));
int physical_id = 0;
{
auto first = pool.borrow_stream();
physical_id = first->id;
}
UCP_EXPECT_EQ(pool.stats().idle_connections, 1u);
auto second = pool.borrow_stream_for(std::chrono::milliseconds(0));
UCP_EXPECT_TRUE(second.has_value());
UCP_EXPECT_EQ((*second)->id, physical_id);
UCP_EXPECT_EQ(created.load(), 1);
UCP_EXPECT_EQ(pool.stats().active_streams, 1u);
}
UCP_TEST(StreamBorrow_DoesNotShareActivePhysicalConnectionAcrossAffinityKeys) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
auto options = stream_options(1, 2);
options.locality.policy = ucp::locality_policy::prefer_warm;
options.locality.migration = ucp::migration_policy::allow;
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed), options);
ucp::borrow_options key_a;
key_a.affinity_key = "tenant-a";
auto first = pool.borrow_stream(key_a);
ucp::borrow_options key_b;
key_b.affinity_key = "tenant-b";
auto different_key = pool.try_borrow_stream(key_b);
UCP_EXPECT_FALSE(different_key.has_value());
auto same_key = pool.try_borrow_stream(key_a);
UCP_EXPECT_TRUE(same_key.has_value());
UCP_EXPECT_EQ((*same_key)->id, first->id);
UCP_EXPECT_EQ(pool.stats().warm_reuse_total, 1u);
UCP_EXPECT_EQ(pool.stats().locality_rejected_total, 1u);
}
UCP_TEST(StreamBorrow_PinTokenDoesNotJoinUnpinnedActivePhysicalConnection) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed),
stream_options(1, 2));
auto first = pool.borrow_stream();
ucp::borrow_options pinned_options;
pinned_options.pin_token = "session-1";
auto pinned = pool.try_borrow_stream(pinned_options);
UCP_EXPECT_FALSE(pinned.has_value());
UCP_EXPECT_EQ(pool.stats().active_streams, 1u);
UCP_EXPECT_EQ(created.load(), 1);
}
UCP_TEST(StreamBorrow_CrossTenantReuseDisabledDoesNotShareOrReturnIdle) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
auto options = stream_options(1, 2);
options.protocol.allow_cross_tenant_reuse = false;
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed), options);
ucp::borrow_options tenant_a;
tenant_a.tenant_id = "tenant-a";
auto first = pool.borrow_stream(tenant_a);
ucp::borrow_options tenant_b;
tenant_b.tenant_id = "tenant-b";
auto second = pool.try_borrow_stream(tenant_b);
UCP_EXPECT_FALSE(second.has_value());
first.reset();
UCP_EXPECT_EQ(pool.stats().active_streams, 0u);
UCP_EXPECT_EQ(pool.stats().idle_connections, 0u);
UCP_EXPECT_EQ(closed.load(), 1);
}
UCP_TEST(StreamBorrow_AtStreamCapacityCreatesSecondPhysicalConnection) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed),
stream_options(2, 1));
auto first = pool.borrow_stream();
auto second = pool.borrow_stream();
UCP_EXPECT_TRUE(first->id != second->id);
UCP_EXPECT_EQ(created.load(), 2);
UCP_EXPECT_EQ(pool.stats().active_connections, 2u);
UCP_EXPECT_EQ(pool.stats().active_streams, 2u);
}
UCP_TEST(StreamBorrow_EndpointCapacityOverridesPoolDefault) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
auto options = stream_options(1, 4);
options.endpoints = {ucp::endpoint_config{.id = "grpc-a", .stream_capacity = 1}};
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed), options);
auto first = pool.borrow_stream();
auto second = pool.try_borrow_stream();
UCP_EXPECT_FALSE(second.has_value());
UCP_EXPECT_EQ(pool.stats().active_streams, 1u);
}
UCP_TEST(StreamBorrow_DrainConnectionPreventsNewStreamsAndRetiresOnReturn) {
std::atomic<int> created{0};
std::atomic<int> closed{0};
ucp::connection_pool<stream_connection> pool(make_stream_factory(created, closed),
stream_options(1, 2));
auto first = pool.borrow_stream();
const auto first_id = first->id;
first.drain_connection();
auto second = pool.try_borrow_stream();
UCP_EXPECT_FALSE(second.has_value());
first.reset();
UCP_EXPECT_EQ(pool.stats().active_connections, 0u);
UCP_EXPECT_EQ(pool.stats().active_streams, 0u);
UCP_EXPECT_EQ(pool.stats().idle_connections, 0u);
UCP_EXPECT_EQ(closed.load(), 1);
auto replacement = pool.borrow_stream();
UCP_EXPECT_TRUE(replacement->id != first_id);
UCP_EXPECT_EQ(created.load(), 2);
}