forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_impl_test.cc
134 lines (114 loc) · 4.52 KB
/
worker_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
#include "common/event/dispatcher_impl.h"
#include "server/worker_impl.h"
#include "test/mocks/network/mocks.h"
#include "test/mocks/server/mocks.h"
#include "test/mocks/thread_local/mocks.h"
#include "test/test_common/utility.h"
#include "gtest/gtest.h"
using testing::InSequence;
using testing::Invoke;
using testing::InvokeWithoutArgs;
using testing::NiceMock;
using testing::Return;
using testing::Throw;
using testing::_;
namespace Envoy {
namespace Server {
class WorkerImplTest : public testing::Test {
public:
WorkerImplTest() {
// In the real worker the watchdog has timers that prevent exit. Here we need to prevent event
// loop exit since we use mock timers.
no_exit_timer_->enableTimer(std::chrono::hours(1));
}
NiceMock<ThreadLocal::MockInstance> tls_;
Event::DispatcherImpl* dispatcher_ = new Event::DispatcherImpl();
Network::MockConnectionHandler* handler_ = new Network::MockConnectionHandler();
NiceMock<MockGuardDog> guard_dog_;
DefaultTestHooks hooks_;
WorkerImpl worker_{tls_, hooks_, Event::DispatcherPtr{dispatcher_},
Network::ConnectionHandlerPtr{handler_}};
Event::TimerPtr no_exit_timer_ = dispatcher_->createTimer([]() -> void {});
};
TEST_F(WorkerImplTest, BasicFlow) {
InSequence s;
std::thread::id current_thread_id = std::this_thread::get_id();
ConditionalInitializer ci;
// Before a worker is started adding a listener will be posted and will get added when the
// thread starts running.
NiceMock<Network::MockListenerConfig> listener;
ON_CALL(listener, listenerTag()).WillByDefault(Return(1UL));
EXPECT_CALL(*handler_, addListener(_))
.WillOnce(Invoke([current_thread_id](Network::ListenerConfig& config) -> void {
EXPECT_EQ(config.listenerTag(), 1UL);
EXPECT_NE(current_thread_id, std::this_thread::get_id());
}));
worker_.addListener(listener, [&ci](bool success) -> void {
EXPECT_TRUE(success);
ci.setReady();
});
worker_.start(guard_dog_);
ci.waitReady();
// After a worker is started adding/stopping/removing a listener happens on the worker thread.
NiceMock<Network::MockListenerConfig> listener2;
ON_CALL(listener2, listenerTag()).WillByDefault(Return(2UL));
EXPECT_CALL(*handler_, addListener(_))
.WillOnce(Invoke([current_thread_id](Network::ListenerConfig& config) -> void {
EXPECT_EQ(config.listenerTag(), 2UL);
EXPECT_NE(current_thread_id, std::this_thread::get_id());
}));
worker_.addListener(listener2, [&ci](bool success) -> void {
EXPECT_TRUE(success);
ci.setReady();
});
ci.waitReady();
EXPECT_CALL(*handler_, stopListeners(2))
.WillOnce(InvokeWithoutArgs([current_thread_id, &ci]() -> void {
EXPECT_NE(current_thread_id, std::this_thread::get_id());
ci.setReady();
}));
worker_.stopListener(listener2);
ci.waitReady();
EXPECT_CALL(*handler_, removeListeners(2))
.WillOnce(InvokeWithoutArgs([current_thread_id]() -> void {
EXPECT_NE(current_thread_id, std::this_thread::get_id());
}));
worker_.removeListener(listener2, [current_thread_id, &ci]() -> void {
EXPECT_NE(current_thread_id, std::this_thread::get_id());
ci.setReady();
});
ci.waitReady();
// Now test adding and removing a listener without stopping it first.
NiceMock<Network::MockListenerConfig> listener3;
ON_CALL(listener3, listenerTag()).WillByDefault(Return(3UL));
EXPECT_CALL(*handler_, addListener(_))
.WillOnce(Invoke([current_thread_id](Network::ListenerConfig& config) -> void {
EXPECT_EQ(config.listenerTag(), 3UL);
EXPECT_NE(current_thread_id, std::this_thread::get_id());
}));
worker_.addListener(listener3, [&ci](bool success) -> void {
EXPECT_TRUE(success);
ci.setReady();
});
ci.waitReady();
EXPECT_CALL(*handler_, removeListeners(3))
.WillOnce(InvokeWithoutArgs([current_thread_id]() -> void {
EXPECT_NE(current_thread_id, std::this_thread::get_id());
}));
worker_.removeListener(listener3, [current_thread_id]() -> void {
EXPECT_NE(current_thread_id, std::this_thread::get_id());
});
worker_.stop();
}
TEST_F(WorkerImplTest, ListenerException) {
InSequence s;
NiceMock<Network::MockListenerConfig> listener;
ON_CALL(listener, listenerTag()).WillByDefault(Return(1UL));
EXPECT_CALL(*handler_, addListener(_))
.WillOnce(Throw(Network::CreateListenerException("failed")));
worker_.addListener(listener, [](bool success) -> void { EXPECT_FALSE(success); });
worker_.start(guard_dog_);
worker_.stop();
}
} // namespace Server
} // namespace Envoy