Skip to content

Commit

Permalink
test: Split huge monolith mock header to speed up test compilation (e…
Browse files Browse the repository at this point in the history
…nvoyproxy#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: envoyproxy#10917 

Signed-off-by: Muge Chen <mugechen@google.com>
  • Loading branch information
foreseeable authored and songhu committed Jun 25, 2020
1 parent 87b3740 commit c5b1fa1
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 60 deletions.
21 changes: 21 additions & 0 deletions test/mocks/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions test/mocks/server/admin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "admin.h"

#include <string>

#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
39 changes: 39 additions & 0 deletions test/mocks/server/admin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <string>

#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<MockConfigTracker> config_tracker_;
};
} // namespace Server

} // namespace Envoy
26 changes: 26 additions & 0 deletions test/mocks/server/config_tracker.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "config_tracker.h"

#include <string>

#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
30 changes: 30 additions & 0 deletions test/mocks/server/config_tracker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <string>

#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<std::string, Cb> config_tracker_callbacks_;
};
} // namespace Server

} // namespace Envoy
15 changes: 0 additions & 15 deletions test/mocks/server/mocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
45 changes: 2 additions & 43 deletions test/mocks/server/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -128,49 +130,6 @@ class MockOptions : public Options {
std::vector<std::string> 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<std::string, Cb> 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<MockConfigTracker> config_tracker_;
};

class MockAdminStream : public AdminStream {
public:
MockAdminStream();
Expand Down
3 changes: 2 additions & 1 deletion test/server/config_validation/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion test/server/config_validation/cluster_manager_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
Expand Down

0 comments on commit c5b1fa1

Please sign in to comment.