@ -78,6 +78,8 @@ namespace grpc {
namespace testing {
namespace {
constexpr char kRequestMessage [ ] = " Live long and prosper. " ;
gpr_atm g_connection_delay_ms ;
void tcp_client_connect_with_delay ( grpc_closure * closure , grpc_endpoint * * ep ,
@ -232,7 +234,6 @@ class ClientLbEnd2endTest : public ::testing::Test {
protected :
ClientLbEnd2endTest ( )
: server_host_ ( " localhost " ) ,
kRequestMessage_ ( " Live long and prosper. " ) ,
creds_ ( new SecureChannelCredentials (
grpc_fake_transport_security_credentials_create ( ) ) ) { }
@ -316,21 +317,22 @@ class ClientLbEnd2endTest : public ::testing::Test {
bool SendRpc (
const std : : unique_ptr < grpc : : testing : : EchoTestService : : Stub > & stub ,
EchoResponse * response = nullptr , int timeout_ms = 1000 ,
Status * result = nullptr , bool wait_for_ready = false ) {
const bool local_response = ( response = = nullptr ) ;
if ( local_response ) response = new EchoResponse ;
EchoRequest request ;
request . set_message ( kRequestMessage_ ) ;
request . mutable_param ( ) - > set_echo_metadata ( true ) ;
Status * result = nullptr , bool wait_for_ready = false ,
EchoRequest * request = nullptr ) {
EchoResponse local_response ;
if ( response = = nullptr ) response = & local_response ;
EchoRequest local_request ;
if ( request = = nullptr ) request = & local_request ;
request - > set_message ( kRequestMessage ) ;
request - > mutable_param ( ) - > set_echo_metadata ( true ) ;
ClientContext context ;
context . set_deadline ( grpc_timeout_milliseconds_to_deadline ( timeout_ms ) ) ;
if ( wait_for_ready ) context . set_wait_for_ready ( true ) ;
context . AddMetadata ( " foo " , " 1 " ) ;
context . AddMetadata ( " bar " , " 2 " ) ;
context . AddMetadata ( " baz " , " 3 " ) ;
Status status = stub - > Echo ( & context , request , response ) ;
Status status = stub - > Echo ( & context , * request , response ) ;
if ( result ! = nullptr ) * result = status ;
if ( local_response ) delete response ;
return status . ok ( ) ;
}
@ -345,7 +347,7 @@ class ClientLbEnd2endTest : public ::testing::Test {
< < " \n "
< < " Error: " < < status . error_message ( ) < < " "
< < status . error_details ( ) ;
ASSERT_EQ ( response . message ( ) , kRequestMessage_ )
ASSERT_EQ ( response . message ( ) , kRequestMessage )
< < " From " < < location . file ( ) < < " : " < < location . line ( ) ;
if ( ! success ) abort ( ) ;
}
@ -483,7 +485,6 @@ class ClientLbEnd2endTest : public ::testing::Test {
const std : : string server_host_ ;
std : : vector < std : : unique_ptr < ServerData > > servers_ ;
const std : : string kRequestMessage_ ;
std : : shared_ptr < ChannelCredentials > creds_ ;
bool ipv6_only_ = false ;
} ;
@ -1832,14 +1833,19 @@ class ClientLbInterceptTrailingMetadataTest : public ClientLbEnd2endTest {
return trailers_intercepted_ ;
}
const grpc_core : : MetadataVector & trailing_metadata ( ) {
absl : : Status last_status ( ) {
grpc : : internal : : MutexLock lock ( & mu_ ) ;
return last_status_ ;
}
grpc_core : : MetadataVector trailing_metadata ( ) {
grpc : : internal : : MutexLock lock ( & mu_ ) ;
return trailing_metadata_ ;
return std : : move ( trailing_metadata_ ) ;
}
const xds : : data : : orca : : v3 : : OrcaLoadReport * backend_load_report ( ) {
std : : unique_ptr < xds : : data : : orca : : v3 : : OrcaLoadReport > backend_load_report ( ) {
grpc : : internal : : MutexLock lock ( & mu_ ) ;
return load_report_ . get ( ) ;
return std : : move ( load_report_ ) ;
}
private :
@ -1848,6 +1854,7 @@ class ClientLbInterceptTrailingMetadataTest : public ClientLbEnd2endTest {
const auto * backend_metric_data = args_seen . backend_metric_data ;
ClientLbInterceptTrailingMetadataTest * self = current_test_instance_ ;
grpc : : internal : : MutexLock lock ( & self - > mu_ ) ;
self - > last_status_ = args_seen . status ;
self - > trailers_intercepted_ + + ;
self - > trailing_metadata_ = args_seen . metadata ;
if ( backend_metric_data ! = nullptr ) {
@ -1872,6 +1879,7 @@ class ClientLbInterceptTrailingMetadataTest : public ClientLbEnd2endTest {
static ClientLbInterceptTrailingMetadataTest * current_test_instance_ ;
grpc : : internal : : Mutex mu_ ;
int trailers_intercepted_ = 0 ;
absl : : Status last_status_ ;
grpc_core : : MetadataVector trailing_metadata_ ;
std : : unique_ptr < xds : : data : : orca : : v3 : : OrcaLoadReport > load_report_ ;
} ;
@ -1879,13 +1887,74 @@ class ClientLbInterceptTrailingMetadataTest : public ClientLbEnd2endTest {
ClientLbInterceptTrailingMetadataTest *
ClientLbInterceptTrailingMetadataTest : : current_test_instance_ = nullptr ;
TEST_F ( ClientLbInterceptTrailingMetadataTest , StatusOk ) {
StartServers ( 1 ) ;
auto response_generator = BuildResolverResponseGenerator ( ) ;
auto channel =
BuildChannel ( " intercept_trailing_metadata_lb " , response_generator ) ;
auto stub = BuildStub ( channel ) ;
response_generator . SetNextResolution ( GetServersPorts ( ) ) ;
// Send an OK RPC.
CheckRpcSendOk ( stub , DEBUG_LOCATION ) ;
// Check LB policy name for the channel.
EXPECT_EQ ( " intercept_trailing_metadata_lb " ,
channel - > GetLoadBalancingPolicyName ( ) ) ;
EXPECT_EQ ( 1 , trailers_intercepted ( ) ) ;
EXPECT_EQ ( absl : : OkStatus ( ) , last_status ( ) ) ;
}
TEST_F ( ClientLbInterceptTrailingMetadataTest , StatusFailed ) {
StartServers ( 1 ) ;
auto response_generator = BuildResolverResponseGenerator ( ) ;
auto channel =
BuildChannel ( " intercept_trailing_metadata_lb " , response_generator ) ;
auto stub = BuildStub ( channel ) ;
response_generator . SetNextResolution ( GetServersPorts ( ) ) ;
EchoRequest request ;
auto * expected_error = request . mutable_param ( ) - > mutable_expected_error ( ) ;
expected_error - > set_code ( GRPC_STATUS_PERMISSION_DENIED ) ;
expected_error - > set_error_message ( " bummer, man " ) ;
Status status ;
SendRpc ( stub , /*response=*/ nullptr , /*timeout_ms=*/ 1000 , & status ,
/*wait_for_ready=*/ false , & request ) ;
EXPECT_EQ ( status . error_code ( ) , GRPC_STATUS_PERMISSION_DENIED ) ;
EXPECT_EQ ( status . error_message ( ) , " bummer, man " ) ;
absl : : Status status_seen_by_lb = last_status ( ) ;
EXPECT_EQ ( status_seen_by_lb . code ( ) , absl : : StatusCode : : kPermissionDenied ) ;
EXPECT_EQ ( status_seen_by_lb . message ( ) , " bummer, man " ) ;
}
TEST_F ( ClientLbInterceptTrailingMetadataTest ,
StatusCancelledWithoutStartingRecvTrailingMetadata ) {
StartServers ( 1 ) ;
auto response_generator = BuildResolverResponseGenerator ( ) ;
auto channel =
BuildChannel ( " intercept_trailing_metadata_lb " , response_generator ) ;
response_generator . SetNextResolution ( GetServersPorts ( ) ) ;
auto stub = BuildStub ( channel ) ;
{
// Start a stream (sends initial metadata) and then cancel without
// calling Finish().
ClientContext ctx ;
auto stream = stub - > BidiStream ( & ctx ) ;
ctx . TryCancel ( ) ;
}
// Check status seen by LB policy.
EXPECT_EQ ( 1 , trailers_intercepted ( ) ) ;
absl : : Status status_seen_by_lb = last_status ( ) ;
EXPECT_EQ ( status_seen_by_lb . code ( ) , absl : : StatusCode : : kCancelled ) ;
EXPECT_EQ ( status_seen_by_lb . message ( ) , " call cancelled " ) ;
}
TEST_F ( ClientLbInterceptTrailingMetadataTest , InterceptsRetriesDisabled ) {
const int kNumServers = 1 ;
const int kNumRpcs = 10 ;
StartServers ( kNumServers ) ;
auto response_generator = BuildResolverResponseGenerator ( ) ;
auto channel =
BuildChannel ( " intercept_trailing_metadata_lb " , response_generator ) ;
ChannelArguments channel_args ;
channel_args . SetInt ( GRPC_ARG_ENABLE_RETRIES , 0 ) ;
auto channel = BuildChannel ( " intercept_trailing_metadata_lb " ,
response_generator , channel_args ) ;
auto stub = BuildStub ( channel ) ;
response_generator . SetNextResolution ( GetServersPorts ( ) ) ;
for ( size_t i = 0 ; i < kNumRpcs ; + + i ) {
@ -1971,7 +2040,7 @@ TEST_F(ClientLbInterceptTrailingMetadataTest, BackendMetricData) {
response_generator . SetNextResolution ( GetServersPorts ( ) ) ;
for ( size_t i = 0 ; i < kNumRpcs ; + + i ) {
CheckRpcSendOk ( stub , DEBUG_LOCATION ) ;
auto * actual = backend_load_report ( ) ;
auto actual = backend_load_report ( ) ;
ASSERT_NE ( actual , nullptr ) ;
// TODO(roth): Change this to use EqualsProto() once that becomes
// available in OSS.