Skip to content

Commit

Permalink
test: Move MockedUpdatedClusterManagerImpl to cluster_manager_impl_te…
Browse files Browse the repository at this point in the history
…st (#30464)

The class is only used within cluster_manager_impl_test and is very
specific in its behavior, so I think it makes more sense to locate it in
the same file as the test that uses it, instead of keeping it in
test_cluster_manager.h.

Signed-off-by: Ali Beyad <abeyad@google.com>
  • Loading branch information
abeyad authored Oct 24, 2023
1 parent 42934fb commit 6750243
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
51 changes: 51 additions & 0 deletions test/common/upstream/cluster_manager_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,57 @@ void verifyCaresDnsConfigAndUnpack(
typed_dns_resolver_config.typed_config().UnpackTo(&cares);
}

// Helper to intercept calls to postThreadLocalClusterUpdate.
class MockLocalClusterUpdate {
public:
MOCK_METHOD(void, post,
(uint32_t priority, const HostVector& hosts_added, const HostVector& hosts_removed));
};

class MockLocalHostsRemoved {
public:
MOCK_METHOD(void, post, (const HostVector&));
};

// Override postThreadLocalClusterUpdate so we can test that merged updates calls
// it with the right values at the right times.
class MockedUpdatedClusterManagerImpl : public TestClusterManagerImpl {
public:
using TestClusterManagerImpl::TestClusterManagerImpl;

MockedUpdatedClusterManagerImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
ClusterManagerFactory& factory, Stats::Store& stats,
ThreadLocal::Instance& tls, Runtime::Loader& runtime,
const LocalInfo::LocalInfo& local_info,
AccessLog::AccessLogManager& log_manager,
Event::Dispatcher& main_thread_dispatcher, Server::Admin& admin,
ProtobufMessage::ValidationContext& validation_context,
Api::Api& api, MockLocalClusterUpdate& local_cluster_update,
MockLocalHostsRemoved& local_hosts_removed,
Http::Context& http_context, Grpc::Context& grpc_context,
Router::Context& router_context, Server::Instance& server)
: TestClusterManagerImpl(bootstrap, factory, stats, tls, runtime, local_info, log_manager,
main_thread_dispatcher, admin, validation_context, api, http_context,
grpc_context, router_context, server),
local_cluster_update_(local_cluster_update), local_hosts_removed_(local_hosts_removed) {}

protected:
void postThreadLocalClusterUpdate(ClusterManagerCluster&,
ThreadLocalClusterUpdateParams&& params) override {
for (const auto& per_priority : params.per_priority_update_params_) {
local_cluster_update_.post(per_priority.priority_, per_priority.hosts_added_,
per_priority.hosts_removed_);
}
}

void postThreadLocalRemoveHosts(const Cluster&, const HostVector& hosts_removed) override {
local_hosts_removed_.post(hosts_removed);
}

MockLocalClusterUpdate& local_cluster_update_;
MockLocalHostsRemoved& local_hosts_removed_;
};

// A gRPC MuxFactory that returns a MockGrpcMux instance when trying to instantiate a mux for the
// `envoy.config_mux.grpc_mux_factory` type. This enables testing call expectations on the ADS gRPC
// mux.
Expand Down
51 changes: 0 additions & 51 deletions test/common/upstream/test_cluster_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,6 @@ class TestClusterManagerFactory : public ClusterManagerFactory {
Server::MockOptions& options_ = server_context_.options_;
};

// Helper to intercept calls to postThreadLocalClusterUpdate.
class MockLocalClusterUpdate {
public:
MOCK_METHOD(void, post,
(uint32_t priority, const HostVector& hosts_added, const HostVector& hosts_removed));
};

class MockLocalHostsRemoved {
public:
MOCK_METHOD(void, post, (const HostVector&));
};

// A test version of ClusterManagerImpl that provides a way to get a non-const handle to the
// clusters, which is necessary in order to call updateHosts on the priority set.
class TestClusterManagerImpl : public ClusterManagerImpl {
Expand Down Expand Up @@ -231,44 +219,5 @@ class TestClusterManagerImpl : public ClusterManagerImpl {
grpc_context, router_context, server) {}
};

// Override postThreadLocalClusterUpdate so we can test that merged updates calls
// it with the right values at the right times.
class MockedUpdatedClusterManagerImpl : public TestClusterManagerImpl {
public:
using TestClusterManagerImpl::TestClusterManagerImpl;

MockedUpdatedClusterManagerImpl(const envoy::config::bootstrap::v3::Bootstrap& bootstrap,
ClusterManagerFactory& factory, Stats::Store& stats,
ThreadLocal::Instance& tls, Runtime::Loader& runtime,
const LocalInfo::LocalInfo& local_info,
AccessLog::AccessLogManager& log_manager,
Event::Dispatcher& main_thread_dispatcher, Server::Admin& admin,
ProtobufMessage::ValidationContext& validation_context,
Api::Api& api, MockLocalClusterUpdate& local_cluster_update,
MockLocalHostsRemoved& local_hosts_removed,
Http::Context& http_context, Grpc::Context& grpc_context,
Router::Context& router_context, Server::Instance& server)
: TestClusterManagerImpl(bootstrap, factory, stats, tls, runtime, local_info, log_manager,
main_thread_dispatcher, admin, validation_context, api, http_context,
grpc_context, router_context, server),
local_cluster_update_(local_cluster_update), local_hosts_removed_(local_hosts_removed) {}

protected:
void postThreadLocalClusterUpdate(ClusterManagerCluster&,
ThreadLocalClusterUpdateParams&& params) override {
for (const auto& per_priority : params.per_priority_update_params_) {
local_cluster_update_.post(per_priority.priority_, per_priority.hosts_added_,
per_priority.hosts_removed_);
}
}

void postThreadLocalRemoveHosts(const Cluster&, const HostVector& hosts_removed) override {
local_hosts_removed_.post(hosts_removed);
}

MockLocalClusterUpdate& local_cluster_update_;
MockLocalHostsRemoved& local_hosts_removed_;
};

} // namespace Upstream
} // namespace Envoy

0 comments on commit 6750243

Please sign in to comment.