| 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 |
|
| 19 |
#include "src/core/lib/transport/connectivity_state.h" |
| 20 |
|
| 21 |
#include "absl/strings/string_view.h" |
| 22 |
#include "gtest/gtest.h" |
| 23 |
|
| 24 |
#include <grpc/grpc.h> |
| 25 |
|
| 26 |
#include "test/core/util/test_config.h" |
| 27 |
#include "test/core/util/tracer_util.h" |
| 28 |
|
| 29 |
namespace grpc_core { |
| 30 |
namespace { |
| 31 |
|
| 32 |
TEST(ConnectivityStateName, Basic) { |
| 33 |
EXPECT_STREQ("IDLE", ConnectivityStateName(GRPC_CHANNEL_IDLE)); |
| 34 |
EXPECT_STREQ("CONNECTING", ConnectivityStateName(GRPC_CHANNEL_CONNECTING)); |
| 35 |
EXPECT_STREQ("READY", ConnectivityStateName(GRPC_CHANNEL_READY)); |
| 36 |
EXPECT_STREQ("TRANSIENT_FAILURE", |
| 37 |
ConnectivityStateName(GRPC_CHANNEL_TRANSIENT_FAILURE)); |
| 38 |
EXPECT_STREQ("SHUTDOWN", ConnectivityStateName(GRPC_CHANNEL_SHUTDOWN)); |
| 39 |
} |
| 40 |
|
| 41 |
class Watcher : public ConnectivityStateWatcherInterface { |
| 42 |
public: |
| 43 |
Watcher(int* count, grpc_connectivity_state* output, absl::Status* status, |
| 44 |
bool* destroyed = nullptr) |
| 45 |
: count_(count), |
| 46 |
output_(output), |
| 47 |
status_(status), |
| 48 |
destroyed_(destroyed) {} |
| 49 |
|
| 50 |
~Watcher() override { |
| 51 |
if (destroyed_ != nullptr) *destroyed_ = true; |
| 52 |
} |
| 53 |
|
| 54 |
void Notify(grpc_connectivity_state new_state, |
| 55 |
const absl::Status& status) override { |
| 56 |
++*count_; |
| 57 |
*output_ = new_state; |
| 58 |
*status_ = status; |
| 59 |
} |
| 60 |
|
| 61 |
private: |
| 62 |
int* count_; |
| 63 |
grpc_connectivity_state* output_; |
| 64 |
absl::Status* status_; |
| 65 |
bool* destroyed_; |
| 66 |
}; |
| 67 |
|
| 68 |
TEST(StateTracker, SetAndGetState) { |
| 69 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_CONNECTING, |
| 70 |
absl::Status()); |
| 71 |
EXPECT_EQ(tracker.state(), GRPC_CHANNEL_CONNECTING); |
| 72 |
EXPECT_TRUE(tracker.status().ok()); |
| 73 |
tracker.SetState(GRPC_CHANNEL_READY, absl::Status(), "whee"); |
| 74 |
EXPECT_EQ(tracker.state(), GRPC_CHANNEL_READY); |
| 75 |
EXPECT_TRUE(tracker.status().ok()); |
| 76 |
absl::Status transient_failure_status(absl::StatusCode::kUnavailable, |
| 77 |
"status for testing"); |
| 78 |
tracker.SetState(GRPC_CHANNEL_TRANSIENT_FAILURE, transient_failure_status, |
| 79 |
"reason"); |
| 80 |
EXPECT_EQ(tracker.state(), GRPC_CHANNEL_TRANSIENT_FAILURE); |
| 81 |
EXPECT_EQ(tracker.status(), transient_failure_status); |
| 82 |
} |
| 83 |
|
| 84 |
TEST(StateTracker, NotificationUponAddingWatcher) { |
| 85 |
int count = 0; |
| 86 |
grpc_connectivity_state state = GRPC_CHANNEL_IDLE; |
| 87 |
absl::Status status; |
| 88 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_CONNECTING); |
| 89 |
tracker.AddWatcher(GRPC_CHANNEL_IDLE, |
| 90 |
MakeOrphanable<Watcher>(&count, &state, &status)); |
| 91 |
EXPECT_EQ(count, 1); |
| 92 |
EXPECT_EQ(state, GRPC_CHANNEL_CONNECTING); |
| 93 |
EXPECT_TRUE(status.ok()); |
| 94 |
} |
| 95 |
|
| 96 |
TEST(StateTracker, NotificationUponAddingWatcherWithTransientFailure) { |
| 97 |
int count = 0; |
| 98 |
grpc_connectivity_state state = GRPC_CHANNEL_IDLE; |
| 99 |
absl::Status status; |
| 100 |
absl::Status transient_failure_status(absl::StatusCode::kUnavailable, |
| 101 |
"status for testing"); |
| 102 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_TRANSIENT_FAILURE, |
| 103 |
transient_failure_status); |
| 104 |
tracker.AddWatcher(GRPC_CHANNEL_IDLE, |
| 105 |
MakeOrphanable<Watcher>(&count, &state, &status)); |
| 106 |
EXPECT_EQ(count, 1); |
| 107 |
EXPECT_EQ(state, GRPC_CHANNEL_TRANSIENT_FAILURE); |
| 108 |
EXPECT_EQ(status, transient_failure_status); |
| 109 |
} |
| 110 |
|
| 111 |
TEST(StateTracker, NotificationUponStateChange) { |
| 112 |
int count = 0; |
| 113 |
grpc_connectivity_state state = GRPC_CHANNEL_IDLE; |
| 114 |
absl::Status status; |
| 115 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE); |
| 116 |
tracker.AddWatcher(GRPC_CHANNEL_IDLE, |
| 117 |
MakeOrphanable<Watcher>(&count, &state, &status)); |
| 118 |
EXPECT_EQ(count, 0); |
| 119 |
EXPECT_EQ(state, GRPC_CHANNEL_IDLE); |
| 120 |
EXPECT_TRUE(status.ok()); |
| 121 |
absl::Status transient_failure_status(absl::StatusCode::kUnavailable, |
| 122 |
"status for testing"); |
| 123 |
tracker.SetState(GRPC_CHANNEL_TRANSIENT_FAILURE, transient_failure_status, |
| 124 |
"whee"); |
| 125 |
EXPECT_EQ(count, 1); |
| 126 |
EXPECT_EQ(state, GRPC_CHANNEL_TRANSIENT_FAILURE); |
| 127 |
EXPECT_EQ(status, transient_failure_status); |
| 128 |
} |
| 129 |
|
| 130 |
TEST(StateTracker, SubscribeThenUnsubscribe) { |
| 131 |
int count = 0; |
| 132 |
grpc_connectivity_state state = GRPC_CHANNEL_IDLE; |
| 133 |
absl::Status status; |
| 134 |
bool destroyed = false; |
| 135 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE); |
| 136 |
ConnectivityStateWatcherInterface* watcher = |
| 137 |
new Watcher(&count, &state, &status, &destroyed); |
| 138 |
tracker.AddWatcher(GRPC_CHANNEL_IDLE, |
| 139 |
OrphanablePtr<ConnectivityStateWatcherInterface>(watcher)); |
| 140 |
// No initial notification, since we started the watch from the |
| 141 |
// current state. |
| 142 |
EXPECT_EQ(count, 0); |
| 143 |
EXPECT_EQ(state, GRPC_CHANNEL_IDLE); |
| 144 |
EXPECT_TRUE(status.ok()); |
| 145 |
// Cancel watch. This should not generate another notification. |
| 146 |
tracker.RemoveWatcher(watcher); |
| 147 |
EXPECT_TRUE(destroyed); |
| 148 |
EXPECT_EQ(count, 0); |
| 149 |
EXPECT_EQ(state, GRPC_CHANNEL_IDLE); |
| 150 |
EXPECT_TRUE(status.ok()); |
| 151 |
} |
| 152 |
|
| 153 |
TEST(StateTracker, OrphanUponShutdown) { |
| 154 |
int count = 0; |
| 155 |
grpc_connectivity_state state = GRPC_CHANNEL_IDLE; |
| 156 |
absl::Status status; |
| 157 |
bool destroyed = false; |
| 158 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE); |
| 159 |
ConnectivityStateWatcherInterface* watcher = |
| 160 |
new Watcher(&count, &state, &status, &destroyed); |
| 161 |
tracker.AddWatcher(GRPC_CHANNEL_IDLE, |
| 162 |
OrphanablePtr<ConnectivityStateWatcherInterface>(watcher)); |
| 163 |
// No initial notification, since we started the watch from the |
| 164 |
// current state. |
| 165 |
EXPECT_EQ(count, 0); |
| 166 |
EXPECT_EQ(state, GRPC_CHANNEL_IDLE); |
| 167 |
EXPECT_TRUE(status.ok()); |
| 168 |
// Set state to SHUTDOWN. |
| 169 |
tracker.SetState(GRPC_CHANNEL_SHUTDOWN, absl::Status(), "shutting down"); |
| 170 |
EXPECT_TRUE(destroyed); |
| 171 |
EXPECT_EQ(count, 1); |
| 172 |
EXPECT_EQ(state, GRPC_CHANNEL_SHUTDOWN); |
| 173 |
EXPECT_TRUE(status.ok()); |
| 174 |
} |
| 175 |
|
| 176 |
TEST(StateTracker, AddWhenAlreadyShutdown) { |
| 177 |
int count = 0; |
| 178 |
grpc_connectivity_state state = GRPC_CHANNEL_IDLE; |
| 179 |
absl::Status status; |
| 180 |
bool destroyed = false; |
| 181 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_SHUTDOWN, |
| 182 |
absl::Status()); |
| 183 |
ConnectivityStateWatcherInterface* watcher = |
| 184 |
new Watcher(&count, &state, &status, &destroyed); |
| 185 |
tracker.AddWatcher(GRPC_CHANNEL_IDLE, |
| 186 |
OrphanablePtr<ConnectivityStateWatcherInterface>(watcher)); |
| 187 |
EXPECT_TRUE(destroyed); |
| 188 |
EXPECT_EQ(count, 1); |
| 189 |
EXPECT_EQ(state, GRPC_CHANNEL_SHUTDOWN); |
| 190 |
EXPECT_TRUE(status.ok()); |
| 191 |
} |
| 192 |
|
| 193 |
TEST(StateTracker, NotifyShutdownAtDestruction) { |
| 194 |
int count = 0; |
| 195 |
grpc_connectivity_state state = GRPC_CHANNEL_IDLE; |
| 196 |
absl::Status status; |
| 197 |
{ |
| 198 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_IDLE); |
| 199 |
tracker.AddWatcher(GRPC_CHANNEL_IDLE, |
| 200 |
MakeOrphanable<Watcher>(&count, &state, &status)); |
| 201 |
// No initial notification, since we started the watch from the |
| 202 |
// current state. |
| 203 |
EXPECT_EQ(count, 0); |
| 204 |
EXPECT_EQ(state, GRPC_CHANNEL_IDLE); |
| 205 |
} |
| 206 |
// Upon tracker destruction, we get a notification for SHUTDOWN. |
| 207 |
EXPECT_EQ(count, 1); |
| 208 |
EXPECT_EQ(state, GRPC_CHANNEL_SHUTDOWN); |
| 209 |
} |
| 210 |
|
| 211 |
TEST(StateTracker, DoNotNotifyShutdownAtDestructionIfAlreadyInShutdown) { |
| 212 |
int count = 0; |
| 213 |
grpc_connectivity_state state = GRPC_CHANNEL_SHUTDOWN; |
| 214 |
absl::Status status; |
| 215 |
{ |
| 216 |
ConnectivityStateTracker tracker("xxx", GRPC_CHANNEL_SHUTDOWN); |
| 217 |
tracker.AddWatcher(GRPC_CHANNEL_SHUTDOWN, |
| 218 |
MakeOrphanable<Watcher>(&count, &state, &status)); |
| 219 |
// No initial notification, since we started the watch from the |
| 220 |
// current state. |
| 221 |
EXPECT_EQ(count, 0); |
| 222 |
EXPECT_EQ(state, GRPC_CHANNEL_SHUTDOWN); |
| 223 |
} |
| 224 |
// No additional notification upon tracker destruction, since we were |
| 225 |
// already in state SHUTDOWN. |
| 226 |
EXPECT_EQ(count, 0); |
| 227 |
EXPECT_EQ(state, GRPC_CHANNEL_SHUTDOWN); |
| 228 |
} |
| 229 |
|
| 230 |
} // namespace |
| 231 |
} // namespace grpc_core |
| 232 |
|
| 233 |
int main(int argc, char** argv) { |
| 234 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 235 |
::testing::InitGoogleTest(&argc, argv); |
| 236 |
grpc_init(); |
| 237 |
grpc_core::testing::grpc_tracer_enable_flag( |
| 238 |
&grpc_core::grpc_connectivity_state_trace); |
| 239 |
int ret = RUN_ALL_TESTS(); |
| 240 |
grpc_shutdown(); |
| 241 |
return ret; |
| 242 |
} |