From c5b1fa1b8d8421f088166c53379640524757a63e Mon Sep 17 00:00:00 2001 From: foreseeable Date: Thu, 25 Jun 2020 00:24:58 +0000 Subject: [PATCH] test: Split huge monolith mock header to speed up test compilation (#11649) Commit Message: Split huge monolith mock header to speed up test compilation Additional Description: `cluster_manager_test` only used a simple mock class `MockAdmin` from `test/mocks/server/mocks.h`, which is a huge mock library. After splitting, the overall build time for `cluster_manager_test` reduced from 143.481s to 82.443s in my build cluster. Risk Level: low Testing: existing tests Docs Changes: N/A Release Notes: no Related Issues: #10917 Signed-off-by: Muge Chen --- test/mocks/server/BUILD | 21 +++++++++ test/mocks/server/admin.cc | 18 ++++++++ test/mocks/server/admin.h | 39 ++++++++++++++++ test/mocks/server/config_tracker.cc | 26 +++++++++++ test/mocks/server/config_tracker.h | 30 +++++++++++++ test/mocks/server/mocks.cc | 15 ------- test/mocks/server/mocks.h | 45 +------------------ test/server/config_validation/BUILD | 3 +- .../config_validation/cluster_manager_test.cc | 3 +- 9 files changed, 140 insertions(+), 60 deletions(-) create mode 100644 test/mocks/server/admin.cc create mode 100644 test/mocks/server/admin.h create mode 100644 test/mocks/server/config_tracker.cc create mode 100644 test/mocks/server/config_tracker.h diff --git a/test/mocks/server/BUILD b/test/mocks/server/BUILD index c6fcf62562ed..a907ca21feb7 100644 --- a/test/mocks/server/BUILD +++ b/test/mocks/server/BUILD @@ -8,6 +8,25 @@ licenses(["notice"]) # Apache 2 envoy_package() +envoy_cc_mock( + name = "config_tracker_mocks", + srcs = ["config_tracker.cc"], + hdrs = ["config_tracker.h"], + deps = [ + "//include/envoy/server:configuration_interface", + ], +) + +envoy_cc_mock( + name = "admin_mocks", + srcs = ["admin.cc"], + hdrs = ["admin.h"], + deps = [ + "//include/envoy/server:admin_interface", + "//test/mocks/server:config_tracker_mocks", + ], +) + envoy_cc_mock( name = "server_mocks", srcs = ["mocks.cc"], @@ -44,6 +63,8 @@ envoy_cc_mock( "//test/mocks/router:router_mocks", "//test/mocks/runtime:runtime_mocks", "//test/mocks/secret:secret_mocks", + "//test/mocks/server:admin_mocks", + "//test/mocks/server:config_tracker_mocks", "//test/mocks/thread_local:thread_local_mocks", "//test/mocks/tracing:tracing_mocks", "//test/mocks/upstream:upstream_mocks", diff --git a/test/mocks/server/admin.cc b/test/mocks/server/admin.cc new file mode 100644 index 000000000000..2411ef375f00 --- /dev/null +++ b/test/mocks/server/admin.cc @@ -0,0 +1,18 @@ +#include "admin.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { +MockAdmin::MockAdmin() { + ON_CALL(*this, getConfigTracker()).WillByDefault(testing::ReturnRef(config_tracker_)); +} + +MockAdmin::~MockAdmin() = default; + +} // namespace Server + +} // namespace Envoy diff --git a/test/mocks/server/admin.h b/test/mocks/server/admin.h new file mode 100644 index 000000000000..2a82d23859c6 --- /dev/null +++ b/test/mocks/server/admin.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +#include "envoy/server/admin.h" + +#include "absl/strings/string_view.h" +#include "config_tracker.h" +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockAdmin : public Admin { +public: + MockAdmin(); + ~MockAdmin() override; + + // Server::Admin + MOCK_METHOD(bool, addHandler, + (const std::string& prefix, const std::string& help_text, HandlerCb callback, + bool removable, bool mutates_server_state)); + MOCK_METHOD(bool, removeHandler, (const std::string& prefix)); + MOCK_METHOD(Network::Socket&, socket, ()); + MOCK_METHOD(ConfigTracker&, getConfigTracker, ()); + MOCK_METHOD(void, startHttpListener, + (const std::string& access_log_path, const std::string& address_out_path, + Network::Address::InstanceConstSharedPtr address, + const Network::Socket::OptionsSharedPtr& socket_options, + Stats::ScopePtr&& listener_scope)); + MOCK_METHOD(Http::Code, request, + (absl::string_view path_and_query, absl::string_view method, + Http::ResponseHeaderMap& response_headers, std::string& body)); + MOCK_METHOD(void, addListenerToHandler, (Network::ConnectionHandler * handler)); + + ::testing::NiceMock config_tracker_; +}; +} // namespace Server + +} // namespace Envoy diff --git a/test/mocks/server/config_tracker.cc b/test/mocks/server/config_tracker.cc new file mode 100644 index 000000000000..bf53f7501e3a --- /dev/null +++ b/test/mocks/server/config_tracker.cc @@ -0,0 +1,26 @@ +#include "config_tracker.h" + +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +namespace Envoy { +namespace Server { + +using ::testing::_; +using ::testing::Invoke; + +MockConfigTracker::MockConfigTracker() { + ON_CALL(*this, add_(_, _)) + .WillByDefault(Invoke([this](const std::string& key, Cb callback) -> EntryOwner* { + EXPECT_TRUE(config_tracker_callbacks_.find(key) == config_tracker_callbacks_.end()); + config_tracker_callbacks_[key] = callback; + return new MockEntryOwner(); + })); +} + +MockConfigTracker::~MockConfigTracker() = default; + +} // namespace Server +} // namespace Envoy diff --git a/test/mocks/server/config_tracker.h b/test/mocks/server/config_tracker.h new file mode 100644 index 000000000000..a84b87c7feb0 --- /dev/null +++ b/test/mocks/server/config_tracker.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include "envoy/server/config_tracker.h" + +#include "gmock/gmock.h" + +namespace Envoy { +namespace Server { +class MockConfigTracker : public ConfigTracker { +public: + MockConfigTracker(); + ~MockConfigTracker() override; + + struct MockEntryOwner : public EntryOwner {}; + + MOCK_METHOD(EntryOwner*, add_, (std::string, Cb)); + + // Server::ConfigTracker + MOCK_METHOD(const CbsMap&, getCallbacksMap, (), (const)); + EntryOwnerPtr add(const std::string& key, Cb callback) override { + return EntryOwnerPtr{add_(key, std::move(callback))}; + } + + std::unordered_map config_tracker_callbacks_; +}; +} // namespace Server + +} // namespace Envoy diff --git a/test/mocks/server/mocks.cc b/test/mocks/server/mocks.cc index e5b7fb43c63b..e9fc293558c5 100644 --- a/test/mocks/server/mocks.cc +++ b/test/mocks/server/mocks.cc @@ -53,21 +53,6 @@ MockOptions::MockOptions(const std::string& config_path) : config_path_(config_p } MockOptions::~MockOptions() = default; -MockConfigTracker::MockConfigTracker() { - ON_CALL(*this, add_(_, _)) - .WillByDefault(Invoke([this](const std::string& key, Cb callback) -> EntryOwner* { - EXPECT_TRUE(config_tracker_callbacks_.find(key) == config_tracker_callbacks_.end()); - config_tracker_callbacks_[key] = callback; - return new MockEntryOwner(); - })); -} -MockConfigTracker::~MockConfigTracker() = default; - -MockAdmin::MockAdmin() { - ON_CALL(*this, getConfigTracker()).WillByDefault(testing::ReturnRef(config_tracker_)); -} -MockAdmin::~MockAdmin() = default; - MockAdminStream::MockAdminStream() = default; MockAdminStream::~MockAdminStream() = default; diff --git a/test/mocks/server/mocks.h b/test/mocks/server/mocks.h index 6e5060014f72..1685d0f706d2 100644 --- a/test/mocks/server/mocks.h +++ b/test/mocks/server/mocks.h @@ -53,6 +53,8 @@ #include "test/test_common/test_time_system.h" #include "absl/strings/string_view.h" +#include "admin.h" +#include "config_tracker.h" #include "gmock/gmock.h" #include "spdlog/spdlog.h" @@ -128,49 +130,6 @@ class MockOptions : public Options { std::vector disabled_extensions_; }; -class MockConfigTracker : public ConfigTracker { -public: - MockConfigTracker(); - ~MockConfigTracker() override; - - struct MockEntryOwner : public EntryOwner {}; - - MOCK_METHOD(EntryOwner*, add_, (std::string, Cb)); - - // Server::ConfigTracker - MOCK_METHOD(const CbsMap&, getCallbacksMap, (), (const)); - EntryOwnerPtr add(const std::string& key, Cb callback) override { - return EntryOwnerPtr{add_(key, std::move(callback))}; - } - - std::unordered_map config_tracker_callbacks_; -}; - -class MockAdmin : public Admin { -public: - MockAdmin(); - ~MockAdmin() override; - - // Server::Admin - MOCK_METHOD(bool, addHandler, - (const std::string& prefix, const std::string& help_text, HandlerCb callback, - bool removable, bool mutates_server_state)); - MOCK_METHOD(bool, removeHandler, (const std::string& prefix)); - MOCK_METHOD(Network::Socket&, socket, ()); - MOCK_METHOD(ConfigTracker&, getConfigTracker, ()); - MOCK_METHOD(void, startHttpListener, - (const std::string& access_log_path, const std::string& address_out_path, - Network::Address::InstanceConstSharedPtr address, - const Network::Socket::OptionsSharedPtr& socket_options, - Stats::ScopePtr&& listener_scope)); - MOCK_METHOD(Http::Code, request, - (absl::string_view path_and_query, absl::string_view method, - Http::ResponseHeaderMap& response_headers, std::string& body)); - MOCK_METHOD(void, addListenerToHandler, (Network::ConnectionHandler * handler)); - - NiceMock config_tracker_; -}; - class MockAdminStream : public AdminStream { public: MockAdminStream(); diff --git a/test/server/config_validation/BUILD b/test/server/config_validation/BUILD index 19942cb6d89e..086575831d1f 100644 --- a/test/server/config_validation/BUILD +++ b/test/server/config_validation/BUILD @@ -27,6 +27,7 @@ envoy_cc_test( "//include/envoy/upstream:resource_manager_interface", "//include/envoy/upstream:upstream_interface", "//source/common/api:api_lib", + "//source/common/singleton:manager_impl_lib", "//source/common/stats:stats_lib", "//source/extensions/transport_sockets/tls:context_lib", "//source/server/config_validation:cluster_manager_lib", @@ -39,7 +40,7 @@ envoy_cc_test( "//test/mocks/protobuf:protobuf_mocks", "//test/mocks/runtime:runtime_mocks", "//test/mocks/secret:secret_mocks", - "//test/mocks/server:server_mocks", + "//test/mocks/server:admin_mocks", "//test/mocks/thread_local:thread_local_mocks", "//test/mocks/upstream:upstream_mocks", "//test/test_common:simulated_time_system_lib", diff --git a/test/server/config_validation/cluster_manager_test.cc b/test/server/config_validation/cluster_manager_test.cc index cd6cfdbb9b56..a1475f197281 100644 --- a/test/server/config_validation/cluster_manager_test.cc +++ b/test/server/config_validation/cluster_manager_test.cc @@ -3,6 +3,7 @@ #include "envoy/upstream/upstream.h" #include "common/api/api_impl.h" +#include "common/grpc/context_impl.h" #include "common/http/context_impl.h" #include "common/singleton/manager_impl.h" @@ -18,7 +19,7 @@ #include "test/mocks/protobuf/mocks.h" #include "test/mocks/runtime/mocks.h" #include "test/mocks/secret/mocks.h" -#include "test/mocks/server/mocks.h" +#include "test/mocks/server/admin.h" #include "test/mocks/thread_local/mocks.h" #include "test/mocks/upstream/mocks.h" #include "test/test_common/simulated_time_system.h"