Skip to content

Commit

Permalink
Using common member variables in integration tests (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssawilk authored and mattklein123 committed Aug 24, 2017
1 parent 8f1cc1e commit a8507f6
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 641 deletions.
47 changes: 22 additions & 25 deletions test/integration/grpc_json_transcoder_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,26 @@ class GrpcJsonTranscoderIntegrationTest
const std::vector<std::string>& grpc_response_messages,
const Status& grpc_status, Http::HeaderMap&& response_headers,
const std::string& response_body) {
IntegrationCodecClientPtr codec_client;
FakeHttpConnectionPtr fake_upstream_connection;
IntegrationStreamDecoderPtr response(new IntegrationStreamDecoder(*dispatcher_));
FakeStreamPtr request_stream;
response_.reset(new IntegrationStreamDecoder(*dispatcher_));

codec_client = makeHttpConnection(lookupPort("http"), Http::CodecClient::Type::HTTP1);
codec_client_ = makeHttpConnection(lookupPort("http"), Http::CodecClient::Type::HTTP1);

if (!request_body.empty()) {
Http::StreamEncoder& encoder = codec_client->startRequest(request_headers, *response);
request_encoder_ = &codec_client_->startRequest(request_headers, *response_);
Buffer::OwnedImpl body(request_body);
codec_client->sendData(encoder, body, true);
codec_client_->sendData(*request_encoder_, body, true);
} else {
codec_client->makeHeaderOnlyRequest(request_headers, *response);
codec_client_->makeHeaderOnlyRequest(request_headers, *response_);
}

fake_upstream_connection = fake_upstreams_[0]->waitForHttpConnection(*dispatcher_);
request_stream = fake_upstream_connection->waitForNewStream();
fake_upstream_connection_ = fake_upstreams_[0]->waitForHttpConnection(*dispatcher_);
upstream_request_ = fake_upstream_connection_->waitForNewStream();
if (!grpc_request_messages.empty()) {
request_stream->waitForEndStream(*dispatcher_);
upstream_request_->waitForEndStream(*dispatcher_);

Grpc::Decoder grpc_decoder;
std::vector<Grpc::Frame> frames;
EXPECT_TRUE(grpc_decoder.decode(request_stream->body(), frames));
EXPECT_TRUE(grpc_decoder.decode(upstream_request_->body(), frames));
EXPECT_EQ(grpc_request_messages.size(), frames.size());

for (size_t i = 0; i < grpc_request_messages.size(); ++i) {
Expand All @@ -90,41 +87,41 @@ class GrpcJsonTranscoderIntegrationTest
if (grpc_response_messages.empty()) {
response_headers.insertGrpcStatus().value(grpc_status.error_code());
response_headers.insertGrpcMessage().value(grpc_status.error_message());
request_stream->encodeHeaders(response_headers, true);
upstream_request_->encodeHeaders(response_headers, true);
} else {
request_stream->encodeHeaders(response_headers, false);
upstream_request_->encodeHeaders(response_headers, false);
for (const auto& response_message_str : grpc_response_messages) {
ResponseType response_message;
EXPECT_TRUE(TextFormat::ParseFromString(response_message_str, &response_message));
auto buffer = Grpc::Common::serializeBody(response_message);
request_stream->encodeData(*buffer, false);
upstream_request_->encodeData(*buffer, false);
}
Http::TestHeaderMapImpl response_trailers;
response_trailers.insertGrpcStatus().value(grpc_status.error_code());
response_trailers.insertGrpcMessage().value(grpc_status.error_message());
request_stream->encodeTrailers(response_trailers);
upstream_request_->encodeTrailers(response_trailers);
}
EXPECT_TRUE(request_stream->complete());
EXPECT_TRUE(upstream_request_->complete());
} else {
request_stream->waitForReset();
upstream_request_->waitForReset();
}

response->waitForEndStream();
EXPECT_TRUE(response->complete());
response_->waitForEndStream();
EXPECT_TRUE(response_->complete());
response_headers.iterate(
[](const Http::HeaderEntry& entry, void* context) -> void {
IntegrationStreamDecoder* response = static_cast<IntegrationStreamDecoder*>(context);
Http::LowerCaseString lower_key{entry.key().c_str()};
EXPECT_STREQ(entry.value().c_str(), response->headers().get(lower_key)->value().c_str());
},
response.get());
response_.get());
if (!response_body.empty()) {
EXPECT_EQ(response_body, response->body());
EXPECT_EQ(response_body, response_->body());
}

codec_client->close();
fake_upstream_connection->close();
fake_upstream_connection->waitForDisconnect();
codec_client_->close();
fake_upstream_connection_->close();
fake_upstream_connection_->waitForDisconnect();
}
};

Expand Down
79 changes: 38 additions & 41 deletions test/integration/http2_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,12 @@ TEST_P(Http2IntegrationTest, MaxHeadersInCodec) {

big_headers.addCopy("big", std::string(63 * 1024, 'a'));

IntegrationCodecClientPtr codec_client;
IntegrationStreamDecoderPtr response(new IntegrationStreamDecoder(*dispatcher_));
Http::StreamEncoder* downstream_request{};
executeActions(
{[&]() -> void {
codec_client = makeHttpConnection(lookupPort("http"), Http::CodecClient::Type::HTTP2);
codec_client_ = makeHttpConnection(lookupPort("http"), Http::CodecClient::Type::HTTP2);
},
[&]() -> void { downstream_request = &codec_client->startRequest(big_headers, *response); },
[&]() -> void { response->waitForReset(); }, [&]() -> void { codec_client->close(); }});
[&]() -> void { codec_client_->startRequest(big_headers, *response_); },
[&]() -> void { response_->waitForReset(); }, [&]() -> void { codec_client_->close(); }});
}

TEST_P(Http2IntegrationTest, DownstreamResetBeforeResponseComplete) {
Expand Down Expand Up @@ -166,26 +163,25 @@ TEST_P(Http2IntegrationTest, BadFrame) {
}

TEST_P(Http2IntegrationTest, GoAway) {
IntegrationCodecClientPtr codec_client;
Http::StreamEncoder* encoder;
IntegrationStreamDecoderPtr response(new IntegrationStreamDecoder(*dispatcher_));
executeActions(
{[&]() -> void {
codec_client = makeHttpConnection(lookupPort("http"), Http::CodecClient::Type::HTTP2);
},
[&]() -> void {
encoder = &codec_client->startRequest(Http::TestHeaderMapImpl{{":method", "GET"},
{":path", "/healthcheck"},
{":scheme", "http"},
{":authority", "host"}},
*response);
},
[&]() -> void { codec_client->goAway(); },
[&]() -> void { codec_client->sendData(*encoder, 0, true); },
[&]() -> void { response->waitForEndStream(); }, [&]() -> void { codec_client->close(); }});

EXPECT_TRUE(response->complete());
EXPECT_STREQ("200", response->headers().Status()->value().c_str());
executeActions({[&]() -> void {
codec_client_ =
makeHttpConnection(lookupPort("http"), Http::CodecClient::Type::HTTP2);
},
[&]() -> void {
request_encoder_ = &codec_client_->startRequest(
Http::TestHeaderMapImpl{{":method", "GET"},
{":path", "/healthcheck"},
{":scheme", "http"},
{":authority", "host"}},
*response_);
},
[&]() -> void { codec_client_->goAway(); },
[&]() -> void { codec_client_->sendData(*request_encoder_, 0, true); },
[&]() -> void { response_->waitForEndStream(); },
[&]() -> void { codec_client_->close(); }});

EXPECT_TRUE(response_->complete());
EXPECT_STREQ("200", response_->headers().Status()->value().c_str());
}

TEST_P(Http2IntegrationTest, Trailers) { testTrailers(1024, 2048); }
Expand All @@ -194,7 +190,6 @@ TEST_P(Http2IntegrationTest, TrailersGiantBody) { testTrailers(1024 * 1024, 1024

void Http2IntegrationTest::simultaneousRequest(uint32_t port, int32_t request1_bytes,
int32_t request2_bytes) {
IntegrationCodecClientPtr codec_client;
FakeHttpConnectionPtr fake_upstream_connection1;
FakeHttpConnectionPtr fake_upstream_connection2;
Http::StreamEncoder* encoder1;
Expand All @@ -204,14 +199,15 @@ void Http2IntegrationTest::simultaneousRequest(uint32_t port, int32_t request1_b
FakeStreamPtr upstream_request1;
FakeStreamPtr upstream_request2;
executeActions(
{[&]() -> void { codec_client = makeHttpConnection(port, Http::CodecClient::Type::HTTP2); },
{[&]() -> void { codec_client_ = makeHttpConnection(port, Http::CodecClient::Type::HTTP2); },
// Start request 1
[&]() -> void {
encoder1 = &codec_client->startRequest(Http::TestHeaderMapImpl{{":method", "POST"},
{":path", "/test/long/url"},
{":scheme", "http"},
{":authority", "host"}},
*response1);
encoder1 =
&codec_client_->startRequest(Http::TestHeaderMapImpl{{":method", "POST"},
{":path", "/test/long/url"},
{":scheme", "http"},
{":authority", "host"}},
*response1);
},
[&]() -> void {
fake_upstream_connection1 = fake_upstreams_[0]->waitForHttpConnection(*dispatcher_);
Expand All @@ -221,11 +217,12 @@ void Http2IntegrationTest::simultaneousRequest(uint32_t port, int32_t request1_b
// Start request 2
[&]() -> void {
response2.reset(new IntegrationStreamDecoder(*dispatcher_));
encoder2 = &codec_client->startRequest(Http::TestHeaderMapImpl{{":method", "POST"},
{":path", "/test/long/url"},
{":scheme", "http"},
{":authority", "host"}},
*response2);
encoder2 =
&codec_client_->startRequest(Http::TestHeaderMapImpl{{":method", "POST"},
{":path", "/test/long/url"},
{":scheme", "http"},
{":authority", "host"}},
*response2);
},
[&]() -> void {
fake_upstream_connection2 = fake_upstreams_[0]->waitForHttpConnection(*dispatcher_);
Expand All @@ -234,14 +231,14 @@ void Http2IntegrationTest::simultaneousRequest(uint32_t port, int32_t request1_b

// Finish request 1
[&]() -> void {
codec_client->sendData(*encoder1, request1_bytes, true);
codec_client_->sendData(*encoder1, request1_bytes, true);

},
[&]() -> void { upstream_request1->waitForEndStream(*dispatcher_); },

// Finish request 2
[&]() -> void {
codec_client->sendData(*encoder2, request2_bytes, true);
codec_client_->sendData(*encoder2, request2_bytes, true);

},
[&]() -> void { upstream_request2->waitForEndStream(*dispatcher_); },
Expand Down Expand Up @@ -277,7 +274,7 @@ void Http2IntegrationTest::simultaneousRequest(uint32_t port, int32_t request1_b
},

// Cleanup both downstream and upstream
[&]() -> void { codec_client->close(); },
[&]() -> void { codec_client_->close(); },
[&]() -> void { fake_upstream_connection1->close(); },
[&]() -> void { fake_upstream_connection1->waitForDisconnect(); },
[&]() -> void { fake_upstream_connection2->close(); },
Expand Down
Loading

0 comments on commit a8507f6

Please sign in to comment.