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