forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_conn_pool_integration_test.cc
219 lines (169 loc) · 9.07 KB
/
http_conn_pool_integration_test.cc
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
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "test/integration/http_protocol_integration.h"
namespace Envoy {
namespace {
class HttpConnPoolIntegrationTest : public HttpProtocolIntegrationTest {
public:
void initialize() override {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
// Set pool limit so that the test can use it's stats to validate that
// the pool is deleted.
envoy::config::cluster::v3::CircuitBreakers circuit_breakers;
auto* threshold = circuit_breakers.mutable_thresholds()->Add();
threshold->mutable_max_connection_pools()->set_value(1);
auto* static_resources = bootstrap.mutable_static_resources();
for (int i = 0; i < static_resources->clusters_size(); ++i) {
static_resources->mutable_clusters(i)->mutable_circuit_breakers()->MergeFrom(
circuit_breakers);
}
});
HttpProtocolIntegrationTest::initialize();
}
};
INSTANTIATE_TEST_SUITE_P(Protocols, HttpConnPoolIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams()),
HttpProtocolIntegrationTest::protocolTestParamsToString);
// Tests that conn pools are cleaned up after becoming idle due to a LocalClose
TEST_P(HttpConnPoolIntegrationTest, PoolCleanupAfterLocalClose) {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
// Make Envoy close the upstream connection after a single request.
ConfigHelper::HttpProtocolOptions protocol_options;
protocol_options.mutable_common_http_protocol_options()
->mutable_max_requests_per_connection()
->set_value(1);
ConfigHelper::setProtocolOptions(*bootstrap.mutable_static_resources()->mutable_clusters(0),
protocol_options);
});
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
// Validate that the circuit breaker config is setup as we expect.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 1);
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
// Validate that the pool is deleted when it becomes idle.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 0);
}
// Tests that conn pools are cleaned up after becoming idle due to a RemoteClose
TEST_P(HttpConnPoolIntegrationTest, PoolCleanupAfterRemoteClose) {
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
// Validate that the circuit breaker config is setup as we expect.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 1);
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
ASSERT_TRUE(fake_upstream_connection_->close());
// Validate that the pool is deleted when it becomes idle.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 0);
}
// Verify that the drainConnections() cluster manager API works correctly.
TEST_P(HttpConnPoolIntegrationTest, PoolDrainAfterDrainApiSpecificCluster) {
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
// Validate that the circuit breaker config is setup as we expect.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 1);
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
// Drain connection pools via API. Need to post this to the server thread.
test_server_->server().dispatcher().post(
[this] { test_server_->server().clusterManager().drainConnections("cluster_0", nullptr); });
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
// Validate that the pool is deleted when it becomes idle.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 0);
}
// Verify the drainConnections() with a predicate is able to filter host drains.
TEST_P(HttpConnPoolIntegrationTest, DrainConnectionsWithPredicate) {
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
// Perform a drain request which doesn't actually do a drain.
test_server_->server().dispatcher().post([this] {
test_server_->server().clusterManager().drainConnections(
"cluster_0", [](const Upstream::Host&) { return false; });
});
// The existing upstream connection should continue to work.
response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
// Now do a drain that matches.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 1);
test_server_->server().dispatcher().post([this] {
test_server_->server().clusterManager().drainConnections(
"cluster_0", [](const Upstream::Host&) { return true; });
});
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 0);
}
// Verify that the drainConnections() cluster manager API works correctly.
TEST_P(HttpConnPoolIntegrationTest, PoolDrainAfterDrainApiAllClusters) {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
bootstrap.mutable_static_resources()->mutable_clusters()->Add()->MergeFrom(
*bootstrap.mutable_static_resources()->mutable_clusters(0));
bootstrap.mutable_static_resources()->mutable_clusters(1)->set_name("cluster_1");
});
setUpstreamCount(2);
auto host = config_helper_.createVirtualHost("cluster_1.com", "/", "cluster_1");
config_helper_.addVirtualHost(host);
config_helper_.setDefaultHostAndRoute("cluster_0.com", "/");
initialize();
// Request Flow to cluster_0.
codec_client_ = makeHttpConnection(lookupPort("http"));
default_request_headers_.setHost("cluster_0.com");
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
// Validate that the circuit breaker config is setup as we expect.
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 1);
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
auto first_connection = std::move(fake_upstream_connection_);
codec_client_->close();
// Request Flow to cluster_1.
codec_client_ = makeHttpConnection(lookupPort("http"));
default_request_headers_.setHost("cluster_1.com");
response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest(1);
// Validate that the circuit breaker config is setup as we expect.
test_server_->waitForGaugeEq("cluster.cluster_1.circuit_breakers.default.cx_pool_open", 1);
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
ASSERT_TRUE(response->waitForEndStream());
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
// Drain connection pools via API. Need to post this to the server thread.
test_server_->server().dispatcher().post(
[this] { test_server_->server().clusterManager().drainConnections(nullptr); });
ASSERT_TRUE(first_connection->waitForDisconnect());
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
test_server_->waitForGaugeEq("cluster.cluster_0.circuit_breakers.default.cx_pool_open", 0);
test_server_->waitForGaugeEq("cluster.cluster_1.circuit_breakers.default.cx_pool_open", 0);
}
} // namespace
} // namespace Envoy