forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_stream_decoder.cc
More file actions
167 lines (143 loc) · 5.71 KB
/
integration_stream_decoder.cc
File metadata and controls
167 lines (143 loc) · 5.71 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
#include "test/integration/integration_stream_decoder.h"
#include <algorithm>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "envoy/buffer/buffer.h"
#include "envoy/event/dispatcher.h"
#include "envoy/http/header_map.h"
#include "source/common/common/assert.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::AssertionFailure;
using testing::AssertionResult;
using testing::AssertionSuccess;
namespace Envoy {
IntegrationStreamDecoder::IntegrationStreamDecoder(Event::Dispatcher& dispatcher)
: dispatcher_(dispatcher) {}
IntegrationStreamDecoder::~IntegrationStreamDecoder() {
ENVOY_LOG_MISC(trace, "Destroying IntegrationStreamDecoder");
}
void IntegrationStreamDecoder::waitFor1xxHeaders() {
if (continue_headers_.get()) {
return;
}
waiting_for_continue_headers_ = true;
ASSERT_TRUE(waitForWithDispatcherRun([this]() { return continue_headers_.get() != nullptr; },
"1xx headers", TestUtility::DefaultTimeout));
}
void IntegrationStreamDecoder::waitForHeaders() {
if (headers_.get()) {
return;
}
waiting_for_headers_ = true;
ASSERT_TRUE(waitForWithDispatcherRun([this]() { return headers_.get() != nullptr; }, "headers",
TestUtility::DefaultTimeout));
}
void IntegrationStreamDecoder::waitForBodyData(uint64_t size) {
ASSERT(body_data_waiting_length_ == 0);
body_data_waiting_length_ = size;
body_data_waiting_length_ -=
std::min(body_data_waiting_length_, static_cast<uint64_t>(body_.size()));
if (body_data_waiting_length_ == 0) {
return;
}
ASSERT_TRUE(waitForWithDispatcherRun([this]() { return body_data_waiting_length_ == 0; },
"body data", TestUtility::DefaultTimeout));
}
AssertionResult
IntegrationStreamDecoder::waitForWithDispatcherRun(const std::function<bool()>& condition,
absl::string_view description,
std::chrono::milliseconds timeout) {
Event::TestTimeSystem::RealTimeBound bound(timeout);
while (!condition()) {
if (!bound.withinBound()) {
return AssertionFailure() << "Timed out (" << timeout.count() << "ms) waiting for "
<< description << debugState();
}
dispatcher_.run(Event::Dispatcher::RunType::NonBlock);
if (condition()) {
break;
}
if (isFinished()) {
return AssertionFailure() << "Stream finished while waiting for " << description
<< debugState();
}
// Wait for a moment before running the dispatcher again to avoid spinning.
std::this_thread::yield();
}
return AssertionSuccess();
}
std::string IntegrationStreamDecoder::debugState() const {
return absl::StrCat(
"\nIntegrationStreamDecoder state:", "\n saw_end_stream_=", saw_end_stream_,
"\n saw_reset_=", saw_reset_,
"\n headers_=", headers_ ? fmt::format("{}", *headers_) : "null",
"\n continue_headers_=", continue_headers_ ? fmt::format("{}", *continue_headers_) : "null",
"\n body_=", absl::CEscape(body_),
"\n trailers_=", trailers_ ? fmt::format("{}", *trailers_) : "null");
}
AssertionResult IntegrationStreamDecoder::waitForEndStream(std::chrono::milliseconds timeout) {
waiting_for_end_stream_ = true;
return waitForWithDispatcherRun([this]() { return saw_end_stream_; }, "end stream", timeout);
}
AssertionResult IntegrationStreamDecoder::waitForReset(std::chrono::milliseconds timeout) {
waiting_for_reset_ = true;
return waitForWithDispatcherRun([this]() { return saw_reset_; }, "reset", timeout);
}
AssertionResult IntegrationStreamDecoder::waitForAnyTermination(std::chrono::milliseconds timeout) {
waiting_for_end_stream_ = true;
waiting_for_reset_ = true;
return waitForWithDispatcherRun([this]() { return isFinished(); }, "any termination", timeout);
}
void IntegrationStreamDecoder::decode1xxHeaders(Http::ResponseHeaderMapPtr&& headers) {
continue_headers_ = std::move(headers);
if (waiting_for_continue_headers_) {
dispatcher_.exit();
}
}
void IntegrationStreamDecoder::decodeHeaders(Http::ResponseHeaderMapPtr&& headers,
bool end_stream) {
saw_end_stream_ = end_stream;
headers_ = std::move(headers);
if ((end_stream && (waiting_for_reset_ || waiting_for_end_stream_)) || waiting_for_headers_) {
dispatcher_.exit();
}
}
void IntegrationStreamDecoder::decodeData(Buffer::Instance& data, bool end_stream) {
saw_end_stream_ = end_stream;
body_ += data.toString();
if (end_stream && (waiting_for_reset_ || waiting_for_end_stream_)) {
dispatcher_.exit();
} else if (body_data_waiting_length_ > 0) {
body_data_waiting_length_ -= std::min(body_data_waiting_length_, data.length());
if (body_data_waiting_length_ == 0) {
dispatcher_.exit();
}
}
}
void IntegrationStreamDecoder::decodeTrailers(Http::ResponseTrailerMapPtr&& trailers) {
saw_end_stream_ = true;
trailers_ = std::move(trailers);
if (waiting_for_reset_ || waiting_for_end_stream_) {
dispatcher_.exit();
}
}
void IntegrationStreamDecoder::decodeMetadata(Http::MetadataMapPtr&& metadata_map) {
metadata_maps_decoded_count_++;
// Combines newly received metadata with the existing metadata.
for (const auto& metadata : *metadata_map) {
duplicated_metadata_key_count_[metadata.first]++;
metadata_map_->insert(metadata);
}
}
void IntegrationStreamDecoder::onResetStream(Http::StreamResetReason reason, absl::string_view) {
saw_reset_ = true;
reset_reason_ = reason;
if (waiting_for_reset_ || waiting_for_end_stream_ || waiting_for_continue_headers_ ||
waiting_for_headers_) {
dispatcher_.exit();
}
}
} // namespace Envoy