forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrain_manager_impl_test.cc
158 lines (131 loc) · 6.09 KB
/
drain_manager_impl_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <chrono>
#include "envoy/config/listener/v3/listener.pb.h"
#include "server/drain_manager_impl.h"
#include "test/mocks/server/instance.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using testing::_;
using testing::InSequence;
using testing::Return;
namespace Envoy {
namespace Server {
namespace {
constexpr int DrainTimeSeconds(600);
class DrainManagerImplTest : public Event::TestUsingSimulatedTime,
public testing::TestWithParam<bool> {
protected:
DrainManagerImplTest() {
ON_CALL(server_.options_, drainTime())
.WillByDefault(Return(std::chrono::seconds(DrainTimeSeconds)));
ON_CALL(server_.options_, parentShutdownTime())
.WillByDefault(Return(std::chrono::seconds(900)));
}
NiceMock<MockInstance> server_;
};
TEST_F(DrainManagerImplTest, Default) {
InSequence s;
DrainManagerImpl drain_manager(server_, envoy::config::listener::v3::Listener::DEFAULT);
// Test parent shutdown.
Event::MockTimer* shutdown_timer = new Event::MockTimer(&server_.dispatcher_);
EXPECT_CALL(*shutdown_timer, enableTimer(std::chrono::milliseconds(900000), _));
drain_manager.startParentShutdownSequence();
EXPECT_CALL(server_.hot_restart_, sendParentTerminateRequest());
shutdown_timer->invokeCallback();
// Verify basic drain close.
EXPECT_CALL(server_, healthCheckFailed()).WillOnce(Return(false));
EXPECT_FALSE(drain_manager.drainClose());
EXPECT_CALL(server_, healthCheckFailed()).WillOnce(Return(true));
EXPECT_TRUE(drain_manager.drainClose());
// Test drain sequence.
Event::MockTimer* drain_timer = new Event::MockTimer(&server_.dispatcher_);
const auto expected_delay = std::chrono::milliseconds(DrainTimeSeconds * 1000);
EXPECT_CALL(*drain_timer, enableTimer(expected_delay, nullptr));
ReadyWatcher drain_complete;
drain_manager.startDrainSequence([&drain_complete]() -> void { drain_complete.ready(); });
EXPECT_CALL(drain_complete, ready());
drain_timer->invokeCallback();
}
TEST_F(DrainManagerImplTest, ModifyOnly) {
InSequence s;
DrainManagerImpl drain_manager(server_, envoy::config::listener::v3::Listener::MODIFY_ONLY);
EXPECT_CALL(server_, healthCheckFailed()).Times(0); // Listener check will short-circuit
EXPECT_FALSE(drain_manager.drainClose());
}
TEST_P(DrainManagerImplTest, DrainDeadline) {
const bool drain_gradually = GetParam();
ON_CALL(server_.options_, drainStrategy())
.WillByDefault(Return(drain_gradually ? Server::DrainStrategy::Gradual
: Server::DrainStrategy::Immediate));
// TODO(auni53): Add integration tests for this once TestDrainManager is
// removed.
DrainManagerImpl drain_manager(server_, envoy::config::listener::v3::Listener::DEFAULT);
// Ensure drainClose() behaviour is determined by the deadline.
drain_manager.startDrainSequence([] {});
EXPECT_CALL(server_, healthCheckFailed()).WillRepeatedly(Return(false));
ON_CALL(server_.random_, random()).WillByDefault(Return(DrainTimeSeconds * 2 - 1));
ON_CALL(server_.options_, drainTime())
.WillByDefault(Return(std::chrono::seconds(DrainTimeSeconds)));
if (drain_gradually) {
// random() should be called when elapsed time < drain timeout
EXPECT_CALL(server_.random_, random()).Times(2);
EXPECT_FALSE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(DrainTimeSeconds - 1));
EXPECT_FALSE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(1));
EXPECT_TRUE(drain_manager.drainClose());
// Test that this still works if remaining time is negative
simTime().advanceTimeWait(std::chrono::seconds(1));
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(500));
EXPECT_TRUE(drain_manager.drainClose());
} else {
EXPECT_CALL(server_.random_, random()).Times(0);
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(DrainTimeSeconds - 1));
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(1));
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(1));
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(500));
EXPECT_TRUE(drain_manager.drainClose());
}
}
TEST_P(DrainManagerImplTest, DrainDeadlineProbability) {
const bool drain_gradually = GetParam();
ON_CALL(server_.options_, drainStrategy())
.WillByDefault(Return(drain_gradually ? Server::DrainStrategy::Gradual
: Server::DrainStrategy::Immediate));
ON_CALL(server_.random_, random()).WillByDefault(Return(4));
ON_CALL(server_.options_, drainTime()).WillByDefault(Return(std::chrono::seconds(3)));
DrainManagerImpl drain_manager(server_, envoy::config::listener::v3::Listener::DEFAULT);
EXPECT_CALL(server_, healthCheckFailed()).WillOnce(Return(true));
EXPECT_TRUE(drain_manager.drainClose());
EXPECT_CALL(server_, healthCheckFailed()).WillRepeatedly(Return(false));
EXPECT_FALSE(drain_manager.drainClose());
EXPECT_FALSE(drain_manager.draining());
drain_manager.startDrainSequence([] {});
EXPECT_TRUE(drain_manager.draining());
if (drain_gradually) {
// random() should be called when elapsed time < drain timeout
EXPECT_CALL(server_.random_, random()).Times(2);
// Current elapsed time is 0
// drainClose() will return true when elapsed time > (4 % 3 == 1).
EXPECT_FALSE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(2));
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(1));
EXPECT_TRUE(drain_manager.drainClose());
} else {
EXPECT_CALL(server_.random_, random()).Times(0);
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(2));
EXPECT_TRUE(drain_manager.drainClose());
simTime().advanceTimeWait(std::chrono::seconds(1));
EXPECT_TRUE(drain_manager.drainClose());
}
}
INSTANTIATE_TEST_SUITE_P(DrainStrategies, DrainManagerImplTest, testing::Bool());
} // namespace
} // namespace Server
} // namespace Envoy