From 875066b61e3b57af4bb1d6e36aabe95a4f6ba4f7 Mon Sep 17 00:00:00 2001 From: Eugene Ostroukhov Date: Tue, 27 Dec 2022 09:25:38 -0800 Subject: [PATCH] tests: Validate subchannel state transitions (#31971) --- .../lb_policy/lb_policy_test_lib.h | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/test/core/client_channel/lb_policy/lb_policy_test_lib.h b/test/core/client_channel/lb_policy/lb_policy_test_lib.h index c9ba77b06d9..fc1293c9dac 100644 --- a/test/core/client_channel/lb_policy/lb_policy_test_lib.h +++ b/test/core/client_channel/lb_policy/lb_policy_test_lib.h @@ -41,6 +41,7 @@ #include "absl/types/optional.h" #include "absl/types/span.h" #include "absl/types/variant.h" +#include "gmock/gmock.h" #include "gtest/gtest.h" #include @@ -160,10 +161,49 @@ class LoadBalancingPolicyTest : public ::testing::Test { const std::string& address() const { return address_; } + void AssertValidConnectivityStateTransition( + grpc_connectivity_state from_state, grpc_connectivity_state to_state, + SourceLocation location = SourceLocation()) { + switch (from_state) { + case GRPC_CHANNEL_IDLE: + ASSERT_EQ(to_state, GRPC_CHANNEL_CONNECTING) + << ConnectivityStateName(from_state) << "=>" + << ConnectivityStateName(to_state) << "\n" + << location.file() << ":" << location.line(); + break; + case GRPC_CHANNEL_CONNECTING: + ASSERT_THAT(to_state, + ::testing::AnyOf(GRPC_CHANNEL_READY, + GRPC_CHANNEL_TRANSIENT_FAILURE)) + << ConnectivityStateName(from_state) << "=>" + << ConnectivityStateName(to_state) << "\n" + << location.file() << ":" << location.line(); + break; + case GRPC_CHANNEL_READY: + ASSERT_EQ(to_state, GRPC_CHANNEL_IDLE) + << ConnectivityStateName(from_state) << "=>" + << ConnectivityStateName(to_state) << "\n" + << location.file() << ":" << location.line(); + break; + case GRPC_CHANNEL_TRANSIENT_FAILURE: + ASSERT_EQ(to_state, GRPC_CHANNEL_IDLE) + << ConnectivityStateName(from_state) << "=>" + << ConnectivityStateName(to_state) << "\n" + << location.file() << ":" << location.line(); + break; + default: + FAIL() << ConnectivityStateName(from_state) << "=>" + << ConnectivityStateName(to_state) << "\n" + << location.file() << ":" << location.line(); + break; + } + } + // Sets the connectivity state for this subchannel. The updated state // will be reported to all associated SubchannelInterface objects. void SetConnectivityState(grpc_connectivity_state state, - const absl::Status& status = absl::OkStatus()) { + const absl::Status& status = absl::OkStatus(), + SourceLocation location = SourceLocation()) { if (state == GRPC_CHANNEL_TRANSIENT_FAILURE) { EXPECT_FALSE(status.ok()) << "bug in test: TRANSIENT_FAILURE must have non-OK status"; @@ -173,6 +213,8 @@ class LoadBalancingPolicyTest : public ::testing::Test { << " must have OK status: " << status; } MutexLock lock(&mu_); + AssertValidConnectivityStateTransition(state_tracker_.state(), state, + location); state_tracker_.SetState(state, status, "set from test"); }