TEST_P(ProtocolIntegrationTest, HandleUpstreamSocketFail) {
  SocketInterfaceSwap socket_swap;

  useAccessLog("%RESPONSE_CODE_DETAILS%");
  initialize();
  codec_client_ = makeHttpConnection(lookupPort("http"));
  auto encoder_decoder = codec_client_->startRequest(default_request_headers_);
  auto downstream_request = &encoder_decoder.first;
  auto response = std::move(encoder_decoder.second);

  // Make sure the headers made it through.
  waitForNextUpstreamConnection(std::vector<uint64_t>{0}, TestUtility::DefaultTimeout,
                                fake_upstream_connection_);
  AssertionResult result =
      fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_);
  RELEASE_ASSERT(result, result.message());

  // Makes us have Envoy's writes to upstream return EBADF
  Network::IoSocketError* ebadf = Network::IoSocketError::getIoSocketEbadfInstance();
  socket_swap.write_matcher_->setDestinationPort(fake_upstreams_[0]->localAddress()->ip()->port());
  socket_swap.write_matcher_->setWriteOverride(ebadf);

  Buffer::OwnedImpl data("HTTP body content goes here");
  codec_client_->sendData(*downstream_request, data, true);

  ASSERT_TRUE(response->waitForEndStream());
  EXPECT_THAT(waitForAccessLog(access_log_name_),
              HasSubstr("upstream_reset_before_response_started{connection_termination}"));
  EXPECT_TRUE(response->complete());
  EXPECT_EQ("503", response->headers().getStatusValue());
  socket_swap.write_matcher_->setWriteOverride(nullptr);
  // Shut down the server before os_calls goes out of scope to avoid syscalls
  // during its removal.
  test_server_.reset();
}
TEST_P(ProtocolIntegrationTest, HandleUpstreamSocketFail) {
  SocketInterfaceSwap socket_swap;

  useAccessLog("%RESPONSE_CODE_DETAILS%");
  initialize();
  codec_client_ = makeHttpConnection(lookupPort("http"));
  auto encoder_decoder = codec_client_->startRequest(default_request_headers_);
  auto downstream_request = &encoder_decoder.first;
  auto response = std::move(encoder_decoder.second);

   
  waitForNextUpstreamConnection(std::vector<uint64_t>{0}, TestUtility::DefaultTimeout,
                                fake_upstream_connection_);
  AssertionResult result =
      fake_upstream_connection_->waitForNewStream(*dispatcher_, upstream_request_);
  RELEASE_ASSERT(result, result.message());

   
  Network::IoSocketError* ebadf = Network::IoSocketError::getIoSocketEbadfInstance();
  socket_swap.write_matcher_->setDestinationPort(fake_upstreams_[0]->localAddress()->ip()->port());
  socket_swap.write_matcher_->setWriteOverride(ebadf);

  Buffer::OwnedImpl data("HTTP body content goes here");
  codec_client_->sendData(*downstream_request, data, true);

  ASSERT_TRUE(response->waitForEndStream());
  EXPECT_THAT(waitForAccessLog(access_log_name_),
              HasSubstr("upstream_reset_before_response_started{connection_termination}"));
  EXPECT_TRUE(response->complete());
  EXPECT_EQ("503", response->headers().getStatusValue());
  socket_swap.write_matcher_->setWriteOverride(nullptr);
   
   
  test_server_.reset();
}