forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drain_close_integration_test.cc
179 lines (146 loc) · 7.66 KB
/
drain_close_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
#include "test/integration/http_protocol_integration.h"
namespace Envoy {
namespace {
using DrainCloseIntegrationTest = HttpProtocolIntegrationTest;
// Add a health check filter and verify correct behavior when draining.
TEST_P(DrainCloseIntegrationTest, DrainCloseGradual) {
// The probability of drain close increases over time. With a high timeout,
// the probability will be very low, but the rapid retries prevent this from
// increasing total test time.
drain_time_ = std::chrono::seconds(100);
config_helper_.addFilter(ConfigHelper::defaultHealthCheckFilter());
initialize();
absl::Notification drain_sequence_started;
test_server_->server().dispatcher().post([this, &drain_sequence_started]() {
test_server_->drainManager().startDrainSequence([] {});
drain_sequence_started.Notify();
});
drain_sequence_started.WaitForNotification();
codec_client_ = makeHttpConnection(lookupPort("http"));
EXPECT_FALSE(codec_client_->disconnected());
IntegrationStreamDecoderPtr response;
while (!test_server_->counter("http.config_test.downstream_cx_drain_close")->value()) {
response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
response->waitForEndStream();
}
EXPECT_EQ(test_server_->counter("http.config_test.downstream_cx_drain_close")->value(), 1L);
ASSERT_TRUE(codec_client_->waitForDisconnect());
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
if (downstream_protocol_ == Http::CodecClient::Type::HTTP2) {
EXPECT_TRUE(codec_client_->sawGoAway());
} else {
EXPECT_EQ("close", response->headers().getConnectionValue());
}
}
TEST_P(DrainCloseIntegrationTest, DrainCloseImmediate) {
drain_strategy_ = Server::DrainStrategy::Immediate;
drain_time_ = std::chrono::seconds(100);
config_helper_.addFilter(ConfigHelper::defaultHealthCheckFilter());
initialize();
absl::Notification drain_sequence_started;
test_server_->server().dispatcher().post([this, &drain_sequence_started]() {
test_server_->drainManager().startDrainSequence([] {});
drain_sequence_started.Notify();
});
drain_sequence_started.WaitForNotification();
codec_client_ = makeHttpConnection(lookupPort("http"));
EXPECT_FALSE(codec_client_->disconnected());
IntegrationStreamDecoderPtr response;
response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
response->waitForEndStream();
ASSERT_TRUE(codec_client_->waitForDisconnect());
EXPECT_TRUE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
if (downstream_protocol_ == Http::CodecClient::Type::HTTP2) {
EXPECT_TRUE(codec_client_->sawGoAway());
} else {
EXPECT_EQ("close", response->headers().getConnectionValue());
}
}
TEST_P(DrainCloseIntegrationTest, AdminDrain) { testAdminDrain(downstreamProtocol()); }
TEST_P(DrainCloseIntegrationTest, AdminGracefulDrain) {
drain_strategy_ = Server::DrainStrategy::Immediate;
drain_time_ = std::chrono::seconds(999);
initialize();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
uint32_t http_port = lookupPort("http");
codec_client_ = makeHttpConnection(http_port);
auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest(0);
upstream_request_->encodeHeaders(default_response_headers_, true);
response->waitForEndStream();
ASSERT_TRUE(response->complete());
EXPECT_THAT(response->headers(), Http::HttpStatusIs("200"));
// The request is completed but the connection remains open.
EXPECT_TRUE(codec_client_->connected());
// Invoke /drain_listeners with graceful drain
BufferingStreamDecoderPtr admin_response = IntegrationUtil::makeSingleRequest(
lookupPort("admin"), "POST", "/drain_listeners?graceful", "", downstreamProtocol(), version_);
EXPECT_EQ(admin_response->headers().Status()->value().getStringView(), "200");
// With a 999s graceful drain period, the listener should still be open.
EXPECT_EQ(test_server_->counter("listener_manager.listener_stopped")->value(), 0);
response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest(0);
upstream_request_->encodeHeaders(default_response_headers_, true);
response->waitForEndStream();
ASSERT_TRUE(response->complete());
EXPECT_THAT(response->headers(), Http::HttpStatusIs("200"));
// Connections will terminate on request complete
ASSERT_TRUE(codec_client_->waitForDisconnect());
if (downstream_protocol_ == Http::CodecClient::Type::HTTP2) {
EXPECT_TRUE(codec_client_->sawGoAway());
} else {
EXPECT_EQ("close", response->headers().getConnectionValue());
}
// New connections can still be made.
auto second_codec_client_ = makeRawHttpConnection(makeClientConnection(http_port), absl::nullopt);
EXPECT_TRUE(second_codec_client_->connected());
// Invoke /drain_listeners and shut down listeners.
second_codec_client_->rawConnection().close(Network::ConnectionCloseType::NoFlush);
admin_response = IntegrationUtil::makeSingleRequest(
lookupPort("admin"), "POST", "/drain_listeners", "", downstreamProtocol(), version_);
EXPECT_EQ(admin_response->headers().Status()->value().getStringView(), "200");
test_server_->waitForCounterEq("listener_manager.listener_stopped", 1);
ASSERT_TRUE(waitForPortAvailable(http_port));
}
TEST_P(DrainCloseIntegrationTest, RepeatedAdminGracefulDrain) {
// Use the default gradual probabilistic DrainStrategy so drainClose()
// behaviour isn't conflated with whether the drain sequence has started.
drain_time_ = std::chrono::seconds(999);
initialize();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
uint32_t http_port = lookupPort("http");
codec_client_ = makeHttpConnection(http_port);
auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest(0);
upstream_request_->encodeHeaders(default_response_headers_, true);
response->waitForEndStream();
// Invoke /drain_listeners with graceful drain
BufferingStreamDecoderPtr admin_response = IntegrationUtil::makeSingleRequest(
lookupPort("admin"), "POST", "/drain_listeners?graceful", "", downstreamProtocol(), version_);
EXPECT_EQ(admin_response->headers().Status()->value().getStringView(), "200");
EXPECT_EQ(test_server_->counter("listener_manager.listener_stopped")->value(), 0);
admin_response = IntegrationUtil::makeSingleRequest(
lookupPort("admin"), "POST", "/drain_listeners?graceful", "", downstreamProtocol(), version_);
EXPECT_EQ(admin_response->headers().Status()->value().getStringView(), "200");
EXPECT_EQ(admin_response->headers().Status()->value().getStringView(), "200");
response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest(0);
upstream_request_->encodeHeaders(default_response_headers_, true);
response->waitForEndStream();
ASSERT_TRUE(response->complete());
EXPECT_THAT(response->headers(), Http::HttpStatusIs("200"));
admin_response = IntegrationUtil::makeSingleRequest(
lookupPort("admin"), "POST", "/drain_listeners", "", downstreamProtocol(), version_);
EXPECT_EQ(admin_response->headers().Status()->value().getStringView(), "200");
test_server_->waitForCounterEq("listener_manager.listener_stopped", 1);
ASSERT_TRUE(waitForPortAvailable(http_port));
}
INSTANTIATE_TEST_SUITE_P(Protocols, DrainCloseIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams(
{Http::CodecClient::Type::HTTP1, Http::CodecClient::Type::HTTP2},
{FakeHttpConnection::Type::HTTP1})),
HttpProtocolIntegrationTest::protocolTestParamsToString);
} // namespace
} // namespace Envoy