forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idle_timeout_integration_test.cc
418 lines (338 loc) · 16.6 KB
/
idle_timeout_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "envoy/config/bootstrap/v3/bootstrap.pb.h"
#include "envoy/extensions/filters/network/http_connection_manager/v3/http_connection_manager.pb.h"
#include "test/integration/http_protocol_integration.h"
#include "test/test_common/test_time.h"
using testing::HasSubstr;
namespace Envoy {
namespace {
class IdleTimeoutIntegrationTest : public HttpProtocolIntegrationTest {
public:
void initialize() override {
useAccessLog("%RESPONSE_CODE_DETAILS%");
config_helper_.addConfigModifier(
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
hcm) -> void {
if (enable_global_idle_timeout_) {
hcm.mutable_stream_idle_timeout()->set_seconds(0);
hcm.mutable_stream_idle_timeout()->set_nanos(IdleTimeoutMs * 1000 * 1000);
}
if (enable_per_stream_idle_timeout_) {
auto* route_config = hcm.mutable_route_config();
auto* virtual_host = route_config->mutable_virtual_hosts(0);
auto* route = virtual_host->mutable_routes(0)->mutable_route();
route->mutable_idle_timeout()->set_seconds(0);
route->mutable_idle_timeout()->set_nanos(IdleTimeoutMs * 1000 * 1000);
auto* header = virtual_host->mutable_response_headers_to_add()->Add()->mutable_header();
header->set_key("foo");
header->set_value("bar");
}
if (enable_request_timeout_) {
hcm.mutable_request_timeout()->set_seconds(0);
hcm.mutable_request_timeout()->set_nanos(RequestTimeoutMs * 1000 * 1000);
}
// For validating encode100ContinueHeaders() timer kick.
hcm.set_proxy_100_continue(true);
});
HttpProtocolIntegrationTest::initialize();
}
IntegrationStreamDecoderPtr setupPerStreamIdleTimeoutTest(const char* method = "GET") {
initialize();
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto encoder_decoder =
codec_client_->startRequest(Http::TestRequestHeaderMapImpl{{":method", method},
{":path", "/test/long/url"},
{":scheme", "http"},
{":authority", "host"}});
request_encoder_ = &encoder_decoder.first;
auto response = std::move(encoder_decoder.second);
AssertionResult result =
fake_upstreams_[0]->waitForHttpConnection(*dispatcher_, fake_upstream_connection_);
RELEASE_ASSERT(result, result.message());
result = fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_);
RELEASE_ASSERT(result, result.message());
result = upstream_request_->waitForHeadersComplete();
RELEASE_ASSERT(result, result.message());
return response;
}
void sleep() {
test_time_.timeSystem().advanceTimeWait(std::chrono::milliseconds(IdleTimeoutMs / 2));
}
void waitForTimeout(IntegrationStreamDecoder& response, absl::string_view stat_name = "",
absl::string_view stat_prefix = "http.config_test") {
if (downstream_protocol_ == Http::CodecClient::Type::HTTP1) {
ASSERT_TRUE(codec_client_->waitForDisconnect());
} else {
response.waitForReset();
codec_client_->close();
}
if (!stat_name.empty()) {
EXPECT_EQ(1, test_server_->counter(fmt::format("{}.{}", stat_prefix, stat_name))->value());
}
}
// TODO(htuch): This might require scaling for TSAN/ASAN/Valgrind/etc. Bump if
// this is the cause of flakes.
static constexpr uint64_t IdleTimeoutMs = 400;
static constexpr uint64_t RequestTimeoutMs = 200;
bool enable_global_idle_timeout_{false};
bool enable_per_stream_idle_timeout_{false};
bool enable_request_timeout_{false};
DangerousDeprecatedTestTime test_time_;
};
INSTANTIATE_TEST_SUITE_P(Protocols, IdleTimeoutIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams()),
HttpProtocolIntegrationTest::protocolTestParamsToString);
// Tests idle timeout behaviour with single request and validates that idle timer kicks in
// after given timeout.
TEST_P(IdleTimeoutIntegrationTest, TimeoutBasic) {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
ConfigHelper::HttpProtocolOptions protocol_options;
auto* http_protocol_options = protocol_options.mutable_common_http_protocol_options();
auto* idle_time_out = http_protocol_options->mutable_idle_timeout();
std::chrono::milliseconds timeout(1000);
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(timeout);
idle_time_out->set_seconds(seconds.count());
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();
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
response->waitForEndStream();
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 1);
test_server_->waitForCounterGe("cluster.cluster_0.upstream_rq_200", 1);
// Do not send any requests and validate if idle time out kicks in.
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_idle_timeout", 1);
}
// Tests idle timeout behaviour with multiple requests and validates that idle timer kicks in
// after both the requests are done.
TEST_P(IdleTimeoutIntegrationTest, IdleTimeoutWithTwoRequests) {
config_helper_.addConfigModifier([](envoy::config::bootstrap::v3::Bootstrap& bootstrap) {
ConfigHelper::HttpProtocolOptions protocol_options;
auto* http_protocol_options = protocol_options.mutable_common_http_protocol_options();
auto* idle_time_out = http_protocol_options->mutable_idle_timeout();
std::chrono::milliseconds timeout(1000);
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(timeout);
idle_time_out->set_seconds(seconds.count());
ConfigHelper::setProtocolOptions(*bootstrap.mutable_static_resources()->mutable_clusters(0),
protocol_options);
});
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
// Request 1.
auto response = codec_client_->makeRequestWithBody(default_request_headers_, 1024);
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(512, true);
response->waitForEndStream();
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 1);
test_server_->waitForCounterGe("cluster.cluster_0.upstream_rq_200", 1);
// Request 2.
response = codec_client_->makeRequestWithBody(default_request_headers_, 512);
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(default_response_headers_, false);
upstream_request_->encodeData(1024, true);
response->waitForEndStream();
EXPECT_TRUE(upstream_request_->complete());
EXPECT_TRUE(response->complete());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_total", 1);
test_server_->waitForCounterGe("cluster.cluster_0.upstream_rq_200", 2);
// Do not send any requests and validate if idle time out kicks in.
ASSERT_TRUE(fake_upstream_connection_->waitForDisconnect());
test_server_->waitForCounterGe("cluster.cluster_0.upstream_cx_idle_timeout", 1);
}
// Per-stream idle timeout after having sent downstream headers.
TEST_P(IdleTimeoutIntegrationTest, PerStreamIdleTimeoutAfterDownstreamHeaders) {
enable_per_stream_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest();
waitForTimeout(*response, "downstream_rq_idle_timeout");
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("408", response->headers().getStatusValue());
auto foo = Http::LowerCaseString("foo");
ASSERT_FALSE(response->headers().get(foo).empty());
EXPECT_EQ("bar", response->headers().get(foo)[0]->value().getStringView());
EXPECT_EQ("stream timeout", response->body());
EXPECT_THAT(waitForAccessLog(access_log_name_), HasSubstr("stream_idle_timeout"));
}
// Per-stream idle timeout with reads disabled.
TEST_P(IdleTimeoutIntegrationTest, PerStreamIdleTimeoutWithLargeBuffer) {
config_helper_.addFilter(R"EOF(
name: backpressure-filter
)EOF");
enable_per_stream_idle_timeout_ = true;
initialize();
codec_client_ = makeHttpConnection(makeClientConnection((lookupPort("http"))));
auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
response->waitForEndStream();
EXPECT_TRUE(response->complete());
// Make sure that for HTTP/1.1 reads are enabled even though the first request
// ended in the "backed up" state.
auto response2 = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
response2->waitForEndStream();
EXPECT_TRUE(response2->complete());
}
// Per-stream idle timeout after having sent downstream head request.
TEST_P(IdleTimeoutIntegrationTest, PerStreamIdleTimeoutHeadRequestAfterDownstreamHeadRequest) {
enable_per_stream_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest("HEAD");
waitForTimeout(*response, "downstream_rq_idle_timeout");
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("408", response->headers().getStatusValue());
EXPECT_EQ(fmt::format("{}", strlen("stream timeout")),
response->headers().getContentLengthValue());
EXPECT_EQ("", response->body());
}
// Global per-stream idle timeout applies if there is no per-stream idle timeout.
TEST_P(IdleTimeoutIntegrationTest, GlobalPerStreamIdleTimeoutAfterDownstreamHeaders) {
enable_per_stream_idle_timeout_ = true;
enable_global_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest();
waitForTimeout(*response, "downstream_rq_idle_timeout");
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("408", response->headers().getStatusValue());
EXPECT_EQ("stream timeout", response->body());
}
// Per-stream idle timeout after having sent downstream headers+body.
TEST_P(IdleTimeoutIntegrationTest, PerStreamIdleTimeoutAfterDownstreamHeadersAndBody) {
enable_per_stream_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest();
sleep();
codec_client_->sendData(*request_encoder_, 1, false);
waitForTimeout(*response, "downstream_rq_idle_timeout");
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(1U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("408", response->headers().getStatusValue());
EXPECT_EQ("stream timeout", response->body());
}
// Per-stream idle timeout after upstream headers have been sent.
TEST_P(IdleTimeoutIntegrationTest, PerStreamIdleTimeoutAfterUpstreamHeaders) {
enable_per_stream_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest();
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, false);
waitForTimeout(*response, "downstream_rq_idle_timeout");
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_FALSE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ("", response->body());
}
// Per-stream idle timeout after a sequence of header/data events.
TEST_P(IdleTimeoutIntegrationTest, PerStreamIdleTimeoutAfterBidiData) {
enable_per_stream_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest();
sleep();
upstream_request_->encode100ContinueHeaders(Http::TestResponseHeaderMapImpl{{":status", "100"}});
sleep();
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, false);
sleep();
upstream_request_->encodeData(1, false);
sleep();
codec_client_->sendData(*request_encoder_, 1, false);
sleep();
Http::TestRequestTrailerMapImpl request_trailers{{"request1", "trailer1"},
{"request2", "trailer2"}};
codec_client_->sendTrailers(*request_encoder_, request_trailers);
sleep();
upstream_request_->encodeData(1, false);
waitForTimeout(*response, "downstream_rq_idle_timeout");
EXPECT_TRUE(upstream_request_->complete());
EXPECT_EQ(1U, upstream_request_->bodyLength());
EXPECT_FALSE(response->complete());
EXPECT_EQ("200", response->headers().getStatusValue());
EXPECT_EQ("aa", response->body());
}
// Successful request/response when per-stream idle timeout is configured.
TEST_P(IdleTimeoutIntegrationTest, PerStreamIdleTimeoutRequestAndResponse) {
enable_per_stream_idle_timeout_ = true;
testRouterRequestAndResponseWithBody(1024, 1024, false);
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutConfiguredRequestResponse) {
enable_request_timeout_ = true;
testRouterHeaderOnlyRequestAndResponse();
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutConfiguredRequestResponseWithBody) {
enable_request_timeout_ = true;
testRouterRequestAndResponseWithBody(1024, 1024, false);
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutTriggersOnBodilessPost) {
enable_request_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest("POST");
waitForTimeout(*response, "downstream_rq_timeout");
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("408", response->headers().getStatusValue());
EXPECT_EQ("request timeout", response->body());
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutUnconfiguredDoesNotTriggerOnBodilessPost) {
enable_request_timeout_ = false;
// with no request timeout configured, the idle timeout triggers instead
enable_per_stream_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest("POST");
waitForTimeout(*response);
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("408", response->headers().getStatusValue());
EXPECT_NE("request timeout", response->body());
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutTriggersOnRawIncompleteRequestWithHeaders) {
// Omitting \r\n\r\n does not indicate incomplete request in HTTP2
if (downstreamProtocol() == Envoy::Http::CodecClient::Type::HTTP2) {
return;
}
enable_request_timeout_ = true;
initialize();
std::string raw_response;
sendRawHttpAndWaitForResponse(lookupPort("http"), "GET / HTTP/1.1", &raw_response, true);
EXPECT_THAT(raw_response, testing::HasSubstr("request timeout"));
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutDoesNotTriggerOnRawCompleteRequestWithHeaders) {
if (downstreamProtocol() == Envoy::Http::CodecClient::Type::HTTP2) {
return;
}
enable_request_timeout_ = true;
initialize();
std::string raw_response;
sendRawHttpAndWaitForResponse(lookupPort("http"), "GET / HTTP/1.1\r\n\r\n", &raw_response, true);
EXPECT_THAT(raw_response, testing::Not(testing::HasSubstr("request timeout")));
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutIsDisarmedByPrematureEncodeHeaders) {
enable_request_timeout_ = true;
enable_per_stream_idle_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest("POST");
upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, false);
waitForTimeout(*response);
EXPECT_FALSE(upstream_request_->complete());
EXPECT_FALSE(response->complete());
EXPECT_NE("request timeout", response->body());
}
TEST_P(IdleTimeoutIntegrationTest, RequestTimeoutIsNotDisarmedByEncode100ContinueHeaders) {
enable_request_timeout_ = true;
auto response = setupPerStreamIdleTimeoutTest("POST");
upstream_request_->encode100ContinueHeaders(Http::TestResponseHeaderMapImpl{{":status", "100"}});
waitForTimeout(*response, "downstream_rq_timeout");
EXPECT_FALSE(upstream_request_->complete());
EXPECT_EQ(0U, upstream_request_->bodyLength());
EXPECT_TRUE(response->complete());
EXPECT_EQ("408", response->headers().getStatusValue());
EXPECT_EQ("request timeout", response->body());
}
// TODO(auni53) create a test filter that hangs and does not send data upstream, which would
// trigger a configured request_timer
} // namespace
} // namespace Envoy