Skip to content

Commit

Permalink
Adaptive load helper mocks (#529)
Browse files Browse the repository at this point in the history
- MockNighthawkServiceClient
- MockMetricsEvaluator
- MockAdaptiveLoadSessionSpecProtoHelper

Part 8 of splitting PR #483.

Signed-off-by: eric846 <[email protected]>
  • Loading branch information
eric846 authored Sep 14, 2020
1 parent 44ba1ca commit e744a10
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/mocks/adaptive_load/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load(
"@envoy//bazel:envoy_build_system.bzl",
"envoy_cc_mock",
"envoy_package",
)

licenses(["notice"]) # Apache 2

envoy_package()

envoy_cc_mock(
name = "mock_metrics_evaluator",
srcs = ["mock_metrics_evaluator.cc"],
hdrs = ["mock_metrics_evaluator.h"],
repository = "@envoy",
deps = [
"//include/nighthawk/adaptive_load:metrics_evaluator",
],
)

envoy_cc_mock(
name = "mock_session_spec_proto_helper",
srcs = ["mock_session_spec_proto_helper.cc"],
hdrs = ["mock_session_spec_proto_helper.h"],
repository = "@envoy",
deps = [
"//include/nighthawk/adaptive_load:session_spec_proto_helper",
],
)
7 changes: 7 additions & 0 deletions test/mocks/adaptive_load/mock_metrics_evaluator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "test/mocks/adaptive_load/mock_metrics_evaluator.h"

namespace Nighthawk {

MockMetricsEvaluator::MockMetricsEvaluator() = default;

} // namespace Nighthawk
46 changes: 46 additions & 0 deletions test/mocks/adaptive_load/mock_metrics_evaluator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "nighthawk/adaptive_load/metrics_evaluator.h"

#include "gmock/gmock.h"

namespace Nighthawk {

/**
* A mock MetricsEvaluator that returns empty values from all methods.
*
* Typical usage:
*
* MockMetricsEvaluator mock_metrics_evaluator;
* BenchmarkResult benchmark_result;
* // (set benchmark_result fields here)
* EXPECT_CALL(mock_metrics_evaluator, AnalyzeNighthawkBenchmark(_, _, _))
* .WillRepeatedly(Return(benchmark_result));
*/
class MockMetricsEvaluator : public MetricsEvaluator {
public:
/**
* Empty constructor.
*/
MockMetricsEvaluator();

MOCK_CONST_METHOD3(EvaluateMetric,
absl::StatusOr<nighthawk::adaptive_load::MetricEvaluation>(
const nighthawk::adaptive_load::MetricSpec& metric_spec,
MetricsPlugin& metrics_plugin,
const nighthawk::adaptive_load::ThresholdSpec* threshold_spec));

MOCK_CONST_METHOD1(ExtractMetricSpecs,
const std::vector<std::pair<const nighthawk::adaptive_load::MetricSpec*,
const nighthawk::adaptive_load::ThresholdSpec*>>(
const nighthawk::adaptive_load::AdaptiveLoadSessionSpec& spec));

MOCK_CONST_METHOD3(
AnalyzeNighthawkBenchmark,
absl::StatusOr<nighthawk::adaptive_load::BenchmarkResult>(
const nighthawk::client::ExecutionResponse& execution_response,
const nighthawk::adaptive_load::AdaptiveLoadSessionSpec& spec,
const absl::flat_hash_map<std::string, MetricsPluginPtr>& name_to_custom_plugin_map));
};

} // namespace Nighthawk
7 changes: 7 additions & 0 deletions test/mocks/adaptive_load/mock_session_spec_proto_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "test/mocks/adaptive_load/mock_session_spec_proto_helper.h"

namespace Nighthawk {

MockAdaptiveLoadSessionSpecProtoHelper::MockAdaptiveLoadSessionSpecProtoHelper() = default;

} // namespace Nighthawk
42 changes: 42 additions & 0 deletions test/mocks/adaptive_load/mock_session_spec_proto_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include "nighthawk/adaptive_load/session_spec_proto_helper.h"

#include "gmock/gmock.h"

namespace Nighthawk {

/**
* A mock AdaptiveLoadSessionSpecProtoHelper that returns empty values or success from all methods
* by default.
*
* In particular, SetSessionSpecDefaults does not pass its input value through to its output;
* regardless of the output, it returns an empty proto, unless explicitly configured (see below). If
* you don't need to inspect calls to the spec proto helper, it may be easier to use the real
* AdaptiveLoadSessionSpecProtoHelperImpl in tests instead.
*
* Typical usage:
*
* NiceMock<MockAdaptiveLoadSessionSpecProtoHelper> mock_spec_proto_helper;
* EXPECT_CALL(mock_spec_proto_helper, CheckSessionSpec(_))
* .WillOnce(Return(absl::OkStatus()));
* AdaptiveLoadSessionSpec spec;
* // Set spec fields here, including providing all defaults yourself.
* EXPECT_CALL(mock_spec_proto_helper, SetSessionSpecDefaults(_))
* .WillOnce(Return(spec));
*/
class MockAdaptiveLoadSessionSpecProtoHelper : public AdaptiveLoadSessionSpecProtoHelper {
public:
/**
* Empty constructor.
*/
MockAdaptiveLoadSessionSpecProtoHelper();

MOCK_CONST_METHOD1(SetSessionSpecDefaults,
nighthawk::adaptive_load::AdaptiveLoadSessionSpec(
const nighthawk::adaptive_load::AdaptiveLoadSessionSpec spec));
MOCK_CONST_METHOD1(CheckSessionSpec,
absl::Status(const nighthawk::adaptive_load::AdaptiveLoadSessionSpec& spec));
};

} // namespace Nighthawk
10 changes: 10 additions & 0 deletions test/mocks/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ licenses(["notice"]) # Apache 2

envoy_package()

envoy_cc_mock(
name = "mock_nighthawk_service_client",
srcs = ["mock_nighthawk_service_client.cc"],
hdrs = ["mock_nighthawk_service_client.h"],
repository = "@envoy",
deps = [
"//include/nighthawk/common:nighthawk_service_client",
],
)

envoy_cc_mock(
name = "mock_rate_limiter",
srcs = ["mock_rate_limiter.cc"],
Expand Down
7 changes: 7 additions & 0 deletions test/mocks/common/mock_nighthawk_service_client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "test/mocks/common/mock_nighthawk_service_client.h"

namespace Nighthawk {

MockNighthawkServiceClient::MockNighthawkServiceClient() = default;

} // namespace Nighthawk
32 changes: 32 additions & 0 deletions test/mocks/common/mock_nighthawk_service_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "nighthawk/common/nighthawk_service_client.h"

#include "gmock/gmock.h"

namespace Nighthawk {

/**
* A mock NighthawkServiceClient that returns an empty response by default.
*
* Typical usage:
*
* NiceMock<MockNighthawkServiceClient> mock_nighthawk_service_client;
* nighthawk::client::ExecutionResponse nighthawk_response;
* EXPECT_CALL(mock_nighthawk_service_client, PerformNighthawkBenchmark(_, _))
* .WillRepeatedly(Return(nighthawk_response));
*/
class MockNighthawkServiceClient : public NighthawkServiceClient {
public:
/**
* Empty constructor.
*/
MockNighthawkServiceClient();

MOCK_CONST_METHOD2(PerformNighthawkBenchmark,
absl::StatusOr<nighthawk::client::ExecutionResponse>(
nighthawk::client::NighthawkService::StubInterface* stub,
const nighthawk::client::CommandLineOptions& options));
};

} // namespace Nighthawk

0 comments on commit e744a10

Please sign in to comment.