forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_conn_pool_integration_test.cc
183 lines (138 loc) · 7.5 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
#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_.addRuntimeOverride("envoy.reloadable_features.conn_pool_delete_when_idle",
"true");
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"); });
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 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(); });
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