diff --git a/test/common/config/BUILD b/test/common/config/BUILD index 9b1756a2e9386..d97c5b3daf92b 100644 --- a/test/common/config/BUILD +++ b/test/common/config/BUILD @@ -52,6 +52,7 @@ envoy_cc_test( "//test/mocks/local_info:local_info_mocks", "//test/mocks/runtime:runtime_mocks", "//test/test_common:logging_lib", + "//test/test_common:simulated_time_system_lib", "//test/test_common:utility_lib", "@envoy_api//envoy/api/v2:discovery_cc", "@envoy_api//envoy/api/v2:eds_cc", diff --git a/test/common/config/grpc_mux_impl_test.cc b/test/common/config/grpc_mux_impl_test.cc index 030e7dbe26ec2..fb1abfb5c34fc 100644 --- a/test/common/config/grpc_mux_impl_test.cc +++ b/test/common/config/grpc_mux_impl_test.cc @@ -14,6 +14,7 @@ #include "test/mocks/local_info/mocks.h" #include "test/mocks/runtime/mocks.h" #include "test/test_common/logging.h" +#include "test/test_common/simulated_time_system.h" #include "test/test_common/utility.h" #include "gmock/gmock.h" @@ -36,7 +37,7 @@ namespace { class GrpcMuxImplTest : public testing::Test { public: GrpcMuxImplTest() : async_client_(new Grpc::MockAsyncClient()) { - dispatcher_.setTimeSystem(mock_time_system_); + dispatcher_.setTimeSystem(time_system_); } void setup() { @@ -76,7 +77,7 @@ class GrpcMuxImplTest : public testing::Test { Grpc::MockAsyncStream async_stream_; std::unique_ptr grpc_mux_; NiceMock callbacks_; - NiceMock mock_time_system_; + Event::SimulatedTimeSystem time_system_; NiceMock local_info_; Stats::IsolatedStoreImpl stats_; }; @@ -310,8 +311,17 @@ TEST_F(GrpcMuxImplTest, WatchDemux) { expectSendMessage(type_url, {}, "2"); } +// Exactly one test requires a mock time system to provoke behavior that cannot +// easily be achieved with a SimulatedTimeSystem. +class GrpcMuxImplTestWithMockTimeSystem : public GrpcMuxImplTest { +protected: + GrpcMuxImplTestWithMockTimeSystem() { dispatcher_.setTimeSystem(mock_time_system_); } + + MockTimeSystem mock_time_system_; +}; + // Verifies that warning messages get logged when Envoy detects too many requests. -TEST_F(GrpcMuxImplTest, TooManyRequests) { +TEST_F(GrpcMuxImplTestWithMockTimeSystem, TooManyRequests) { setup(); EXPECT_CALL(async_stream_, sendMessage(_, false)).Times(AtLeast(100)); diff --git a/test/common/http/BUILD b/test/common/http/BUILD index 93fa583a5c478..b9f6104ed30e5 100644 --- a/test/common/http/BUILD +++ b/test/common/http/BUILD @@ -121,7 +121,7 @@ envoy_cc_fuzz_test( "//test/mocks/ssl:ssl_mocks", "//test/mocks/tracing:tracing_mocks", "//test/mocks/upstream:upstream_mocks", - "//test/test_common:test_time_lib", + "//test/test_common:simulated_time_system_lib", "@envoy_api//envoy/config/filter/network/http_connection_manager/v2:http_connection_manager_cc", ], ) diff --git a/test/common/http/conn_manager_impl_fuzz_test.cc b/test/common/http/conn_manager_impl_fuzz_test.cc index 0eaf6d8e7ded0..2e6f2bcea62c0 100644 --- a/test/common/http/conn_manager_impl_fuzz_test.cc +++ b/test/common/http/conn_manager_impl_fuzz_test.cc @@ -31,7 +31,7 @@ #include "test/mocks/ssl/mocks.h" #include "test/mocks/tracing/mocks.h" #include "test/mocks/upstream/mocks.h" -#include "test/test_common/test_time.h" +#include "test/test_common/simulated_time_system.h" #include "gmock/gmock.h" @@ -56,7 +56,7 @@ class FuzzConfig : public ConnectionManagerConfig { }; FuzzConfig() - : route_config_provider_(test_time_.timeSystem()), + : route_config_provider_(time_system_), stats_{{ALL_HTTP_CONN_MAN_STATS(POOL_COUNTER(fake_stats_), POOL_GAUGE(fake_stats_), POOL_HISTOGRAM(fake_stats_))}, "", @@ -121,7 +121,7 @@ class FuzzConfig : public ConnectionManagerConfig { MockStreamEncoderFilter* encoder_filter_{}; NiceMock filter_factory_; absl::optional idle_timeout_; - DangerousDeprecatedTestTime test_time_; + Event::SimulatedTimeSystem time_system_; RouteConfigProvider route_config_provider_; std::string server_name_; Stats::IsolatedStoreImpl fake_stats_; @@ -382,7 +382,6 @@ DEFINE_PROTO_FUZZER(const test::common::http::ConnManagerImplTestCase& input) { NiceMock local_info; NiceMock cluster_manager; NiceMock filter_callbacks; - NiceMock time_system; std::unique_ptr ssl_connection; bool connection_alive = true; @@ -396,7 +395,7 @@ DEFINE_PROTO_FUZZER(const test::common::http::ConnManagerImplTestCase& input) { std::make_shared("0.0.0.0"); ConnectionManagerImpl conn_manager(config, drain_close, random, tracer, runtime, local_info, - cluster_manager, nullptr, time_system); + cluster_manager, nullptr, config.time_system_); conn_manager.initializeReadFilterCallbacks(filter_callbacks); std::vector streams; diff --git a/test/common/network/BUILD b/test/common/network/BUILD index ba4eadec52082..23b5f1548ccda 100644 --- a/test/common/network/BUILD +++ b/test/common/network/BUILD @@ -51,7 +51,7 @@ envoy_cc_test( "//test/mocks/stats:stats_mocks", "//test/test_common:environment_lib", "//test/test_common:network_utility_lib", - "//test/test_common:test_time_lib", + "//test/test_common:simulated_time_system_lib", ], ) diff --git a/test/common/network/connection_impl_test.cc b/test/common/network/connection_impl_test.cc index 65aa72f6db99d..db7a63c676246 100644 --- a/test/common/network/connection_impl_test.cc +++ b/test/common/network/connection_impl_test.cc @@ -20,7 +20,7 @@ #include "test/test_common/environment.h" #include "test/test_common/network_utility.h" #include "test/test_common/printers.h" -#include "test/test_common/test_time.h" +#include "test/test_common/simulated_time_system.h" #include "test/test_common/utility.h" #include "gmock/gmock.h" @@ -77,7 +77,7 @@ INSTANTIATE_TEST_CASE_P(IpVersions, ConnectionImplDeathTest, TestUtility::ipTestParamsToString); TEST_P(ConnectionImplDeathTest, BadFd) { - MockTimeSystem time_system; + Event::SimulatedTimeSystem time_system; Event::DispatcherImpl dispatcher(time_system); EXPECT_DEATH_LOG_TO_STDERR( ConnectionImpl(dispatcher, std::make_unique(-1, nullptr, nullptr), @@ -169,7 +169,7 @@ class ConnectionImplTest : public testing::TestWithParam { } protected: - MockTimeSystem time_system_; + Event::SimulatedTimeSystem time_system_; Event::DispatcherPtr dispatcher_; Stats::IsolatedStoreImpl stats_store_; Network::TcpListenSocket socket_{Network::Test::getAnyAddress(GetParam()), nullptr, true}; @@ -1271,7 +1271,7 @@ TEST_P(ReadBufferLimitTest, SomeLimit) { class TcpClientConnectionImplTest : public testing::TestWithParam { protected: TcpClientConnectionImplTest() : dispatcher_(time_system_) {} - MockTimeSystem time_system_; + Event::SimulatedTimeSystem time_system_; Event::DispatcherImpl dispatcher_; }; INSTANTIATE_TEST_CASE_P(IpVersions, TcpClientConnectionImplTest, @@ -1312,7 +1312,7 @@ TEST_P(TcpClientConnectionImplTest, BadConnectConnRefused) { class PipeClientConnectionImplTest : public testing::Test { protected: PipeClientConnectionImplTest() : dispatcher_(time_system_) {} - MockTimeSystem time_system_; + Event::SimulatedTimeSystem time_system_; Event::DispatcherImpl dispatcher_; const std::string path_{TestEnvironment::unixDomainSocketPath("foo")}; }; diff --git a/test/common/upstream/BUILD b/test/common/upstream/BUILD index a308e0881b0ba..789582d242f6f 100644 --- a/test/common/upstream/BUILD +++ b/test/common/upstream/BUILD @@ -158,6 +158,7 @@ envoy_cc_test( "//test/mocks/grpc:grpc_mocks", "//test/mocks/local_info:local_info_mocks", "//test/mocks/upstream:upstream_mocks", + "//test/test_common:simulated_time_system_lib", "//test/test_common:utility_lib", "@envoy_api//envoy/api/v2:eds_cc", "@envoy_api//envoy/api/v2/endpoint:endpoint_cc", diff --git a/test/common/upstream/load_stats_reporter_test.cc b/test/common/upstream/load_stats_reporter_test.cc index 1b69bfb2771a6..86bfaa79cb661 100644 --- a/test/common/upstream/load_stats_reporter_test.cc +++ b/test/common/upstream/load_stats_reporter_test.cc @@ -7,6 +7,7 @@ #include "test/mocks/grpc/mocks.h" #include "test/mocks/local_info/mocks.h" #include "test/mocks/upstream/mocks.h" +#include "test/test_common/simulated_time_system.h" #include "test/test_common/utility.h" #include "gmock/gmock.h" @@ -75,7 +76,7 @@ class LoadStatsReporterTest : public testing::Test { Event::TimerCb response_timer_cb_; Grpc::MockAsyncStream async_stream_; Grpc::MockAsyncClient* async_client_; - MockTimeSystem time_system_; + Event::SimulatedTimeSystem time_system_; NiceMock local_info_; }; @@ -93,14 +94,12 @@ TEST_F(LoadStatsReporterTest, TestPubSub) { EXPECT_CALL(*async_client_, start(_, _)).WillOnce(Return(&async_stream_)); EXPECT_CALL(async_stream_, sendMessage(_, _)); createLoadStatsReporter(); - EXPECT_CALL(time_system_, monotonicTime()); deliverLoadStatsResponse({"foo"}); EXPECT_CALL(async_stream_, sendMessage(_, _)); EXPECT_CALL(*response_timer_, enableTimer(std::chrono::milliseconds(42000))); response_timer_cb_(); - EXPECT_CALL(time_system_, monotonicTime()); deliverLoadStatsResponse({"bar"}); EXPECT_CALL(async_stream_, sendMessage(_, _)); @@ -114,8 +113,7 @@ TEST_F(LoadStatsReporterTest, ExistingClusters) { // Initially, we have no clusters to report on. expectSendMessage({}); createLoadStatsReporter(); - EXPECT_CALL(time_system_, monotonicTime()) - .WillOnce(Return(MonotonicTime(std::chrono::microseconds(3)))); + time_system_.setMonotonicTime(std::chrono::microseconds(3)); // Start reporting on foo. NiceMock foo_cluster; foo_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(2); @@ -125,8 +123,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_system_, monotonicTime()) - .WillOnce(Return(MonotonicTime(std::chrono::microseconds(4)))); + time_system_.setMonotonicTime(std::chrono::microseconds(4)); { envoy::api::v2::endpoint::ClusterStats foo_cluster_stats; foo_cluster_stats.set_cluster_name("foo"); @@ -143,15 +140,12 @@ TEST_F(LoadStatsReporterTest, ExistingClusters) { bar_cluster.info_->load_report_stats_.upstream_rq_dropped_.add(1); // Start reporting on bar. - EXPECT_CALL(time_system_, monotonicTime()) - .WillOnce(Return(MonotonicTime(std::chrono::microseconds(6)))); + time_system_.setMonotonicTime(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_system_, monotonicTime()) - .Times(2) - .WillRepeatedly(Return(MonotonicTime(std::chrono::microseconds(28)))); + time_system_.setMonotonicTime(std::chrono::microseconds(28)); { envoy::api::v2::endpoint::ClusterStats foo_cluster_stats; foo_cluster_stats.set_cluster_name("foo"); @@ -177,8 +171,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_system_, monotonicTime()) - .WillOnce(Return(MonotonicTime(std::chrono::microseconds(33)))); + time_system_.setMonotonicTime(std::chrono::microseconds(33)); { envoy::api::v2::endpoint::ClusterStats bar_cluster_stats; bar_cluster_stats.set_cluster_name("bar"); @@ -195,15 +188,12 @@ 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_system_, monotonicTime()) - .WillOnce(Return(MonotonicTime(std::chrono::microseconds(43)))); + time_system_.setMonotonicTime(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_system_, monotonicTime()) - .Times(2) - .WillRepeatedly(Return(MonotonicTime(std::chrono::microseconds(47)))); + time_system_.setMonotonicTime(std::chrono::microseconds(47)); { envoy::api::v2::endpoint::ClusterStats foo_cluster_stats; foo_cluster_stats.set_cluster_name("foo"); diff --git a/test/extensions/stats_sinks/metrics_service/BUILD b/test/extensions/stats_sinks/metrics_service/BUILD index c6b8050fa0e8e..d9759e8df43d6 100644 --- a/test/extensions/stats_sinks/metrics_service/BUILD +++ b/test/extensions/stats_sinks/metrics_service/BUILD @@ -25,6 +25,7 @@ envoy_extension_cc_test( "//test/mocks/local_info:local_info_mocks", "//test/mocks/thread_local:thread_local_mocks", "//test/mocks/upstream:upstream_mocks", + "//test/test_common:simulated_time_system_lib", ], ) diff --git a/test/extensions/stats_sinks/metrics_service/grpc_metrics_service_impl_test.cc b/test/extensions/stats_sinks/metrics_service/grpc_metrics_service_impl_test.cc index ab078fe6953f1..861f14772afb6 100644 --- a/test/extensions/stats_sinks/metrics_service/grpc_metrics_service_impl_test.cc +++ b/test/extensions/stats_sinks/metrics_service/grpc_metrics_service_impl_test.cc @@ -5,6 +5,7 @@ #include "test/mocks/local_info/mocks.h" #include "test/mocks/stats/mocks.h" #include "test/mocks/thread_local/mocks.h" +#include "test/test_common/simulated_time_system.h" using namespace std::chrono_literals; using testing::_; @@ -99,10 +100,10 @@ class MetricsServiceSinkTest : public testing::Test {}; TEST(MetricsServiceSinkTest, CheckSendCall) { NiceMock source; - NiceMock mock_time; + Event::SimulatedTimeSystem time_system; std::shared_ptr streamer_{new MockGrpcMetricsStreamer()}; - MetricsServiceSink sink(streamer_, mock_time); + MetricsServiceSink sink(streamer_, time_system); auto counter = std::make_shared>(); counter->name_ = "test_counter"; @@ -127,10 +128,10 @@ TEST(MetricsServiceSinkTest, CheckSendCall) { TEST(MetricsServiceSinkTest, CheckStatsCount) { NiceMock source; - NiceMock mock_time; + Event::SimulatedTimeSystem time_system; std::shared_ptr streamer_{new TestGrpcMetricsStreamer()}; - MetricsServiceSink sink(streamer_, mock_time); + MetricsServiceSink sink(streamer_, time_system); auto counter = std::make_shared>(); counter->name_ = "test_counter";