connectivity_state_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <string.h>
22 
23 #include <gtest/gtest.h>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/support/log.h>
27 
31 
32 namespace grpc_core {
33 namespace {
34 
39  EXPECT_STREQ("TRANSIENT_FAILURE",
42 }
43 
44 class Watcher : public ConnectivityStateWatcherInterface {
45  public:
47  bool* destroyed = nullptr)
48  : count_(count),
49  output_(output),
50  status_(status),
51  destroyed_(destroyed) {}
52 
53  ~Watcher() override {
54  if (destroyed_ != nullptr) *destroyed_ = true;
55  }
56 
57  void Notify(grpc_connectivity_state new_state,
58  const absl::Status& status) override {
59  ++*count_;
60  *output_ = new_state;
61  *status_ = status;
62  }
63 
64  private:
65  int* count_;
68  bool* destroyed_;
69 };
70 
71 TEST(StateTracker, SetAndGetState) {
72  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_CONNECTING,
73  absl::Status());
75  EXPECT_TRUE(tracker.status().ok());
76  tracker.SetState(GRPC_CHANNEL_READY, absl::Status(), "whee");
78  EXPECT_TRUE(tracker.status().ok());
79  absl::Status transient_failure_status(absl::StatusCode::kUnavailable,
80  "status for testing");
81  tracker.SetState(GRPC_CHANNEL_TRANSIENT_FAILURE, transient_failure_status,
82  "reason");
84  EXPECT_EQ(tracker.status(), transient_failure_status);
85 }
86 
87 TEST(StateTracker, NotificationUponAddingWatcher) {
88  int count = 0;
91  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_CONNECTING);
92  tracker.AddWatcher(GRPC_CHANNEL_IDLE,
93  MakeOrphanable<Watcher>(&count, &state, &status));
94  EXPECT_EQ(count, 1);
97 }
98 
99 TEST(StateTracker, NotificationUponAddingWatcherWithTransientFailure) {
100  int count = 0;
103  absl::Status transient_failure_status(absl::StatusCode::kUnavailable,
104  "status for testing");
105  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_TRANSIENT_FAILURE,
106  transient_failure_status);
107  tracker.AddWatcher(GRPC_CHANNEL_IDLE,
108  MakeOrphanable<Watcher>(&count, &state, &status));
109  EXPECT_EQ(count, 1);
111  EXPECT_EQ(status, transient_failure_status);
112 }
113 
114 TEST(StateTracker, NotificationUponStateChange) {
115  int count = 0;
118  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE);
119  tracker.AddWatcher(GRPC_CHANNEL_IDLE,
120  MakeOrphanable<Watcher>(&count, &state, &status));
121  EXPECT_EQ(count, 0);
123  EXPECT_TRUE(status.ok());
124  absl::Status transient_failure_status(absl::StatusCode::kUnavailable,
125  "status for testing");
126  tracker.SetState(GRPC_CHANNEL_TRANSIENT_FAILURE, transient_failure_status,
127  "whee");
128  EXPECT_EQ(count, 1);
130  EXPECT_EQ(status, transient_failure_status);
131 }
132 
133 TEST(StateTracker, SubscribeThenUnsubscribe) {
134  int count = 0;
137  bool destroyed = false;
138  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE);
139  ConnectivityStateWatcherInterface* watcher =
140  new Watcher(&count, &state, &status, &destroyed);
141  tracker.AddWatcher(GRPC_CHANNEL_IDLE,
142  OrphanablePtr<ConnectivityStateWatcherInterface>(watcher));
143  // No initial notification, since we started the watch from the
144  // current state.
145  EXPECT_EQ(count, 0);
147  EXPECT_TRUE(status.ok());
148  // Cancel watch. This should not generate another notification.
149  tracker.RemoveWatcher(watcher);
150  EXPECT_TRUE(destroyed);
151  EXPECT_EQ(count, 0);
153  EXPECT_TRUE(status.ok());
154 }
155 
156 TEST(StateTracker, OrphanUponShutdown) {
157  int count = 0;
160  bool destroyed = false;
161  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE);
162  ConnectivityStateWatcherInterface* watcher =
163  new Watcher(&count, &state, &status, &destroyed);
164  tracker.AddWatcher(GRPC_CHANNEL_IDLE,
165  OrphanablePtr<ConnectivityStateWatcherInterface>(watcher));
166  // No initial notification, since we started the watch from the
167  // current state.
168  EXPECT_EQ(count, 0);
170  EXPECT_TRUE(status.ok());
171  // Set state to SHUTDOWN.
172  tracker.SetState(GRPC_CHANNEL_SHUTDOWN, absl::Status(), "shutting down");
173  EXPECT_TRUE(destroyed);
174  EXPECT_EQ(count, 1);
176  EXPECT_TRUE(status.ok());
177 }
178 
179 TEST(StateTracker, AddWhenAlreadyShutdown) {
180  int count = 0;
183  bool destroyed = false;
184  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_SHUTDOWN,
185  absl::Status());
186  ConnectivityStateWatcherInterface* watcher =
187  new Watcher(&count, &state, &status, &destroyed);
188  tracker.AddWatcher(GRPC_CHANNEL_IDLE,
189  OrphanablePtr<ConnectivityStateWatcherInterface>(watcher));
190  EXPECT_TRUE(destroyed);
191  EXPECT_EQ(count, 1);
193  EXPECT_TRUE(status.ok());
194 }
195 
196 TEST(StateTracker, NotifyShutdownAtDestruction) {
197  int count = 0;
200  {
201  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE);
202  tracker.AddWatcher(GRPC_CHANNEL_IDLE,
203  MakeOrphanable<Watcher>(&count, &state, &status));
204  // No initial notification, since we started the watch from the
205  // current state.
206  EXPECT_EQ(count, 0);
208  }
209  // Upon tracker destruction, we get a notification for SHUTDOWN.
210  EXPECT_EQ(count, 1);
212 }
213 
214 TEST(StateTracker, DoNotNotifyShutdownAtDestructionIfAlreadyInShutdown) {
215  int count = 0;
218  {
219  ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_SHUTDOWN);
220  tracker.AddWatcher(GRPC_CHANNEL_SHUTDOWN,
221  MakeOrphanable<Watcher>(&count, &state, &status));
222  // No initial notification, since we started the watch from the
223  // current state.
224  EXPECT_EQ(count, 0);
226  }
227  // No additional notification upon tracker destruction, since we were
228  // already in state SHUTDOWN.
229  EXPECT_EQ(count, 0);
231 }
232 
233 } // namespace
234 } // namespace grpc_core
235 
236 int main(int argc, char** argv) {
237  grpc::testing::TestEnvironment env(&argc, argv);
238  ::testing::InitGoogleTest(&argc, argv);
239  grpc_init();
242  int ret = RUN_ALL_TESTS();
243  grpc_shutdown();
244  return ret;
245 }
GRPC_CHANNEL_READY
@ GRPC_CHANNEL_READY
Definition: include/grpc/impl/codegen/connectivity_state.h:36
log.h
destroyed_
bool * destroyed_
Definition: connectivity_state_test.cc:68
generate.env
env
Definition: generate.py:37
connectivity_state.h
Watcher
Definition: lame_client_test.cc:32
grpc_core
Definition: call_metric_recorder.h:31
string.h
tracer_util.h
GRPC_CHANNEL_TRANSIENT_FAILURE
@ GRPC_CHANNEL_TRANSIENT_FAILURE
Definition: include/grpc/impl/codegen/connectivity_state.h:38
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
status
absl::Status status
Definition: rls.cc:251
grpc_core::grpc_connectivity_state_trace
TraceFlag grpc_connectivity_state_trace(false, "connectivity_state")
Definition: src/core/lib/transport/connectivity_state.h:39
grpc_connectivity_state
grpc_connectivity_state
Definition: include/grpc/impl/codegen/connectivity_state.h:30
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc.h
GRPC_CHANNEL_IDLE
@ GRPC_CHANNEL_IDLE
Definition: include/grpc/impl/codegen/connectivity_state.h:32
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
GRPC_CHANNEL_CONNECTING
@ GRPC_CHANNEL_CONNECTING
Definition: include/grpc/impl/codegen/connectivity_state.h:34
test_config.h
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
count_
int * count_
Definition: connectivity_state_test.cc:65
grpc_core::testing::grpc_tracer_enable_flag
void grpc_tracer_enable_flag(TraceFlag *flag)
Definition: tracer_util.cc:25
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
grpc_core::ConnectivityStateName
const char * ConnectivityStateName(grpc_connectivity_state state)
Definition: connectivity_state.cc:38
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
main
int main(int argc, char **argv)
Definition: connectivity_state_test.cc:236
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
exec_ctx.h
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
watcher
ClusterWatcher * watcher
Definition: cds.cc:148
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
tracker
SessionTracker * tracker
Definition: ssl_session_cache_test.cc:38
GRPC_CHANNEL_SHUTDOWN
@ GRPC_CHANNEL_SHUTDOWN
Definition: include/grpc/impl/codegen/connectivity_state.h:40
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
absl::StatusCode::kUnavailable
@ kUnavailable
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
output_
grpc_connectivity_state * output_
Definition: connectivity_state_test.cc:66
status_
absl::Status * status_
Definition: connectivity_state_test.cc:67


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:01