-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathclient_worker_test.cc
More file actions
153 lines (124 loc) · 5.7 KB
/
client_worker_test.cc
File metadata and controls
153 lines (124 loc) · 5.7 KB
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
#include <functional>
#include <thread>
#include "envoy/common/exception.h"
#include "envoy/upstream/cluster_manager.h"
#include "nighthawk/user_defined_output/user_defined_output_plugin.h"
#include "external/envoy/source/common/common/random_generator.h"
#include "external/envoy/source/common/runtime/runtime_impl.h"
#include "external/envoy/source/common/stats/isolated_store_impl.h"
#include "external/envoy/test/mocks/local_info/mocks.h"
#include "external/envoy/test/mocks/protobuf/mocks.h"
#include "external/envoy/test/mocks/thread_local/mocks.h"
#include "external/envoy/test/test_common/simulated_time_system.h"
#include "source/client/client_worker_impl.h"
#include "source/common/statistic_impl.h"
#include "test/mocks/client/mock_benchmark_client.h"
#include "test/mocks/client/mock_benchmark_client_factory.h"
#include "test/mocks/common/mock_request_source.h"
#include "test/mocks/common/mock_request_source_factory.h"
#include "test/mocks/common/mock_sequencer.h"
#include "test/mocks/common/mock_sequencer_factory.h"
#include "test/mocks/common/mock_termination_predicate.h"
#include "test/mocks/common/mock_termination_predicate_factory.h"
#include "gtest/gtest.h"
using namespace testing;
using namespace std::chrono_literals;
namespace Nighthawk {
namespace Client {
namespace {
using EnvoyException = Envoy::EnvoyException;
} // namespace
class ClientWorkerTest : public Test {
public:
ClientWorkerTest()
: api_(Envoy::Api::createApiForTest()), thread_id_(std::this_thread::get_id()) {
absl::StatusOr<Envoy::Runtime::LoaderPtr> loader = Envoy::Runtime::LoaderImpl::create(
dispatcher_, tls_, {}, local_info_, store_, rand_, validation_visitor_, *api_);
THROW_IF_NOT_OK(loader.status());
loader_ = *std::move(loader);
benchmark_client_ = new MockBenchmarkClient();
sequencer_ = new MockSequencer();
request_generator_ = new MockRequestSource();
EXPECT_CALL(benchmark_client_factory_, create(_, _, _, _, _, _, _, _, _))
.Times(1)
.WillOnce(Return(ByMove(std::unique_ptr<BenchmarkClient>(benchmark_client_))));
EXPECT_CALL(sequencer_factory_, create(_, _, _, _, _, _))
.Times(1)
.WillOnce(Return(ByMove(std::unique_ptr<Sequencer>(sequencer_))));
EXPECT_CALL(request_generator_factory_, create(_, _, _, _))
.Times(1)
.WillOnce(Return(ByMove(std::unique_ptr<RequestSource>(request_generator_))));
EXPECT_CALL(*request_generator_, initOnThread());
EXPECT_CALL(*request_generator_, destroyOnThread());
EXPECT_CALL(termination_predicate_factory_, create(_, _, _))
.WillOnce(Return(ByMove(createMockTerminationPredicate())));
}
StatisticPtrMap createStatisticPtrMap() const {
StatisticPtrMap map;
map["foo1"] = &statistic_;
map["foo2"] = &statistic_;
return map;
}
bool CheckThreadChanged(const CompletionCallback&) {
EXPECT_NE(thread_id_, std::this_thread::get_id());
return false;
}
TerminationPredicatePtr createMockTerminationPredicate() {
auto predicate = std::make_unique<NiceMock<MockTerminationPredicate>>();
ON_CALL(*predicate, appendToChain(_)).WillByDefault(ReturnRef(*predicate));
return predicate;
}
StreamingStatistic statistic_;
Envoy::Api::ApiPtr api_;
std::thread::id thread_id_;
MockBenchmarkClientFactory benchmark_client_factory_;
MockTerminationPredicateFactory termination_predicate_factory_;
MockSequencerFactory sequencer_factory_;
MockRequestSourceFactory request_generator_factory_;
Envoy::Stats::IsolatedStoreImpl store_;
NiceMock<Envoy::ThreadLocal::MockInstance> tls_;
Envoy::Event::SimulatedTimeSystem time_system_;
MockBenchmarkClient* benchmark_client_;
MockSequencer* sequencer_;
MockRequestSource* request_generator_;
Envoy::Random::RandomGeneratorImpl rand_;
NiceMock<Envoy::Event::MockDispatcher> dispatcher_;
Envoy::Runtime::LoaderPtr loader_;
NiceMock<Envoy::LocalInfo::MockLocalInfo> local_info_;
NiceMock<Envoy::ProtobufMessage::MockValidationVisitor> validation_visitor_;
Envoy::Upstream::ClusterManagerPtr cluster_manager_ptr_;
Envoy::Tracing::TracerSharedPtr tracer_;
};
TEST_F(ClientWorkerTest, BasicTest) {
ASSERT_EQ(std::this_thread::get_id(), thread_id_);
{
InSequence dummy;
EXPECT_CALL(*benchmark_client_, setShouldMeasureLatencies(false));
EXPECT_CALL(*benchmark_client_, tryStartRequest(_))
.WillOnce(Invoke(this, &ClientWorkerTest::CheckThreadChanged));
EXPECT_CALL(*benchmark_client_, setShouldMeasureLatencies(true));
EXPECT_CALL(*sequencer_, start);
EXPECT_CALL(*sequencer_, waitForCompletion);
EXPECT_CALL(*benchmark_client_, terminate());
}
int worker_number = 12345;
std::vector<UserDefinedOutputNamePluginPair> user_defined_output_plugins;
auto worker = std::make_unique<ClientWorkerImpl>(
*api_, tls_, cluster_manager_ptr_, benchmark_client_factory_, termination_predicate_factory_,
sequencer_factory_, request_generator_factory_, store_, worker_number,
time_system_.monotonicTime(), tracer_, ClientWorkerImpl::HardCodedWarmupStyle::ON,
std::move(user_defined_output_plugins));
worker->start();
worker->waitForCompletion();
EXPECT_CALL(*benchmark_client_, statistics()).WillOnce(Return(createStatisticPtrMap()));
EXPECT_CALL(*sequencer_, statistics()).WillOnce(Return(createStatisticPtrMap()));
auto statistics = worker->statistics();
EXPECT_EQ(2, statistics.size());
std::vector<nighthawk::client::UserDefinedOutput> mocked_vector{};
EXPECT_CALL(*benchmark_client_, getUserDefinedOutputResults()).WillOnce(Return(mocked_vector));
std::vector<nighthawk::client::UserDefinedOutput> outputs = worker->getUserDefinedOutputResults();
EXPECT_TRUE(outputs.empty());
worker->shutdown();
}
} // namespace Client
} // namespace Nighthawk