diff --git a/source/common/grpc/async_client_impl.cc b/source/common/grpc/async_client_impl.cc index 852270d25110..278eca02bba2 100644 --- a/source/common/grpc/async_client_impl.cc +++ b/source/common/grpc/async_client_impl.cc @@ -11,9 +11,10 @@ namespace Envoy { namespace Grpc { AsyncClientImpl::AsyncClientImpl(Upstream::ClusterManager& cm, - const envoy::api::v2::core::GrpcService& config) + const envoy::api::v2::core::GrpcService& config, + TimeSource& time_source) : cm_(cm), remote_cluster_name_(config.envoy_grpc().cluster_name()), - initial_metadata_(config.initial_metadata()) {} + initial_metadata_(config.initial_metadata()), time_source_(time_source) {} AsyncClientImpl::~AsyncClientImpl() { while (!active_streams_.empty()) { @@ -207,7 +208,7 @@ AsyncRequestImpl::AsyncRequestImpl(AsyncClientImpl& parent, current_span_ = parent_span.spawnChild(Tracing::EgressConfig::get(), "async " + parent.remote_cluster_name_ + " egress", - parent.timeSource().systemTime()); + parent.time_source_.systemTime()); current_span_->setTag(Tracing::Tags::get().UPSTREAM_CLUSTER, parent.remote_cluster_name_); current_span_->setTag(Tracing::Tags::get().COMPONENT, Tracing::Tags::get().PROXY); } diff --git a/source/common/grpc/async_client_impl.h b/source/common/grpc/async_client_impl.h index 3841a911be11..6fa54b8dfdf7 100644 --- a/source/common/grpc/async_client_impl.h +++ b/source/common/grpc/async_client_impl.h @@ -14,7 +14,8 @@ class AsyncStreamImpl; class AsyncClientImpl final : public AsyncClient { public: - AsyncClientImpl(Upstream::ClusterManager& cm, const envoy::api::v2::core::GrpcService& config); + AsyncClientImpl(Upstream::ClusterManager& cm, const envoy::api::v2::core::GrpcService& config, + TimeSource& time_source); ~AsyncClientImpl() override; // Grpc::AsyncClient @@ -25,13 +26,12 @@ class AsyncClientImpl final : public AsyncClient { AsyncStream* start(const Protobuf::MethodDescriptor& service_method, AsyncStreamCallbacks& callbacks) override; - TimeSource& timeSource() { return cm_.timeSource(); } - private: Upstream::ClusterManager& cm_; const std::string remote_cluster_name_; const Protobuf::RepeatedPtrField initial_metadata_; std::list> active_streams_; + TimeSource& time_source_; friend class AsyncRequestImpl; friend class AsyncStreamImpl; diff --git a/source/common/grpc/async_client_manager_impl.cc b/source/common/grpc/async_client_manager_impl.cc index 78d17846755e..a769351be0cf 100644 --- a/source/common/grpc/async_client_manager_impl.cc +++ b/source/common/grpc/async_client_manager_impl.cc @@ -13,8 +13,8 @@ namespace Grpc { AsyncClientFactoryImpl::AsyncClientFactoryImpl(Upstream::ClusterManager& cm, const envoy::api::v2::core::GrpcService& config, - bool skip_cluster_check) - : cm_(cm), config_(config) { + bool skip_cluster_check, TimeSource& time_source) + : cm_(cm), config_(config), time_source_(time_source) { if (skip_cluster_check) { return; } @@ -31,8 +31,8 @@ AsyncClientFactoryImpl::AsyncClientFactoryImpl(Upstream::ClusterManager& cm, } AsyncClientManagerImpl::AsyncClientManagerImpl(Upstream::ClusterManager& cm, - ThreadLocal::Instance& tls) - : cm_(cm), tls_(tls) { + ThreadLocal::Instance& tls, TimeSource& time_source) + : cm_(cm), tls_(tls), time_source_(time_source) { #ifdef ENVOY_GOOGLE_GRPC google_tls_slot_ = tls.allocateSlot(); google_tls_slot_->set( @@ -41,7 +41,7 @@ AsyncClientManagerImpl::AsyncClientManagerImpl(Upstream::ClusterManager& cm, } AsyncClientPtr AsyncClientFactoryImpl::create() { - return std::make_unique(cm_, config_); + return std::make_unique(cm_, config_, time_source_); } GoogleAsyncClientFactoryImpl::GoogleAsyncClientFactoryImpl( @@ -50,6 +50,7 @@ GoogleAsyncClientFactoryImpl::GoogleAsyncClientFactoryImpl( : tls_(tls), google_tls_slot_(google_tls_slot), scope_(scope.createScope(fmt::format("grpc.{}.", config.google_grpc().stat_prefix()))), config_(config) { + #ifndef ENVOY_GOOGLE_GRPC UNREFERENCED_PARAMETER(tls_); UNREFERENCED_PARAMETER(google_tls_slot_); @@ -77,7 +78,7 @@ AsyncClientManagerImpl::factoryForGrpcService(const envoy::api::v2::core::GrpcSe Stats::Scope& scope, bool skip_cluster_check) { switch (config.target_specifier_case()) { case envoy::api::v2::core::GrpcService::kEnvoyGrpc: - return std::make_unique(cm_, config, skip_cluster_check); + return std::make_unique(cm_, config, skip_cluster_check, time_source_); case envoy::api::v2::core::GrpcService::kGoogleGrpc: return std::make_unique(tls_, google_tls_slot_.get(), scope, config); diff --git a/source/common/grpc/async_client_manager_impl.h b/source/common/grpc/async_client_manager_impl.h index 2c45bd6c1921..9f182669db8c 100644 --- a/source/common/grpc/async_client_manager_impl.h +++ b/source/common/grpc/async_client_manager_impl.h @@ -12,13 +12,15 @@ namespace Grpc { class AsyncClientFactoryImpl : public AsyncClientFactory { public: AsyncClientFactoryImpl(Upstream::ClusterManager& cm, - const envoy::api::v2::core::GrpcService& config, bool skip_cluster_check); + const envoy::api::v2::core::GrpcService& config, bool skip_cluster_check, + TimeSource& time_source); AsyncClientPtr create() override; private: Upstream::ClusterManager& cm_; const envoy::api::v2::core::GrpcService config_; + TimeSource& time_source_; }; class GoogleAsyncClientFactoryImpl : public AsyncClientFactory { @@ -38,7 +40,8 @@ class GoogleAsyncClientFactoryImpl : public AsyncClientFactory { class AsyncClientManagerImpl : public AsyncClientManager { public: - AsyncClientManagerImpl(Upstream::ClusterManager& cm, ThreadLocal::Instance& tls); + AsyncClientManagerImpl(Upstream::ClusterManager& cm, ThreadLocal::Instance& tls, + TimeSource& time_source); // Grpc::AsyncClientManager AsyncClientFactoryPtr factoryForGrpcService(const envoy::api::v2::core::GrpcService& config, @@ -49,6 +52,7 @@ class AsyncClientManagerImpl : public AsyncClientManager { Upstream::ClusterManager& cm_; ThreadLocal::Instance& tls_; ThreadLocal::SlotPtr google_tls_slot_; + TimeSource& time_source_; }; } // namespace Grpc diff --git a/source/common/http/async_client_impl.cc b/source/common/http/async_client_impl.cc index b4a875ac5457..8b968c242032 100644 --- a/source/common/http/async_client_impl.cc +++ b/source/common/http/async_client_impl.cc @@ -35,8 +35,9 @@ AsyncClientImpl::AsyncClientImpl(const Upstream::ClusterInfo& cluster, Stats::St Upstream::ClusterManager& cm, Runtime::Loader& runtime, Runtime::RandomGenerator& random, Router::ShadowWriterPtr&& shadow_writer) - : cluster_(cluster), config_("http.async-client.", local_info, stats_store, cm, runtime, random, - std::move(shadow_writer), true, false, false), + : cluster_(cluster), + config_("http.async-client.", local_info, stats_store, cm, runtime, random, + std::move(shadow_writer), true, false, false, dispatcher.timeSource()), dispatcher_(dispatcher) {} AsyncClientImpl::~AsyncClientImpl() { diff --git a/source/common/router/router.h b/source/common/router/router.h index 7709d6b840a0..0f7f6c3a91fa 100644 --- a/source/common/router/router.h +++ b/source/common/router/router.h @@ -96,11 +96,13 @@ class FilterConfig { FilterConfig(const std::string& stat_prefix, const LocalInfo::LocalInfo& local_info, Stats::Scope& scope, Upstream::ClusterManager& cm, Runtime::Loader& runtime, Runtime::RandomGenerator& random, ShadowWriterPtr&& shadow_writer, - bool emit_dynamic_stats, bool start_child_span, bool suppress_envoy_headers) + bool emit_dynamic_stats, bool start_child_span, bool suppress_envoy_headers, + TimeSource& time_source) : scope_(scope), local_info_(local_info), cm_(cm), runtime_(runtime), random_(random), stats_{ALL_ROUTER_STATS(POOL_COUNTER_PREFIX(scope, stat_prefix))}, emit_dynamic_stats_(emit_dynamic_stats), start_child_span_(start_child_span), - suppress_envoy_headers_(suppress_envoy_headers), shadow_writer_(std::move(shadow_writer)) {} + suppress_envoy_headers_(suppress_envoy_headers), shadow_writer_(std::move(shadow_writer)), + time_source_(time_source) {} FilterConfig(const std::string& stat_prefix, Server::Configuration::FactoryContext& context, ShadowWriterPtr&& shadow_writer, @@ -108,14 +110,15 @@ class FilterConfig { : FilterConfig(stat_prefix, context.localInfo(), context.scope(), context.clusterManager(), context.runtime(), context.random(), std::move(shadow_writer), PROTOBUF_GET_WRAPPED_OR_DEFAULT(config, dynamic_stats, true), - config.start_child_span(), config.suppress_envoy_headers()) { + config.start_child_span(), config.suppress_envoy_headers(), + context.timeSource()) { for (const auto& upstream_log : config.upstream_log()) { upstream_logs_.push_back(AccessLog::AccessLogFactory::fromProto(upstream_log, context)); } } ShadowWriter& shadowWriter() { return *shadow_writer_; } - TimeSource& timeSource() { return cm_.timeSource(); } + TimeSource& timeSource() { return time_source_; } Stats::Scope& scope_; const LocalInfo::LocalInfo& local_info_; @@ -130,6 +133,7 @@ class FilterConfig { private: ShadowWriterPtr shadow_writer_; + TimeSource& time_source_; }; typedef std::shared_ptr FilterConfigSharedPtr; diff --git a/source/common/upstream/cluster_manager_impl.cc b/source/common/upstream/cluster_manager_impl.cc index 3e71e271651a..7860cdd2c018 100644 --- a/source/common/upstream/cluster_manager_impl.cc +++ b/source/common/upstream/cluster_manager_impl.cc @@ -181,7 +181,7 @@ ClusterManagerImpl::ClusterManagerImpl(const envoy::config::bootstrap::v2::Boots config_tracker_entry_( admin.getConfigTracker().add("clusters", [this] { return dumpClusterConfigs(); })), time_source_(main_thread_dispatcher.timeSource()), dispatcher_(main_thread_dispatcher) { - async_client_manager_ = std::make_unique(*this, tls); + async_client_manager_ = std::make_unique(*this, tls, time_source_); const auto& cm_config = bootstrap.cluster_manager(); if (cm_config.has_outlier_detection()) { const std::string event_log_file_path = cm_config.outlier_detection().event_log_path(); @@ -305,7 +305,7 @@ ClusterManagerImpl::ClusterManagerImpl(const envoy::config::bootstrap::v2::Boots Config::Utility::factoryForGrpcApiConfigSource( *async_client_manager_, load_stats_config, stats) ->create(), - main_thread_dispatcher, timeSource().monotonic())); + main_thread_dispatcher)); } } diff --git a/source/common/upstream/load_stats_reporter.cc b/source/common/upstream/load_stats_reporter.cc index 5d54cf511540..1662ddd5167d 100644 --- a/source/common/upstream/load_stats_reporter.cc +++ b/source/common/upstream/load_stats_reporter.cc @@ -10,14 +10,13 @@ namespace Upstream { LoadStatsReporter::LoadStatsReporter(const LocalInfo::LocalInfo& local_info, ClusterManager& cluster_manager, Stats::Scope& scope, Grpc::AsyncClientPtr async_client, - Event::Dispatcher& dispatcher, - MonotonicTimeSource& time_source) + Event::Dispatcher& dispatcher) : cm_(cluster_manager), stats_{ALL_LOAD_REPORTER_STATS( POOL_COUNTER_PREFIX(scope, "load_reporter."))}, async_client_(std::move(async_client)), service_method_(*Protobuf::DescriptorPool::generated_pool()->FindMethodByName( "envoy.service.load_stats.v2.LoadReportingService.StreamLoadStats")), - time_source_(time_source) { + time_source_(dispatcher.timeSource()) { request_.mutable_node()->MergeFrom(local_info.node()); retry_timer_ = dispatcher.createTimer([this]() -> void { establishNewStream(); }); response_timer_ = dispatcher.createTimer([this]() -> void { sendLoadStatsRequest(); }); @@ -78,7 +77,7 @@ void LoadStatsReporter::sendLoadStatsRequest() { } cluster_stats->set_total_dropped_requests( cluster.info()->loadReportStats().upstream_rq_dropped_.latch()); - const auto now = time_source_.currentTime().time_since_epoch(); + const auto now = time_source_.monotonicTime().time_since_epoch(); const auto measured_interval = now - cluster_name_and_timestamp.second; cluster_stats->mutable_load_report_interval()->MergeFrom( Protobuf::util::TimeUtil::MicrosecondsToDuration( @@ -137,7 +136,7 @@ void LoadStatsReporter::startLoadReportPeriod() { for (const std::string& cluster_name : message_->clusters()) { clusters_.emplace(cluster_name, existing_clusters.count(cluster_name) > 0 ? existing_clusters[cluster_name] - : time_source_.currentTime().time_since_epoch()); + : time_source_.monotonicTime().time_since_epoch()); auto cluster_info_map = cm_.clusters(); auto it = cluster_info_map.find(cluster_name); if (it == cluster_info_map.end()) { diff --git a/source/common/upstream/load_stats_reporter.h b/source/common/upstream/load_stats_reporter.h index 50d4c791c444..81ffcbce0583 100644 --- a/source/common/upstream/load_stats_reporter.h +++ b/source/common/upstream/load_stats_reporter.h @@ -35,7 +35,7 @@ class LoadStatsReporter public: LoadStatsReporter(const LocalInfo::LocalInfo& local_info, ClusterManager& cluster_manager, Stats::Scope& scope, Grpc::AsyncClientPtr async_client, - Event::Dispatcher& dispatcher, MonotonicTimeSource& time_source); + Event::Dispatcher& dispatcher); // Grpc::TypedAsyncStreamCallbacks void onCreateInitialMetadata(Http::HeaderMap& metadata) override; @@ -66,7 +66,7 @@ class LoadStatsReporter std::unique_ptr message_; // Map from cluster name to start of measurement interval. std::unordered_map clusters_; - MonotonicTimeSource& time_source_; + TimeSource& time_source_; }; typedef std::unique_ptr LoadStatsReporterPtr; diff --git a/source/extensions/tracers/zipkin/config.cc b/source/extensions/tracers/zipkin/config.cc index 278aebadfbc4..1884b938cf21 100644 --- a/source/extensions/tracers/zipkin/config.cc +++ b/source/extensions/tracers/zipkin/config.cc @@ -18,9 +18,9 @@ Tracing::HttpTracerPtr ZipkinTracerFactory::createHttpTracer(const Json::Object& Envoy::Runtime::RandomGenerator& rand = server.random(); - Tracing::DriverPtr zipkin_driver(new Zipkin::Driver(json_config, server.clusterManager(), - server.stats(), server.threadLocal(), - server.runtime(), server.localInfo(), rand)); + Tracing::DriverPtr zipkin_driver( + new Zipkin::Driver(json_config, server.clusterManager(), server.stats(), server.threadLocal(), + server.runtime(), server.localInfo(), rand, server.timeSource())); return Tracing::HttpTracerPtr( new Tracing::HttpTracerImpl(std::move(zipkin_driver), server.localInfo())); diff --git a/source/extensions/tracers/zipkin/zipkin_tracer_impl.cc b/source/extensions/tracers/zipkin/zipkin_tracer_impl.cc index e122d7fface2..c3fa732be284 100644 --- a/source/extensions/tracers/zipkin/zipkin_tracer_impl.cc +++ b/source/extensions/tracers/zipkin/zipkin_tracer_impl.cc @@ -55,10 +55,12 @@ Driver::TlsTracer::TlsTracer(TracerPtr&& tracer, Driver& driver) Driver::Driver(const Json::Object& config, Upstream::ClusterManager& cluster_manager, Stats::Store& stats, ThreadLocal::SlotAllocator& tls, Runtime::Loader& runtime, - const LocalInfo::LocalInfo& local_info, Runtime::RandomGenerator& random_generator) + const LocalInfo::LocalInfo& local_info, Runtime::RandomGenerator& random_generator, + TimeSource& time_source) : cm_(cluster_manager), tracer_stats_{ZIPKIN_TRACER_STATS( POOL_COUNTER_PREFIX(stats, "tracing.zipkin."))}, - tls_(tls.allocateSlot()), runtime_(runtime), local_info_(local_info) { + tls_(tls.allocateSlot()), runtime_(runtime), local_info_(local_info), + time_source_(time_source) { Upstream::ThreadLocalCluster* cluster = cm_.get(config.getString("collector_cluster")); if (!cluster) { @@ -76,7 +78,7 @@ Driver::Driver(const Json::Object& config, Upstream::ClusterManager& cluster_man tls_->set([this, collector_endpoint, &random_generator, trace_id_128bit]( Event::Dispatcher& dispatcher) -> ThreadLocal::ThreadLocalObjectSharedPtr { TracerPtr tracer(new Tracer(local_info_.clusterName(), local_info_.address(), random_generator, - trace_id_128bit, cm_.timeSource())); + trace_id_128bit, time_source_)); tracer->setReporter( ReporterImpl::NewInstance(std::ref(*this), std::ref(dispatcher), collector_endpoint)); return ThreadLocal::ThreadLocalObjectSharedPtr{new TlsTracer(std::move(tracer), *this)}; diff --git a/source/extensions/tracers/zipkin/zipkin_tracer_impl.h b/source/extensions/tracers/zipkin/zipkin_tracer_impl.h index 7c7ecc4a4323..cb3754affc1d 100644 --- a/source/extensions/tracers/zipkin/zipkin_tracer_impl.h +++ b/source/extensions/tracers/zipkin/zipkin_tracer_impl.h @@ -91,7 +91,8 @@ class Driver : public Tracing::Driver { */ Driver(const Json::Object& config, Upstream::ClusterManager& cluster_manager, Stats::Store& stats, ThreadLocal::SlotAllocator& tls, Runtime::Loader& runtime, - const LocalInfo::LocalInfo& localinfo, Runtime::RandomGenerator& random_generator); + const LocalInfo::LocalInfo& localinfo, Runtime::RandomGenerator& random_generator, + TimeSource& time_source); /** * This function is inherited from the abstract Driver class. @@ -130,6 +131,7 @@ class Driver : public Tracing::Driver { ThreadLocal::SlotPtr tls_; Runtime::Loader& runtime_; const LocalInfo::LocalInfo& local_info_; + TimeSource& time_source_; }; /** diff --git a/source/server/server.cc b/source/server/server.cc index fae27e941694..5d11b4c67216 100644 --- a/source/server/server.cc +++ b/source/server/server.cc @@ -298,8 +298,8 @@ void InstanceImpl::initialize(Options& options, if (bootstrap_.has_hds_config()) { const auto& hds_config = bootstrap_.hds_config(); - async_client_manager_ = - std::make_unique(clusterManager(), thread_local_); + async_client_manager_ = std::make_unique( + clusterManager(), thread_local_, time_source_); hds_delegate_.reset(new Upstream::HdsDelegate( bootstrap_.node(), stats(), Config::Utility::factoryForGrpcApiConfigSource(*async_client_manager_, hds_config, stats()) diff --git a/test/common/grpc/BUILD b/test/common/grpc/BUILD index a448f215ef97..be3d33327aa5 100644 --- a/test/common/grpc/BUILD +++ b/test/common/grpc/BUILD @@ -19,6 +19,7 @@ envoy_cc_test( "//test/mocks/tracing:tracing_mocks", "//test/mocks/upstream:upstream_mocks", "//test/proto:helloworld_proto_cc", + "//test/test_common:test_time_lib", ], ) diff --git a/test/common/grpc/async_client_impl_test.cc b/test/common/grpc/async_client_impl_test.cc index c0f89abc080e..59c19c41b4e3 100644 --- a/test/common/grpc/async_client_impl_test.cc +++ b/test/common/grpc/async_client_impl_test.cc @@ -4,6 +4,7 @@ #include "test/mocks/tracing/mocks.h" #include "test/mocks/upstream/mocks.h" #include "test/proto/helloworld.pb.h" +#include "test/test_common/test_time.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -24,7 +25,7 @@ class EnvoyAsyncClientImplTest : public testing::Test { : method_descriptor_(helloworld::Greeter::descriptor()->FindMethodByName("SayHello")) { envoy::api::v2::core::GrpcService config; config.mutable_envoy_grpc()->set_cluster_name("test_cluster"); - grpc_client_ = std::make_unique(cm_, config); + grpc_client_ = std::make_unique(cm_, config, test_time_.timeSource()); ON_CALL(cm_, httpAsyncClientForCluster("test_cluster")).WillByDefault(ReturnRef(http_client_)); } @@ -32,6 +33,7 @@ class EnvoyAsyncClientImplTest : public testing::Test { NiceMock http_client_; NiceMock cm_; std::unique_ptr grpc_client_; + DangerousDeprecatedTestTime test_time_; }; // Validate that a failure in the HTTP client returns immediately with status diff --git a/test/common/grpc/async_client_manager_impl_test.cc b/test/common/grpc/async_client_manager_impl_test.cc index fc901bdfaeed..76edadb6cec8 100644 --- a/test/common/grpc/async_client_manager_impl_test.cc +++ b/test/common/grpc/async_client_manager_impl_test.cc @@ -3,6 +3,7 @@ #include "test/mocks/stats/mocks.h" #include "test/mocks/thread_local/mocks.h" #include "test/mocks/upstream/mocks.h" +#include "test/test_common/test_time.h" #include "test/test_common/utility.h" #include "gmock/gmock.h" @@ -19,10 +20,11 @@ class AsyncClientManagerImplTest : public testing::Test { Upstream::MockClusterManager cm_; NiceMock tls_; Stats::MockStore scope_; + DangerousDeprecatedTestTime test_time_; }; TEST_F(AsyncClientManagerImplTest, EnvoyGrpcOk) { - AsyncClientManagerImpl async_client_manager(cm_, tls_); + AsyncClientManagerImpl async_client_manager(cm_, tls_, test_time_.timeSource()); envoy::api::v2::core::GrpcService grpc_service; grpc_service.mutable_envoy_grpc()->set_cluster_name("foo"); @@ -37,7 +39,7 @@ TEST_F(AsyncClientManagerImplTest, EnvoyGrpcOk) { } TEST_F(AsyncClientManagerImplTest, EnvoyGrpcUnknown) { - AsyncClientManagerImpl async_client_manager(cm_, tls_); + AsyncClientManagerImpl async_client_manager(cm_, tls_, test_time_.timeSource()); envoy::api::v2::core::GrpcService grpc_service; grpc_service.mutable_envoy_grpc()->set_cluster_name("foo"); @@ -47,7 +49,7 @@ TEST_F(AsyncClientManagerImplTest, EnvoyGrpcUnknown) { } TEST_F(AsyncClientManagerImplTest, EnvoyGrpcDynamicCluster) { - AsyncClientManagerImpl async_client_manager(cm_, tls_); + AsyncClientManagerImpl async_client_manager(cm_, tls_, test_time_.timeSource()); envoy::api::v2::core::GrpcService grpc_service; grpc_service.mutable_envoy_grpc()->set_cluster_name("foo"); @@ -63,7 +65,7 @@ TEST_F(AsyncClientManagerImplTest, EnvoyGrpcDynamicCluster) { TEST_F(AsyncClientManagerImplTest, GoogleGrpc) { EXPECT_CALL(scope_, createScope_("grpc.foo.")); - AsyncClientManagerImpl async_client_manager(cm_, tls_); + AsyncClientManagerImpl async_client_manager(cm_, tls_, test_time_.timeSource()); envoy::api::v2::core::GrpcService grpc_service; grpc_service.mutable_google_grpc()->set_stat_prefix("foo"); @@ -76,7 +78,7 @@ TEST_F(AsyncClientManagerImplTest, GoogleGrpc) { } TEST_F(AsyncClientManagerImplTest, EnvoyGrpcUnknownOk) { - AsyncClientManagerImpl async_client_manager(cm_, tls_); + AsyncClientManagerImpl async_client_manager(cm_, tls_, test_time_.timeSource()); envoy::api::v2::core::GrpcService grpc_service; grpc_service.mutable_envoy_grpc()->set_cluster_name("foo"); diff --git a/test/common/grpc/grpc_client_integration_test_harness.h b/test/common/grpc/grpc_client_integration_test_harness.h index 0f0861e5f7cd..57a40975c45e 100644 --- a/test/common/grpc/grpc_client_integration_test_harness.h +++ b/test/common/grpc/grpc_client_integration_test_harness.h @@ -276,7 +276,7 @@ class GrpcClientIntegrationTest : public GrpcClientIntegrationParamTest { envoy::api::v2::core::GrpcService config; config.mutable_envoy_grpc()->set_cluster_name(fake_cluster_name_); fillServiceWideInitialMetadata(config); - return std::make_unique(cm_, config); + return std::make_unique(cm_, config, dispatcher_.timeSource()); } virtual envoy::api::v2::core::GrpcService createGoogleGrpcConfig() { diff --git a/test/common/router/BUILD b/test/common/router/BUILD index b7acdb092b4b..1c1a23d782a6 100644 --- a/test/common/router/BUILD +++ b/test/common/router/BUILD @@ -140,6 +140,7 @@ envoy_cc_test( "//test/mocks/ssl:ssl_mocks", "//test/mocks/upstream:upstream_mocks", "//test/test_common:environment_lib", + "//test/test_common:test_time_lib", "//test/test_common:utility_lib", ], ) diff --git a/test/common/router/router_test.cc b/test/common/router/router_test.cc index 467b2fa68aa8..067bd01b47ba 100644 --- a/test/common/router/router_test.cc +++ b/test/common/router/router_test.cc @@ -22,6 +22,7 @@ #include "test/mocks/upstream/mocks.h" #include "test/test_common/environment.h" #include "test/test_common/printers.h" +#include "test/test_common/test_time.h" #include "test/test_common/utility.h" #include "gmock/gmock.h" @@ -69,7 +70,8 @@ class RouterTestBase : public testing::Test { RouterTestBase(bool start_child_span, bool suppress_envoy_headers) : shadow_writer_(new MockShadowWriter()), config_("test.", local_info_, stats_store_, cm_, runtime_, random_, - ShadowWriterPtr{shadow_writer_}, true, start_child_span, suppress_envoy_headers), + ShadowWriterPtr{shadow_writer_}, true, start_child_span, suppress_envoy_headers, + test_time_.timeSource()), router_(config_) { router_.setDecoderFilterCallbacks(callbacks_); upstream_locality_.set_zone("to_az"); @@ -173,6 +175,7 @@ class RouterTestBase : public testing::Test { router_.onDestroy(); } + DangerousDeprecatedTestTime test_time_; std::string upstream_zone_{"to_az"}; envoy::api::v2::core::Locality upstream_locality_; Stats::IsolatedStoreImpl stats_store_; diff --git a/test/common/upstream/load_stats_reporter_test.cc b/test/common/upstream/load_stats_reporter_test.cc index 2a6d07e054c5..e9faf2501ae6 100644 --- a/test/common/upstream/load_stats_reporter_test.cc +++ b/test/common/upstream/load_stats_reporter_test.cc @@ -27,7 +27,10 @@ class LoadStatsReporterTest : public testing::Test { public: LoadStatsReporterTest() : retry_timer_(new Event::MockTimer()), response_timer_(new Event::MockTimer()), - async_client_(new Grpc::MockAsyncClient()) {} + async_client_(new Grpc::MockAsyncClient()), + time_source_(system_time_source_, monotonic_time_source_) { + dispatcher_.setTimeSource(time_source_); + } void createLoadStatsReporter() { InSequence s; @@ -39,9 +42,8 @@ class LoadStatsReporterTest : public testing::Test { response_timer_cb_ = timer_cb; return response_timer_; })); - load_stats_reporter_.reset(new LoadStatsReporter(local_info_, cm_, stats_store_, - Grpc::AsyncClientPtr(async_client_), - dispatcher_, time_source_)); + load_stats_reporter_.reset(new LoadStatsReporter( + local_info_, cm_, stats_store_, Grpc::AsyncClientPtr(async_client_), dispatcher_)); } void expectSendMessage( @@ -74,7 +76,9 @@ class LoadStatsReporterTest : public testing::Test { Event::TimerCb response_timer_cb_; Grpc::MockAsyncStream async_stream_; Grpc::MockAsyncClient* async_client_; - MockMonotonicTimeSource time_source_; + MockSystemTimeSource system_time_source_; + MockMonotonicTimeSource monotonic_time_source_; + TimeSource time_source_; NiceMock local_info_; }; @@ -92,14 +96,14 @@ TEST_F(LoadStatsReporterTest, TestPubSub) { EXPECT_CALL(*async_client_, start(_, _)).WillOnce(Return(&async_stream_)); EXPECT_CALL(async_stream_, sendMessage(_, _)); createLoadStatsReporter(); - EXPECT_CALL(time_source_, currentTime()); + EXPECT_CALL(monotonic_time_source_, currentTime()); deliverLoadStatsResponse({"foo"}); EXPECT_CALL(async_stream_, sendMessage(_, _)); EXPECT_CALL(*response_timer_, enableTimer(std::chrono::milliseconds(42000))); response_timer_cb_(); - EXPECT_CALL(time_source_, currentTime()); + EXPECT_CALL(monotonic_time_source_, currentTime()); deliverLoadStatsResponse({"bar"}); EXPECT_CALL(async_stream_, sendMessage(_, _)); @@ -113,7 +117,7 @@ TEST_F(LoadStatsReporterTest, ExistingClusters) { // Initially, we have no clusters to report on. expectSendMessage({}); createLoadStatsReporter(); - EXPECT_CALL(time_source_, currentTime()) + EXPECT_CALL(monotonic_time_source_, currentTime()) .WillOnce(Return(MonotonicTime(std::chrono::microseconds(3)))); // Start reporting on foo. NiceMock foo_cluster; @@ -124,7 +128,7 @@ TEST_F(LoadStatsReporterTest, ExistingClusters) { deliverLoadStatsResponse({"foo"}); // Initial stats report for foo on timer tick. foo_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(5); - EXPECT_CALL(time_source_, currentTime()) + EXPECT_CALL(monotonic_time_source_, currentTime()) .WillOnce(Return(MonotonicTime(std::chrono::microseconds(4)))); { envoy::api::v2::endpoint::ClusterStats foo_cluster_stats; @@ -142,13 +146,13 @@ TEST_F(LoadStatsReporterTest, ExistingClusters) { bar_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(1); // Start reporting on bar. - EXPECT_CALL(time_source_, currentTime()) + EXPECT_CALL(monotonic_time_source_, currentTime()) .WillOnce(Return(MonotonicTime(std::chrono::microseconds(6)))); deliverLoadStatsResponse({"foo", "bar"}); // Stats report foo/bar on timer tick. foo_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(1); bar_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(1); - EXPECT_CALL(time_source_, currentTime()) + EXPECT_CALL(monotonic_time_source_, currentTime()) .Times(2) .WillRepeatedly(Return(MonotonicTime(std::chrono::microseconds(28)))); { @@ -176,7 +180,7 @@ TEST_F(LoadStatsReporterTest, ExistingClusters) { // Stats report for bar on timer tick. foo_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(5); bar_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(5); - EXPECT_CALL(time_source_, currentTime()) + EXPECT_CALL(monotonic_time_source_, currentTime()) .WillOnce(Return(MonotonicTime(std::chrono::microseconds(33)))); { envoy::api::v2::endpoint::ClusterStats bar_cluster_stats; @@ -194,13 +198,13 @@ TEST_F(LoadStatsReporterTest, ExistingClusters) { bar_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(1); // Start tracking foo again, we should forget earlier history for foo. - EXPECT_CALL(time_source_, currentTime()) + EXPECT_CALL(monotonic_time_source_, currentTime()) .WillOnce(Return(MonotonicTime(std::chrono::microseconds(43)))); deliverLoadStatsResponse({"foo", "bar"}); // Stats report foo/bar on timer tick. foo_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(1); bar_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(1); - EXPECT_CALL(time_source_, currentTime()) + EXPECT_CALL(monotonic_time_source_, currentTime()) .Times(2) .WillRepeatedly(Return(MonotonicTime(std::chrono::microseconds(47)))); { diff --git a/test/extensions/tracers/zipkin/zipkin_tracer_impl_test.cc b/test/extensions/tracers/zipkin/zipkin_tracer_impl_test.cc index 8aa4452fc30c..4de0a78078bb 100644 --- a/test/extensions/tracers/zipkin/zipkin_tracer_impl_test.cc +++ b/test/extensions/tracers/zipkin/zipkin_tracer_impl_test.cc @@ -49,7 +49,8 @@ class ZipkinDriverTest : public Test { EXPECT_CALL(*timer_, enableTimer(std::chrono::milliseconds(5000))); } - driver_.reset(new Driver(config, cm_, stats_, tls_, runtime_, local_info_, random_)); + driver_.reset( + new Driver(config, cm_, stats_, tls_, runtime_, local_info_, random_, time_source_)); } void setupValidDriver() {