TEST_P(DownstreamProtocolIntegrationTest, HandleDownstreamSocketFail) {
  // Make sure for HTTP/3 Envoy will use sendmsg, so the write_matcher will work.
  NoUdpGso reject_gso_;
  TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls{&reject_gso_};
  ASSERT(!Api::OsSysCallsSingleton::get().supportsUdpGso());
  SocketInterfaceSwap socket_swap;

  initialize();
  codec_client_ = makeHttpConnection(lookupPort("http"));
  auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
  waitForNextUpstreamRequest();

  // Makes us have Envoy's writes to downstream return EBADF
  Network::IoSocketError* ebadf = Network::IoSocketError::getIoSocketEbadfInstance();
  socket_swap.write_matcher_->setSourcePort(lookupPort("http"));
  socket_swap.write_matcher_->setWriteOverride(ebadf);
  upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);

  if (downstreamProtocol() == Http::CodecType::HTTP3) {
    // For HTTP/3 since the packets are black holed, there is no client side
    // indication of connection close. Wait on Envoy stats instead.
    test_server_->waitForCounterEq("http.config_test.downstream_rq_rx_reset", 1);
    codec_client_->close();
  } else {
    ASSERT_TRUE(codec_client_->waitForDisconnect());
  }
  socket_swap.write_matcher_->setWriteOverride(nullptr);
  // Shut down the server and upstreams before os_calls goes out of scope to avoid syscalls
  // during its removal.
  test_server_.reset();
  cleanupUpstreamAndDownstream();
  fake_upstreams_.clear();
}
TEST_P(DownstreamProtocolIntegrationTest, HandleDownstreamSocketFail) {
   
  NoUdpGso reject_gso_;
  TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls{&reject_gso_};
  ASSERT(!Api::OsSysCallsSingleton::get().supportsUdpGso());
  SocketInterfaceSwap socket_swap;

  initialize();
  codec_client_ = makeHttpConnection(lookupPort("http"));
  auto response = codec_client_->makeHeaderOnlyRequest(default_request_headers_);
  waitForNextUpstreamRequest();

   
  Network::IoSocketError* ebadf = Network::IoSocketError::getIoSocketEbadfInstance();
  socket_swap.write_matcher_->setSourcePort(lookupPort("http"));
  socket_swap.write_matcher_->setWriteOverride(ebadf);
  upstream_request_->encodeHeaders(Http::TestResponseHeaderMapImpl{{":status", "200"}}, true);

  if (downstreamProtocol() == Http::CodecType::HTTP3) {
     
     
    test_server_->waitForCounterEq("http.config_test.downstream_rq_rx_reset", 1);
    codec_client_->close();
  } else {
    ASSERT_TRUE(codec_client_->waitForDisconnect());
  }
  socket_swap.write_matcher_->setWriteOverride(nullptr);
   
   
  test_server_.reset();
  cleanupUpstreamAndDownstream();
  fake_upstreams_.clear();
}