xref: /aosp_15_r20/external/grpc-grpc/test/core/end2end/tests/connectivity.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 
22 #include <grpc/grpc.h>
23 #include <grpc/impl/channel_arg_names.h>
24 
25 #include "src/core/lib/channel/channel_args.h"
26 #include "src/core/lib/gprpp/time.h"
27 #include "test/core/end2end/end2end_tests.h"
28 
29 namespace grpc_core {
30 namespace {
31 
CORE_END2END_TEST(RetryHttp2Test,ConnectivityWatch)32 CORE_END2END_TEST(RetryHttp2Test, ConnectivityWatch) {
33   InitClient(ChannelArgs()
34                  .Set(GRPC_ARG_INITIAL_RECONNECT_BACKOFF_MS, 1000)
35                  .Set(GRPC_ARG_MAX_RECONNECT_BACKOFF_MS, 1000)
36                  .Set(GRPC_ARG_MIN_RECONNECT_BACKOFF_MS, 5000));
37   // channels should start life in IDLE, and stay there
38   EXPECT_EQ(CheckConnectivityState(false), GRPC_CHANNEL_IDLE);
39   Step(Duration::Milliseconds(100));
40   EXPECT_EQ(CheckConnectivityState(false), GRPC_CHANNEL_IDLE);
41   // start watching for a change
42   WatchConnectivityState(GRPC_CHANNEL_IDLE, Duration::Milliseconds(500), 1);
43   Expect(1, false);
44   Step(Duration::Minutes(1));
45   // check that we're still in idle, and start connecting
46   EXPECT_EQ(CheckConnectivityState(true), GRPC_CHANNEL_IDLE);
47   // start watching for a change
48   WatchConnectivityState(GRPC_CHANNEL_IDLE, Duration::Seconds(10), 2);
49   // and now the watch should trigger
50   // (we might miss the notification for CONNECTING, so we might see
51   // TRANSIENT_FAILURE instead)
52   Expect(2, true);
53   Step();
54   grpc_connectivity_state state = CheckConnectivityState(false);
55   EXPECT_THAT(state, ::testing::AnyOf(GRPC_CHANNEL_TRANSIENT_FAILURE,
56                                       GRPC_CHANNEL_CONNECTING));
57   // quickly followed by a transition to TRANSIENT_FAILURE
58   WatchConnectivityState(GRPC_CHANNEL_CONNECTING, Duration::Seconds(10), 3);
59   Expect(3, true);
60   Step();
61   state = CheckConnectivityState(false);
62   EXPECT_EQ(state, GRPC_CHANNEL_TRANSIENT_FAILURE);
63   // now let's bring up a server to connect to
64   InitServer(ChannelArgs());
65   // when the channel gets connected, it will report READY
66   WatchConnectivityState(state, Duration::Seconds(10), 4);
67   Expect(4, true);
68   Step(Duration::Seconds(20));
69   state = CheckConnectivityState(false);
70   EXPECT_EQ(state, GRPC_CHANNEL_READY);
71   // bring down the server again
72   // we should go immediately to IDLE
73   WatchConnectivityState(GRPC_CHANNEL_READY, Duration::Seconds(10), 5);
74   ShutdownServerAndNotify(1000);
75   Expect(5, true);
76   Expect(1000, true);
77   Step();
78   state = CheckConnectivityState(false);
79   EXPECT_EQ(state, GRPC_CHANNEL_IDLE);
80 }
81 
82 }  // namespace
83 }  // namespace grpc_core
84