diff --git a/include/envoy/api/api.h b/include/envoy/api/api.h index 20bd9d87748d2..526b812d28b02 100644 --- a/include/envoy/api/api.h +++ b/include/envoy/api/api.h @@ -35,8 +35,7 @@ class Api { */ virtual Filesystem::FileSharedPtr createFile(const std::string& path, Event::Dispatcher& dispatcher, - Thread::BasicLockable& lock, - Stats::Store& stats_store) PURE; + Thread::BasicLockable& lock) PURE; /** * @return bool whether a file exists and can be opened for read on disk. diff --git a/source/common/access_log/access_log_manager_impl.cc b/source/common/access_log/access_log_manager_impl.cc index 56933387b348b..8993bda70a908 100644 --- a/source/common/access_log/access_log_manager_impl.cc +++ b/source/common/access_log/access_log_manager_impl.cc @@ -16,7 +16,7 @@ Filesystem::FileSharedPtr AccessLogManagerImpl::createAccessLog(const std::strin return access_logs_[file_name]; } - access_logs_[file_name] = api_.createFile(file_name, dispatcher_, lock_, stats_store_); + access_logs_[file_name] = api_.createFile(file_name, dispatcher_, lock_); return access_logs_[file_name]; } diff --git a/source/common/access_log/access_log_manager_impl.h b/source/common/access_log/access_log_manager_impl.h index 3c6c0208a2cdf..502ca50c74e86 100644 --- a/source/common/access_log/access_log_manager_impl.h +++ b/source/common/access_log/access_log_manager_impl.h @@ -11,9 +11,8 @@ namespace AccessLog { class AccessLogManagerImpl : public AccessLogManager { public: - AccessLogManagerImpl(Api::Api& api, Event::Dispatcher& dispatcher, Thread::BasicLockable& lock, - Stats::Store& stats_store) - : api_(api), dispatcher_(dispatcher), lock_(lock), stats_store_(stats_store) {} + AccessLogManagerImpl(Api::Api& api, Event::Dispatcher& dispatcher, Thread::BasicLockable& lock) + : api_(api), dispatcher_(dispatcher), lock_(lock) {} // AccessLog::AccessLogManager void reopen() override; @@ -23,7 +22,6 @@ class AccessLogManagerImpl : public AccessLogManager { Api::Api& api_; Event::Dispatcher& dispatcher_; Thread::BasicLockable& lock_; - Stats::Store& stats_store_; std::unordered_map access_logs_; }; diff --git a/source/common/api/api_impl.cc b/source/common/api/api_impl.cc index 2c7708a10fccd..0fec894a38276 100644 --- a/source/common/api/api_impl.cc +++ b/source/common/api/api_impl.cc @@ -11,17 +11,17 @@ namespace Envoy { namespace Api { Impl::Impl(std::chrono::milliseconds file_flush_interval_msec, - Thread::ThreadFactory& thread_factory) - : file_flush_interval_msec_(file_flush_interval_msec), thread_factory_(thread_factory) {} + Thread::ThreadFactory& thread_factory, Stats::Store& stats_store) + : thread_factory_(thread_factory), + file_system_(file_flush_interval_msec, thread_factory, stats_store) {} Event::DispatcherPtr Impl::allocateDispatcher(Event::TimeSystem& time_system) { return Event::DispatcherPtr{new Event::DispatcherImpl(time_system)}; } Filesystem::FileSharedPtr Impl::createFile(const std::string& path, Event::Dispatcher& dispatcher, - Thread::BasicLockable& lock, Stats::Store& stats_store) { - return std::make_shared(path, dispatcher, lock, stats_store, *this, - file_flush_interval_msec_); + Thread::BasicLockable& lock) { + return file_system_.createFile(path, dispatcher, lock); } bool Impl::fileExists(const std::string& path) { return Filesystem::fileExists(path); } diff --git a/source/common/api/api_impl.h b/source/common/api/api_impl.h index d2343d8e69007..69cdaf79fef45 100644 --- a/source/common/api/api_impl.h +++ b/source/common/api/api_impl.h @@ -8,6 +8,8 @@ #include "envoy/filesystem/filesystem.h" #include "envoy/thread/thread.h" +#include "common/filesystem/filesystem_impl.h" + namespace Envoy { namespace Api { @@ -16,20 +18,21 @@ namespace Api { */ class Impl : public Api::Api { public: - Impl(std::chrono::milliseconds file_flush_interval_msec, Thread::ThreadFactory& thread_factory); + Impl(std::chrono::milliseconds file_flush_interval_msec, Thread::ThreadFactory& thread_factory, + Stats::Store& stats_store); // Api::Api Event::DispatcherPtr allocateDispatcher(Event::TimeSystem& time_system) override; Filesystem::FileSharedPtr createFile(const std::string& path, Event::Dispatcher& dispatcher, - Thread::BasicLockable& lock, - Stats::Store& stats_store) override; + Thread::BasicLockable& lock) override; bool fileExists(const std::string& path) override; std::string fileReadToEnd(const std::string& path) override; Thread::ThreadPtr createThread(std::function thread_routine) override; + Filesystem::Instance& fileSystem() { return file_system_; } private: - std::chrono::milliseconds file_flush_interval_msec_; Thread::ThreadFactory& thread_factory_; + Filesystem::Instance file_system_; }; } // namespace Api diff --git a/source/common/filesystem/filesystem_impl.cc b/source/common/filesystem/filesystem_impl.cc index a86a3901f2ba5..8717d24b03f23 100644 --- a/source/common/filesystem/filesystem_impl.cc +++ b/source/common/filesystem/filesystem_impl.cc @@ -94,18 +94,31 @@ bool illegalPath(const std::string& path) { } } +Instance::Instance(std::chrono::milliseconds file_flush_interval_msec, + Thread::ThreadFactory& thread_factory, Stats::Store& stats_store) + : file_flush_interval_msec_(file_flush_interval_msec), + file_stats_{FILESYSTEM_STATS(POOL_COUNTER_PREFIX(stats_store, "filesystem."), + POOL_GAUGE_PREFIX(stats_store, "filesystem."))}, + thread_factory_(thread_factory) {} + +FileSharedPtr Instance::createFile(const std::string& path, Event::Dispatcher& dispatcher, + Thread::BasicLockable& lock, + std::chrono::milliseconds file_flush_interval_msec) { + return std::make_shared(path, dispatcher, lock, file_stats_, + file_flush_interval_msec, thread_factory_); +}; + FileImpl::FileImpl(const std::string& path, Event::Dispatcher& dispatcher, - Thread::BasicLockable& lock, Stats::Store& stats_store, Api::Api& api, - std::chrono::milliseconds flush_interval_msec) + Thread::BasicLockable& lock, FileSystemStats& stats, + std::chrono::milliseconds flush_interval_msec, + Thread::ThreadFactory& thread_factory) : path_(path), file_lock_(lock), flush_timer_(dispatcher.createTimer([this]() -> void { stats_.flushed_by_timer_.inc(); flush_event_.notifyOne(); flush_timer_->enableTimer(flush_interval_msec_); })), - os_sys_calls_(Api::OsSysCallsSingleton::get()), api_(api), - flush_interval_msec_(flush_interval_msec), - stats_{FILESYSTEM_STATS(POOL_COUNTER_PREFIX(stats_store, "filesystem."), - POOL_GAUGE_PREFIX(stats_store, "filesystem."))} { + os_sys_calls_(Api::OsSysCallsSingleton::get()), thread_factory_(thread_factory), + flush_interval_msec_(flush_interval_msec), stats_(stats) { open(); } @@ -250,7 +263,7 @@ void FileImpl::write(absl::string_view data) { } void FileImpl::createFlushStructures() { - flush_thread_ = api_.createThread([this]() -> void { flushThreadFunc(); }); + flush_thread_ = thread_factory_.createThread([this]() -> void { flushThreadFunc(); }); flush_timer_->enableTimer(flush_interval_msec_); } diff --git a/source/common/filesystem/filesystem_impl.h b/source/common/filesystem/filesystem_impl.h index c1f22683177fb..c14c8a560bf50 100644 --- a/source/common/filesystem/filesystem_impl.h +++ b/source/common/filesystem/filesystem_impl.h @@ -32,6 +32,44 @@ struct FileSystemStats { namespace Filesystem { +/** + * Captures state, properties, and stats of a file-system. + */ +class Instance { +public: + Instance(std::chrono::milliseconds file_flush_interval_msec, + Thread::ThreadFactory& thread_factory, Stats::Store& store); + + /** + * Creates a file, overriding the flush-interval set in the class. + * + * @param path The path of the file to open. + * @param dispatcher The dispatcher used for set up timers to run flush(). + * @param lock The lock. + * @param file_flush_interval_msec Number of milliseconds to delay before flushing. + */ + FileSharedPtr createFile(const std::string& path, Event::Dispatcher& dispatcher, + Thread::BasicLockable& lock, + std::chrono::milliseconds file_flush_interval_msec); + + /** + * Creates a file, using the default flush-interval for the class. + * + * @param path The path of the file to open. + * @param dispatcher The dispatcher used for set up timers to run flush(). + * @param lock The lock. + */ + FileSharedPtr createFile(const std::string& path, Event::Dispatcher& dispatcher, + Thread::BasicLockable& lock) { + return createFile(path, dispatcher, lock, file_flush_interval_msec_); + } + +private: + const std::chrono::milliseconds file_flush_interval_msec_; + FileSystemStats file_stats_; + Thread::ThreadFactory& thread_factory_; +}; + /** * @return bool whether a file exists on disk and can be opened for read. */ @@ -83,7 +121,8 @@ bool illegalPath(const std::string& path); class FileImpl : public File { public: FileImpl(const std::string& path, Event::Dispatcher& dispatcher, Thread::BasicLockable& lock, - Stats::Store& stats_store, Api::Api& api, std::chrono::milliseconds flush_interval_msec); + FileSystemStats& stats_, std::chrono::milliseconds flush_interval_msec, + Thread::ThreadFactory& thread_factory); ~FileImpl(); // Filesystem::File @@ -147,11 +186,11 @@ class FileImpl : public File { // final write to disk. Event::TimerPtr flush_timer_; Api::OsSysCalls& os_sys_calls_; - Api::Api& api_; + Thread::ThreadFactory& thread_factory_; const std::chrono::milliseconds flush_interval_msec_; // Time interval buffer gets flushed no // matter if it reached the MIN_FLUSH_SIZE // or not. - FileSystemStats stats_; + FileSystemStats& stats_; }; } // namespace Filesystem diff --git a/source/server/config_validation/api.cc b/source/server/config_validation/api.cc index c04ab0e77cec4..89973e735fd09 100644 --- a/source/server/config_validation/api.cc +++ b/source/server/config_validation/api.cc @@ -6,8 +6,8 @@ namespace Envoy { namespace Api { ValidationImpl::ValidationImpl(std::chrono::milliseconds file_flush_interval_msec, - Thread::ThreadFactory& thread_factory) - : Impl(file_flush_interval_msec, thread_factory) {} + Thread::ThreadFactory& thread_factory, Stats::Store& stats_store) + : Impl(file_flush_interval_msec, thread_factory, stats_store) {} Event::DispatcherPtr ValidationImpl::allocateDispatcher(Event::TimeSystem& time_system) { return Event::DispatcherPtr{new Event::ValidationDispatcher(time_system)}; diff --git a/source/server/config_validation/api.h b/source/server/config_validation/api.h index 62378934d7732..b00cd2c81e1c5 100644 --- a/source/server/config_validation/api.h +++ b/source/server/config_validation/api.h @@ -16,7 +16,7 @@ namespace Api { class ValidationImpl : public Impl { public: ValidationImpl(std::chrono::milliseconds file_flush_interval_msec, - Thread::ThreadFactory& thread_factory); + Thread::ThreadFactory& thread_factory, Stats::Store& stats_store); Event::DispatcherPtr allocateDispatcher(Event::TimeSystem&) override; }; diff --git a/source/server/config_validation/server.cc b/source/server/config_validation/server.cc index a6a71c5d577df..f35fd1a96deb1 100644 --- a/source/server/config_validation/server.cc +++ b/source/server/config_validation/server.cc @@ -43,10 +43,10 @@ ValidationInstance::ValidationInstance(Options& options, Event::TimeSystem& time ComponentFactory& component_factory, Thread::ThreadFactory& thread_factory) : options_(options), time_system_(time_system), stats_store_(store), - api_(new Api::ValidationImpl(options.fileFlushIntervalMsec(), thread_factory)), + api_(new Api::ValidationImpl(options.fileFlushIntervalMsec(), thread_factory, store)), dispatcher_(api_->allocateDispatcher(time_system)), singleton_manager_(new Singleton::ManagerImpl()), - access_log_manager_(*api_, *dispatcher_, access_log_lock, store), mutex_tracer_(nullptr) { + access_log_manager_(*api_, *dispatcher_, access_log_lock), mutex_tracer_(nullptr) { try { initialize(options, local_address, component_factory); } catch (const EnvoyException& e) { diff --git a/source/server/server.cc b/source/server/server.cc index 554cd202cd92d..0522985656b2b 100644 --- a/source/server/server.cc +++ b/source/server/server.cc @@ -53,7 +53,8 @@ InstanceImpl::InstanceImpl(Options& options, Event::TimeSystem& time_system, ThreadLocal::Instance& tls, Thread::ThreadFactory& thread_factory) : shutdown_(false), options_(options), time_system_(time_system), restarter_(restarter), start_time_(time(nullptr)), original_start_time_(start_time_), stats_store_(store), - thread_local_(tls), api_(new Api::Impl(options.fileFlushIntervalMsec(), thread_factory)), + thread_local_(tls), + api_(new Api::Impl(options.fileFlushIntervalMsec(), thread_factory, store)), secret_manager_(std::make_unique()), dispatcher_(api_->allocateDispatcher(time_system)), singleton_manager_(new Singleton::ManagerImpl()), @@ -61,7 +62,7 @@ InstanceImpl::InstanceImpl(Options& options, Event::TimeSystem& time_system, random_generator_(std::move(random_generator)), listener_component_factory_(*this), worker_factory_(thread_local_, *api_, hooks, time_system), dns_resolver_(dispatcher_->createDnsResolver({})), - access_log_manager_(*api_, *dispatcher_, access_log_lock, store), terminated_(false), + access_log_manager_(*api_, *dispatcher_, access_log_lock), terminated_(false), mutex_tracer_(options.mutexTracingEnabled() ? &Envoy::MutexTracerImpl::getOrCreateTracer() : nullptr) { diff --git a/test/common/access_log/access_log_manager_impl_test.cc b/test/common/access_log/access_log_manager_impl_test.cc index d4b644ad6db7e..cd39e86db0249 100644 --- a/test/common/access_log/access_log_manager_impl_test.cc +++ b/test/common/access_log/access_log_manager_impl_test.cc @@ -25,10 +25,10 @@ TEST(AccessLogManagerImpl, reopenAllFiles) { std::shared_ptr log1(new Filesystem::MockFile()); std::shared_ptr log2(new Filesystem::MockFile()); - AccessLogManagerImpl access_log_manager(api, dispatcher, lock, stats_store); - EXPECT_CALL(api, createFile("foo", _, _, _)).WillOnce(Return(log1)); + AccessLogManagerImpl access_log_manager(api, dispatcher, lock); + EXPECT_CALL(api, createFile("foo", _, _)).WillOnce(Return(log1)); access_log_manager.createAccessLog("foo"); - EXPECT_CALL(api, createFile("bar", _, _, _)).WillOnce(Return(log2)); + EXPECT_CALL(api, createFile("bar", _, _)).WillOnce(Return(log2)); access_log_manager.createAccessLog("bar"); // Make sure that getting the access log with the same name returns the same underlying file. diff --git a/test/common/api/BUILD b/test/common/api/BUILD index 087fbabf2b7af..29b4c3ae12dc8 100644 --- a/test/common/api/BUILD +++ b/test/common/api/BUILD @@ -13,6 +13,7 @@ envoy_cc_test( srcs = ["api_impl_test.cc"], deps = [ "//source/common/api:api_lib", + "//source/common/stats:isolated_store_lib", "//test/test_common:environment_lib", "//test/test_common:utility_lib", ], diff --git a/test/common/api/api_impl_test.cc b/test/common/api/api_impl_test.cc index e6001f482d60b..7a5c53efb03a2 100644 --- a/test/common/api/api_impl_test.cc +++ b/test/common/api/api_impl_test.cc @@ -2,6 +2,7 @@ #include #include "common/api/api_impl.h" +#include "common/stats/isolated_store_impl.h" #include "test/test_common/environment.h" #include "test/test_common/utility.h" @@ -11,20 +12,24 @@ namespace Envoy { namespace Api { -TEST(ApiImplTest, readFileToEnd) { - Impl api(std::chrono::milliseconds(1000), Thread::threadFactoryForTest()); +class ApiImplTest : public testing::Test { +protected: + ApiImplTest() : api_(createApiForTest(store_)) {} + Stats::IsolatedStoreImpl store_; + ApiPtr api_; +}; + +TEST_F(ApiImplTest, readFileToEnd) { const std::string data = "test read To End\nWith new lines."; const std::string file_path = TestEnvironment::writeStringToFileForTest("test_api_envoy", data); - EXPECT_EQ(data, api.fileReadToEnd(file_path)); + EXPECT_EQ(data, api_->fileReadToEnd(file_path)); } -TEST(ApiImplTest, fileExists) { - Impl api(std::chrono::milliseconds(1000), Thread::threadFactoryForTest()); - - EXPECT_TRUE(api.fileExists("/dev/null")); - EXPECT_FALSE(api.fileExists("/dev/blahblahblah")); +TEST_F(ApiImplTest, fileExists) { + EXPECT_TRUE(api_->fileExists("/dev/null")); + EXPECT_FALSE(api_->fileExists("/dev/blahblahblah")); } } // namespace Api diff --git a/test/common/event/BUILD b/test/common/event/BUILD index 26464d75d3dc8..d29918714b6a7 100644 --- a/test/common/event/BUILD +++ b/test/common/event/BUILD @@ -15,6 +15,7 @@ envoy_cc_test( "//source/common/api:api_lib", "//source/common/event:dispatcher_includes", "//source/common/event:dispatcher_lib", + "//source/common/stats:isolated_store_lib", "//test/mocks:common_lib", "//test/test_common:test_time_lib", "//test/test_common:utility_lib", diff --git a/test/common/event/dispatched_thread_impl_test.cc b/test/common/event/dispatched_thread_impl_test.cc index 8842a5110cc26..cf0717371c3a3 100644 --- a/test/common/event/dispatched_thread_impl_test.cc +++ b/test/common/event/dispatched_thread_impl_test.cc @@ -24,13 +24,13 @@ namespace Event { class DispatchedThreadTest : public testing::Test { protected: DispatchedThreadTest() - : config_(1000, 1000, 1000, 1000), api_(Api::createApiForTest()), + : config_(1000, 1000, 1000, 1000), api_(Api::createApiForTest(fakestats_)), thread_(*api_, test_time_.timeSystem()), guard_dog_(fakestats_, config_, test_time_.timeSystem(), *api_) {} void SetUp() { thread_.start(guard_dog_); } NiceMock config_; - NiceMock fakestats_; + Stats::IsolatedStoreImpl fakestats_; DangerousDeprecatedTestTime test_time_; Api::ApiPtr api_; DispatchedThreadImpl thread_; diff --git a/test/common/event/dispatcher_impl_test.cc b/test/common/event/dispatcher_impl_test.cc index 89d1b2eeda277..a61ce53064c09 100644 --- a/test/common/event/dispatcher_impl_test.cc +++ b/test/common/event/dispatcher_impl_test.cc @@ -5,6 +5,7 @@ #include "common/api/api_impl.h" #include "common/common/lock_guard.h" #include "common/event/dispatcher_impl.h" +#include "common/stats/isolated_store_impl.h" #include "test/mocks/common.h" #include "test/test_common/test_time.h" @@ -61,9 +62,10 @@ TEST(DeferredDeleteTest, DeferredDelete) { class DispatcherImplTest : public ::testing::Test { protected: DispatcherImplTest() - : dispatcher_(std::make_unique(test_time_.timeSystem())), + : api_(Api::createApiForTest(stat_store_)), + dispatcher_(std::make_unique(test_time_.timeSystem())), work_finished_(false) { - dispatcher_thread_ = Thread::threadFactoryForTest().createThread([this]() { + dispatcher_thread_ = api_->createThread([this]() { // Must create a keepalive timer to keep the dispatcher from exiting. std::chrono::milliseconds time_interval(500); keepalive_timer_ = dispatcher_->createTimer( @@ -81,6 +83,8 @@ class DispatcherImplTest : public ::testing::Test { DangerousDeprecatedTestTime test_time_; + Stats::IsolatedStoreImpl stat_store_; + Api::ApiPtr api_; Thread::ThreadPtr dispatcher_thread_; DispatcherPtr dispatcher_; Thread::MutexBasicLockable mu_; diff --git a/test/common/filesystem/filesystem_impl_test.cc b/test/common/filesystem/filesystem_impl_test.cc index eb7da18c69868..4ad6f651a37b4 100644 --- a/test/common/filesystem/filesystem_impl_test.cc +++ b/test/common/filesystem/filesystem_impl_test.cc @@ -30,21 +30,22 @@ using testing::Throw; namespace Envoy { -class FileSystemImplTest : public ::testing::Test { +class FileSystemImplTest : public testing::Test { protected: - FileSystemImplTest() : api_(Api::createApiForTest()) {} + FileSystemImplTest() + : file_system_(std::chrono::milliseconds(10000), Thread::threadFactoryForTest(), + stats_store_) {} - Api::ApiPtr api_; + const std::chrono::milliseconds timeout_40ms_{40}; + Stats::IsolatedStoreImpl stats_store_; + Filesystem::Instance file_system_; }; TEST_F(FileSystemImplTest, BadFile) { Event::MockDispatcher dispatcher; Thread::MutexBasicLockable lock; - Stats::IsolatedStoreImpl store; EXPECT_CALL(dispatcher, createTimer_(_)); - EXPECT_THROW( - Filesystem::FileImpl("", dispatcher, lock, store, *api_, std::chrono::milliseconds(10000)), - EnvoyException); + EXPECT_THROW(file_system_.createFile("", dispatcher, lock), EnvoyException); } TEST_F(FileSystemImplTest, fileExists) { @@ -121,15 +122,13 @@ TEST_F(FileSystemImplTest, flushToLogFilePeriodically) { NiceMock* timer = new NiceMock(&dispatcher); Thread::MutexBasicLockable mutex; - Stats::IsolatedStoreImpl stats_store; NiceMock os_sys_calls; TestThreadsafeSingletonInjector os_calls(&os_sys_calls); EXPECT_CALL(os_sys_calls, open_(_, _, _)).WillOnce(Return(5)); - Filesystem::FileImpl file("", dispatcher, mutex, stats_store, *api_, - std::chrono::milliseconds(40)); + Filesystem::FileSharedPtr file = file_system_.createFile("", dispatcher, mutex, timeout_40ms_); - EXPECT_CALL(*timer, enableTimer(std::chrono::milliseconds(40))); + EXPECT_CALL(*timer, enableTimer(timeout_40ms_)); EXPECT_CALL(os_sys_calls, write_(_, _, _)) .WillOnce(Invoke([](int fd, const void* buffer, size_t num_bytes) -> ssize_t { std::string written = std::string(reinterpret_cast(buffer), num_bytes); @@ -139,7 +138,7 @@ TEST_F(FileSystemImplTest, flushToLogFilePeriodically) { return num_bytes; })); - file.write("test"); + file->write("test"); { Thread::LockGuard lock(os_sys_calls.write_mutex_); @@ -158,8 +157,8 @@ TEST_F(FileSystemImplTest, flushToLogFilePeriodically) { })); // make sure timer is re-enabled on callback call - file.write("test2"); - EXPECT_CALL(*timer, enableTimer(std::chrono::milliseconds(40))); + file->write("test2"); + EXPECT_CALL(*timer, enableTimer(timeout_40ms_)); timer->callback_(); { @@ -175,23 +174,21 @@ TEST_F(FileSystemImplTest, flushToLogFileOnDemand) { NiceMock* timer = new NiceMock(&dispatcher); Thread::MutexBasicLockable mutex; - Stats::IsolatedStoreImpl stats_store; NiceMock os_sys_calls; TestThreadsafeSingletonInjector os_calls(&os_sys_calls); EXPECT_CALL(os_sys_calls, open_(_, _, _)).WillOnce(Return(5)); - Filesystem::FileImpl file("", dispatcher, mutex, stats_store, *api_, - std::chrono::milliseconds(40)); + Filesystem::FileSharedPtr file = file_system_.createFile("", dispatcher, mutex, timeout_40ms_); - EXPECT_CALL(*timer, enableTimer(std::chrono::milliseconds(40))); + EXPECT_CALL(*timer, enableTimer(timeout_40ms_)); // The first write to a given file will start the flush thread, which can flush // immediately (race on whether it will or not). So do a write and flush to // get that state out of the way, then test that small writes don't trigger a flush. EXPECT_CALL(os_sys_calls, write_(_, _, _)) .WillOnce(Invoke([](int, const void*, size_t num_bytes) -> ssize_t { return num_bytes; })); - file.write("prime-it"); - file.flush(); + file->write("prime-it"); + file->flush(); uint32_t expected_writes = 1; { Thread::LockGuard lock(os_sys_calls.write_mutex_); @@ -207,14 +204,14 @@ TEST_F(FileSystemImplTest, flushToLogFileOnDemand) { return num_bytes; })); - file.write("test"); + file->write("test"); { Thread::LockGuard lock(os_sys_calls.write_mutex_); EXPECT_EQ(expected_writes, os_sys_calls.num_writes_); } - file.flush(); + file->flush(); expected_writes++; { Thread::LockGuard lock(os_sys_calls.write_mutex_); @@ -231,8 +228,8 @@ TEST_F(FileSystemImplTest, flushToLogFileOnDemand) { })); // make sure timer is re-enabled on callback call - file.write("test2"); - EXPECT_CALL(*timer, enableTimer(std::chrono::milliseconds(40))); + file->write("test2"); + EXPECT_CALL(*timer, enableTimer(timeout_40ms_)); timer->callback_(); expected_writes++; @@ -249,14 +246,12 @@ TEST_F(FileSystemImplTest, reopenFile) { NiceMock* timer = new NiceMock(&dispatcher); Thread::MutexBasicLockable mutex; - Stats::IsolatedStoreImpl stats_store; NiceMock os_sys_calls; TestThreadsafeSingletonInjector os_calls(&os_sys_calls); Sequence sq; EXPECT_CALL(os_sys_calls, open_(_, _, _)).InSequence(sq).WillOnce(Return(5)); - Filesystem::FileImpl file("", dispatcher, mutex, stats_store, *api_, - std::chrono::milliseconds(40)); + Filesystem::FileSharedPtr file = file_system_.createFile("", dispatcher, mutex, timeout_40ms_); EXPECT_CALL(os_sys_calls, write_(_, _, _)) .InSequence(sq) @@ -268,7 +263,7 @@ TEST_F(FileSystemImplTest, reopenFile) { return num_bytes; })); - file.write("before"); + file->write("before"); timer->callback_(); { @@ -293,8 +288,8 @@ TEST_F(FileSystemImplTest, reopenFile) { EXPECT_CALL(os_sys_calls, close(10)).InSequence(sq); - file.reopen(); - file.write("reopened"); + file->reopen(); + file->write("reopened"); timer->callback_(); { @@ -325,12 +320,11 @@ TEST_F(FileSystemImplTest, reopenThrows) { Sequence sq; EXPECT_CALL(os_sys_calls, open_(_, _, _)).InSequence(sq).WillOnce(Return(5)); - Filesystem::FileImpl file("", dispatcher, mutex, stats_store, *api_, - std::chrono::milliseconds(40)); + Filesystem::FileSharedPtr file = file_system_.createFile("", dispatcher, mutex, timeout_40ms_); EXPECT_CALL(os_sys_calls, close(5)).InSequence(sq); EXPECT_CALL(os_sys_calls, open_(_, _, _)).InSequence(sq).WillOnce(Return(-1)); - file.write("test write"); + file->write("test write"); timer->callback_(); { Thread::LockGuard lock(os_sys_calls.write_mutex_); @@ -338,9 +332,9 @@ TEST_F(FileSystemImplTest, reopenThrows) { os_sys_calls.write_event_.wait(os_sys_calls.write_mutex_); } } - file.reopen(); + file->reopen(); - file.write("this is to force reopen"); + file->write("this is to force reopen"); timer->callback_(); { @@ -351,7 +345,7 @@ TEST_F(FileSystemImplTest, reopenThrows) { } // write call should not cause any exceptions - file.write("random data"); + file->write("random data"); timer->callback_(); } @@ -362,8 +356,7 @@ TEST_F(FileSystemImplTest, bigDataChunkShouldBeFlushedWithoutTimer) { NiceMock os_sys_calls; TestThreadsafeSingletonInjector os_calls(&os_sys_calls); - Filesystem::FileImpl file("", dispatcher, mutex, stats_store, *api_, - std::chrono::milliseconds(40)); + Filesystem::FileSharedPtr file = file_system_.createFile("", dispatcher, mutex, timeout_40ms_); EXPECT_CALL(os_sys_calls, write_(_, _, _)) .WillOnce(Invoke([](int fd, const void* buffer, size_t num_bytes) -> ssize_t { @@ -376,7 +369,7 @@ TEST_F(FileSystemImplTest, bigDataChunkShouldBeFlushedWithoutTimer) { return num_bytes; })); - file.write("a"); + file->write("a"); { Thread::LockGuard lock(os_sys_calls.write_mutex_); @@ -399,7 +392,7 @@ TEST_F(FileSystemImplTest, bigDataChunkShouldBeFlushedWithoutTimer) { })); std::string big_string(1024 * 64 + 1, 'b'); - file.write(big_string); + file->write(big_string); { Thread::LockGuard lock(os_sys_calls.write_mutex_); diff --git a/test/common/grpc/async_client_manager_impl_test.cc b/test/common/grpc/async_client_manager_impl_test.cc index 938c3895bdba0..c047970fc9682 100644 --- a/test/common/grpc/async_client_manager_impl_test.cc +++ b/test/common/grpc/async_client_manager_impl_test.cc @@ -18,10 +18,11 @@ namespace { class AsyncClientManagerImplTest : public testing::Test { public: - AsyncClientManagerImplTest() : api_(Api::createApiForTest()) {} + AsyncClientManagerImplTest() : api_(Api::createApiForTest(api_stats_store_)) {} Upstream::MockClusterManager cm_; NiceMock tls_; + Stats::IsolatedStoreImpl api_stats_store_; Stats::MockStore scope_; DangerousDeprecatedTestTime test_time_; Api::ApiPtr api_; diff --git a/test/common/grpc/google_async_client_impl_test.cc b/test/common/grpc/google_async_client_impl_test.cc index 813f93c20d434..4b9ea5fa299be 100644 --- a/test/common/grpc/google_async_client_impl_test.cc +++ b/test/common/grpc/google_async_client_impl_test.cc @@ -47,24 +47,26 @@ class MockStubFactory : public GoogleStubFactory { class EnvoyGoogleAsyncClientImplTest : public testing::Test { public: EnvoyGoogleAsyncClientImplTest() - : dispatcher_(test_time_.timeSystem()), api_(Api::createApiForTest()), + : dispatcher_(test_time_.timeSystem()), stats_store_(new Stats::IsolatedStoreImpl), + api_(Api::createApiForTest(*stats_store_)), scope_(stats_store_), method_descriptor_(helloworld::Greeter::descriptor()->FindMethodByName("SayHello")) { envoy::api::v2::core::GrpcService config; auto* google_grpc = config.mutable_google_grpc(); google_grpc->set_target_uri("fake_address"); google_grpc->set_stat_prefix("test_cluster"); tls_ = std::make_unique(*api_); - grpc_client_ = std::make_unique(dispatcher_, *tls_, stub_factory_, - stats_store_, config); + grpc_client_ = + std::make_unique(dispatcher_, *tls_, stub_factory_, scope_, config); } DangerousDeprecatedTestTime test_time_; Event::DispatcherImpl dispatcher_; + Stats::IsolatedStoreImpl* stats_store_; // Ownership transerred to scope_. Api::ApiPtr api_; + Stats::ScopeSharedPtr scope_; std::unique_ptr tls_; MockStubFactory stub_factory_; const Protobuf::MethodDescriptor* method_descriptor_; - Stats::ScopeSharedPtr stats_store_ = std::make_shared(); std::unique_ptr grpc_client_; }; diff --git a/test/common/grpc/grpc_client_integration_test_harness.h b/test/common/grpc/grpc_client_integration_test_harness.h index 1e38971e54310..7a9f8af73915c 100644 --- a/test/common/grpc/grpc_client_integration_test_harness.h +++ b/test/common/grpc/grpc_client_integration_test_harness.h @@ -209,7 +209,7 @@ class GrpcClientIntegrationTest : public GrpcClientIntegrationParamTest { public: GrpcClientIntegrationTest() : method_descriptor_(helloworld::Greeter::descriptor()->FindMethodByName("SayHello")), - dispatcher_(test_time_.timeSystem()), api_(Api::createApiForTest()) {} + dispatcher_(test_time_.timeSystem()), api_(Api::createApiForTest(*stats_store_)) {} virtual void initialize() { if (fake_upstream_ == nullptr) { @@ -403,8 +403,8 @@ class GrpcClientIntegrationTest : public GrpcClientIntegrationParamTest { DangerousDeprecatedTestTime test_time_; Event::DispatcherImpl dispatcher_; DispatcherHelper dispatcher_helper_{dispatcher_}; - Api::ApiPtr api_; Stats::IsolatedStoreImpl* stats_store_ = new Stats::IsolatedStoreImpl(); + Api::ApiPtr api_; Stats::ScopeSharedPtr stats_scope_{stats_store_}; TestMetadata service_wide_initial_metadata_; #ifdef ENVOY_GOOGLE_GRPC diff --git a/test/common/singleton/BUILD b/test/common/singleton/BUILD index cb66e6dcad8fd..8076c8bafbe52 100644 --- a/test/common/singleton/BUILD +++ b/test/common/singleton/BUILD @@ -23,6 +23,7 @@ envoy_cc_test( deps = [ "//source/common/common:thread_lib", "//source/common/singleton:threadsafe_singleton", + "//source/common/stats:isolated_store_lib", "//test/test_common:threadsafe_singleton_injector_lib", "//test/test_common:utility_lib", ], diff --git a/test/common/singleton/threadsafe_singleton_test.cc b/test/common/singleton/threadsafe_singleton_test.cc index 138d3309fc498..a9172aceade76 100644 --- a/test/common/singleton/threadsafe_singleton_test.cc +++ b/test/common/singleton/threadsafe_singleton_test.cc @@ -3,6 +3,7 @@ #include "common/common/lock_guard.h" #include "common/common/thread.h" #include "common/singleton/threadsafe_singleton.h" +#include "common/stats/isolated_store_impl.h" #include "test/test_common/threadsafe_singleton_injector.h" #include "test/test_common/utility.h" diff --git a/test/common/thread_local/BUILD b/test/common/thread_local/BUILD index 32912d8ccb76f..a1e1de967c805 100644 --- a/test/common/thread_local/BUILD +++ b/test/common/thread_local/BUILD @@ -14,6 +14,7 @@ envoy_cc_test( deps = [ "//source/common/api:api_lib", "//source/common/event:dispatcher_lib", + "//source/common/stats:isolated_store_lib", "//source/common/thread_local:thread_local_lib", "//test/mocks/event:event_mocks", "//test/test_common:test_time_lib", diff --git a/test/common/thread_local/thread_local_impl_test.cc b/test/common/thread_local/thread_local_impl_test.cc index 2f5e2c15f8884..0f81e38a13b16 100644 --- a/test/common/thread_local/thread_local_impl_test.cc +++ b/test/common/thread_local/thread_local_impl_test.cc @@ -1,5 +1,6 @@ #include "common/common/thread.h" #include "common/event/dispatcher_impl.h" +#include "common/stats/isolated_store_impl.h" #include "common/thread_local/thread_local_impl.h" #include "test/mocks/event/mocks.h" diff --git a/test/common/upstream/cluster_manager_impl_test.cc b/test/common/upstream/cluster_manager_impl_test.cc index 00f59f50a95d1..df7bc6b01011d 100644 --- a/test/common/upstream/cluster_manager_impl_test.cc +++ b/test/common/upstream/cluster_manager_impl_test.cc @@ -167,7 +167,7 @@ envoy::config::bootstrap::v2::Bootstrap parseBootstrapFromV2Yaml(const std::stri class ClusterManagerImplTest : public testing::Test { public: - ClusterManagerImplTest() : api_(Api::createApiForTest()) { + ClusterManagerImplTest() : api_(Api::createApiForTest(stats_store_)) { factory_.dispatcher_.setTimeSystem(time_system_); } @@ -243,6 +243,7 @@ class ClusterManagerImplTest : public testing::Test { return metadata; } + Stats::IsolatedStoreImpl stats_store_; Api::ApiPtr api_; NiceMock factory_; std::unique_ptr cluster_manager_; diff --git a/test/config_test/config_test.cc b/test/config_test/config_test.cc index 1074ec8dacadb..93eab2cf3c728 100644 --- a/test/config_test/config_test.cc +++ b/test/config_test/config_test.cc @@ -42,7 +42,8 @@ OptionsImpl asConfigYaml(const OptionsImpl& src) { class ConfigTest { public: - ConfigTest(const OptionsImpl& options) : api_(Api::createApiForTest()), options_(options) { + ConfigTest(const OptionsImpl& options) + : api_(Api::createApiForTest(stats_store_)), options_(options) { ON_CALL(server_, options()).WillByDefault(ReturnRef(options_)); ON_CALL(server_, random()).WillByDefault(ReturnRef(random_)); ON_CALL(server_, sslContextManager()).WillByDefault(ReturnRef(ssl_context_manager_)); @@ -90,6 +91,7 @@ class ConfigTest { server_.thread_local_.shutdownThread(); } + Stats::IsolatedStoreImpl stats_store_; Api::ApiPtr api_; NiceMock server_; NiceMock ssl_context_manager_; diff --git a/test/exe/main_common_test.cc b/test/exe/main_common_test.cc index c79a35b66112d..fb885c85987bf 100644 --- a/test/exe/main_common_test.cc +++ b/test/exe/main_common_test.cc @@ -192,6 +192,7 @@ class AdminRequestTest : public MainCommonTest { return envoy_return_; } + Stats::IsolatedStoreImpl stats_store_; std::unique_ptr envoy_thread_; std::unique_ptr main_common_; absl::Notification started_; diff --git a/test/integration/fake_upstream.cc b/test/integration/fake_upstream.cc index 6892c8e32fe0b..30afe79077d61 100644 --- a/test/integration/fake_upstream.cc +++ b/test/integration/fake_upstream.cc @@ -372,8 +372,9 @@ FakeUpstream::FakeUpstream(Network::TransportSocketFactoryPtr&& transport_socket FakeUpstream::FakeUpstream(Network::TransportSocketFactoryPtr&& transport_socket_factory, Network::SocketPtr&& listen_socket, FakeHttpConnection::Type type, Event::TestTimeSystem& time_system, bool enable_half_close) - : http_type_(type), socket_(std::move(listen_socket)), api_(Api::createApiForTest()), - time_system_(time_system), dispatcher_(api_->allocateDispatcher(time_system_)), + : http_type_(type), socket_(std::move(listen_socket)), + api_(Api::createApiForTest(stats_store_)), time_system_(time_system), + dispatcher_(api_->allocateDispatcher(time_system_)), handler_(new Server::ConnectionHandlerImpl(ENVOY_LOGGER(), *dispatcher_)), allow_unexpected_disconnects_(false), enable_half_close_(enable_half_close), listener_(*this), filter_chain_(Network::Test::createEmptyFilterChain(std::move(transport_socket_factory))) { diff --git a/test/integration/integration.cc b/test/integration/integration.cc index 5ed969354e4e8..1b2a96d03a1da 100644 --- a/test/integration/integration.cc +++ b/test/integration/integration.cc @@ -220,8 +220,8 @@ void IntegrationTcpClient::ConnectionCallbacks::onEvent(Network::ConnectionEvent BaseIntegrationTest::BaseIntegrationTest(Network::Address::IpVersion version, TestTimeSystemPtr time_system, const std::string& config) - : api_(Api::createApiForTest()), mock_buffer_factory_(new NiceMock), - time_system_(std::move(time_system)), + : api_(Api::createApiForTest(stats_store_)), + mock_buffer_factory_(new NiceMock), time_system_(std::move(time_system)), dispatcher_(new Event::DispatcherImpl(*time_system_, Buffer::WatermarkFactoryPtr{mock_buffer_factory_})), version_(version), config_helper_(version, config), diff --git a/test/integration/integration.h b/test/integration/integration.h index daba2a02df96d..85d7c93d39acb 100644 --- a/test/integration/integration.h +++ b/test/integration/integration.h @@ -177,6 +177,7 @@ class BaseIntegrationTest : Logger::Loggable { Event::TestTimeSystem& timeSystem() { return *time_system_; } + Stats::IsolatedStoreImpl stats_store_; Api::ApiPtr api_; MockBufferFactory* mock_buffer_factory_; // Will point to the dispatcher's factory. private: diff --git a/test/integration/utility.cc b/test/integration/utility.cc index 30740c5719181..76b280b4cbff8 100644 --- a/test/integration/utility.cc +++ b/test/integration/utility.cc @@ -18,6 +18,7 @@ #include "common/upstream/upstream_impl.h" #include "test/common/upstream/utility.h" +#include "test/mocks/stats/mocks.h" #include "test/mocks/upstream/mocks.h" #include "test/test_common/network_utility.h" #include "test/test_common/printers.h" @@ -61,8 +62,9 @@ IntegrationUtil::makeSingleRequest(const Network::Address::InstanceConstSharedPt const std::string& body, Http::CodecClient::Type type, const std::string& host, const std::string& content_type) { - Api::ApiPtr api(Api::createApiForTest()); - Event::DispatcherPtr dispatcher(api->allocateDispatcher(evil_singleton_test_time_.timeSystem())); + NiceMock mock_stats_store; + Api::Impl api(std::chrono::milliseconds(9000), Thread::threadFactoryForTest(), mock_stats_store); + Event::DispatcherPtr dispatcher(api.allocateDispatcher(evil_singleton_test_time_.timeSystem())); std::shared_ptr cluster{new NiceMock()}; Upstream::HostDescriptionConstSharedPtr host_description{ Upstream::makeTestHostDescription(cluster, "tcp://127.0.0.1:80")}; @@ -109,7 +111,7 @@ IntegrationUtil::makeSingleRequest(uint32_t port, const std::string& method, con RawConnectionDriver::RawConnectionDriver(uint32_t port, Buffer::Instance& initial_data, ReadCallback data_callback, Network::Address::IpVersion version) { - api_ = Api::createApiForTest(); + api_ = Api::createApiForTest(stats_store_); dispatcher_ = api_->allocateDispatcher(IntegrationUtil::evil_singleton_test_time_.timeSystem()); callbacks_ = std::make_unique(); client_ = dispatcher_->createClientConnection( diff --git a/test/integration/utility.h b/test/integration/utility.h index 2740b87463fc8..23630096ef289 100644 --- a/test/integration/utility.h +++ b/test/integration/utility.h @@ -13,6 +13,7 @@ #include "common/common/assert.h" #include "common/common/utility.h" #include "common/http/codec_client.h" +#include "common/stats/isolated_store_impl.h" #include "test/test_common/printers.h" #include "test/test_common/test_time.h" @@ -99,6 +100,7 @@ class RawConnectionDriver { }; Api::ApiPtr api_; + Stats::IsolatedStoreImpl stats_store_; Event::DispatcherPtr dispatcher_; std::unique_ptr callbacks_; Network::ClientConnectionPtr client_; diff --git a/test/mocks/api/mocks.cc b/test/mocks/api/mocks.cc index 2a577efed1156..626a791d723c9 100644 --- a/test/mocks/api/mocks.cc +++ b/test/mocks/api/mocks.cc @@ -12,7 +12,7 @@ using testing::Return; namespace Envoy { namespace Api { -MockApi::MockApi() { ON_CALL(*this, createFile(_, _, _, _)).WillByDefault(Return(file_)); } +MockApi::MockApi() { ON_CALL(*this, createFile(_, _, _)).WillByDefault(Return(file_)); } MockApi::~MockApi() {} diff --git a/test/mocks/api/mocks.h b/test/mocks/api/mocks.h index a3620bbd56c4f..e0fa75215e024 100644 --- a/test/mocks/api/mocks.h +++ b/test/mocks/api/mocks.h @@ -30,9 +30,9 @@ class MockApi : public Api { } MOCK_METHOD1(allocateDispatcher_, Event::Dispatcher*(Event::TimeSystem&)); - MOCK_METHOD4(createFile, + MOCK_METHOD3(createFile, Filesystem::FileSharedPtr(const std::string& path, Event::Dispatcher& dispatcher, - Thread::BasicLockable& lock, Stats::Store& stats_store)); + Thread::BasicLockable& lock)); MOCK_METHOD1(fileExists, bool(const std::string& path)); MOCK_METHOD1(fileReadToEnd, std::string(const std::string& path)); MOCK_METHOD1(createThread, Thread::ThreadPtr(std::function thread_routine)); diff --git a/test/server/config_validation/BUILD b/test/server/config_validation/BUILD index 8c5ae0b0d1e1f..908de378b27c3 100644 --- a/test/server/config_validation/BUILD +++ b/test/server/config_validation/BUILD @@ -73,6 +73,7 @@ envoy_cc_test( srcs = ["dispatcher_test.cc"], deps = [ "//source/common/event:libevent_lib", + "//source/common/stats:isolated_store_lib", "//source/server/config_validation:api_lib", "//source/server/config_validation:dns_lib", "//test/test_common:environment_lib", diff --git a/test/server/config_validation/cluster_manager_test.cc b/test/server/config_validation/cluster_manager_test.cc index f73d22d1b1e59..0216bb3033fbc 100644 --- a/test/server/config_validation/cluster_manager_test.cc +++ b/test/server/config_validation/cluster_manager_test.cc @@ -23,10 +23,10 @@ namespace Envoy { namespace Upstream { TEST(ValidationClusterManagerTest, MockedMethods) { - Api::ApiPtr api(Api::createApiForTest()); + Stats::IsolatedStoreImpl stats_store; + Api::ApiPtr api(Api::createApiForTest(stats_store)); NiceMock runtime; Event::SimulatedTimeSystem time_system; - Stats::IsolatedStoreImpl stats; NiceMock tls; NiceMock random; testing::NiceMock secret_manager; @@ -36,14 +36,14 @@ TEST(ValidationClusterManagerTest, MockedMethods) { LocalInfo::MockLocalInfo local_info; NiceMock admin; - ValidationClusterManagerFactory factory(runtime, stats, tls, random, dns_resolver, + ValidationClusterManagerFactory factory(runtime, stats_store, tls, random, dns_resolver, ssl_context_manager, dispatcher, local_info, secret_manager, *api); AccessLog::MockAccessLogManager log_manager; const envoy::config::bootstrap::v2::Bootstrap bootstrap; ClusterManagerPtr cluster_manager = factory.clusterManagerFromProto( - bootstrap, stats, tls, runtime, random, local_info, log_manager, admin); + bootstrap, stats_store, tls, runtime, random, local_info, log_manager, admin); EXPECT_EQ(nullptr, cluster_manager->httpConnPoolForCluster("cluster", ResourcePriority::Default, Http::Protocol::Http11, nullptr)); Host::CreateConnectionData data = cluster_manager->tcpConnForCluster("cluster", nullptr, nullptr); diff --git a/test/server/config_validation/dispatcher_test.cc b/test/server/config_validation/dispatcher_test.cc index 0ae3531e6ffd0..0c2474d9a5897 100644 --- a/test/server/config_validation/dispatcher_test.cc +++ b/test/server/config_validation/dispatcher_test.cc @@ -5,6 +5,7 @@ #include "common/event/libevent.h" #include "common/network/address_impl.h" #include "common/network/utility.h" +#include "common/stats/isolated_store_impl.h" #include "server/config_validation/api.h" @@ -23,13 +24,14 @@ class ConfigValidation : public ::testing::TestWithParam(std::chrono::milliseconds(1000), - Thread::threadFactoryForTest()); + validation_ = std::make_unique( + std::chrono::milliseconds(1000), Thread::threadFactoryForTest(), stats_store_); dispatcher_ = validation_->allocateDispatcher(test_time_.timeSystem()); } DangerousDeprecatedTestTime test_time_; Event::DispatcherPtr dispatcher_; + Stats::IsolatedStoreImpl stats_store_; private: // Using config validation API. diff --git a/test/server/configuration_impl_test.cc b/test/server/configuration_impl_test.cc index 13304556fd7fe..b5c5691c17705 100644 --- a/test/server/configuration_impl_test.cc +++ b/test/server/configuration_impl_test.cc @@ -53,12 +53,13 @@ TEST(FilterChainUtility, buildFilterChainFailWithBadFilters) { class ConfigurationImplTest : public testing::Test { protected: ConfigurationImplTest() - : api_(Api::createApiForTest()), + : api_(Api::createApiForTest(stats_store_)), cluster_manager_factory_(server_.runtime(), server_.stats(), server_.threadLocal(), server_.random(), server_.dnsResolver(), server_.sslContextManager(), server_.dispatcher(), server_.localInfo(), server_.secretManager(), *api_) {} + Stats::IsolatedStoreImpl stats_store_; Api::ApiPtr api_; NiceMock server_; Upstream::ProdClusterManagerFactory cluster_manager_factory_; diff --git a/test/server/guarddog_impl_test.cc b/test/server/guarddog_impl_test.cc index 1dbf0850185dd..e074ddf4cea99 100644 --- a/test/server/guarddog_impl_test.cc +++ b/test/server/guarddog_impl_test.cc @@ -26,9 +26,10 @@ namespace Server { class GuardDogTestBase : public testing::Test { protected: - GuardDogTestBase() : api_(Api::createApiForTest()) {} + GuardDogTestBase() : api_(Api::createApiForTest(stats_store_)) {} Event::SimulatedTimeSystem time_system_; + Stats::IsolatedStoreImpl stats_store_; Api::ApiPtr api_; }; diff --git a/test/server/worker_impl_test.cc b/test/server/worker_impl_test.cc index d5a8b4c3f4ea0..c4348252a24c4 100644 --- a/test/server/worker_impl_test.cc +++ b/test/server/worker_impl_test.cc @@ -24,12 +24,13 @@ namespace Server { class WorkerImplTest : public testing::Test { public: - WorkerImplTest() : api_(Api::createApiForTest()) { + WorkerImplTest() : api_(Api::createApiForTest(stats_store_)) { // In the real worker the watchdog has timers that prevent exit. Here we need to prevent event // loop exit since we use mock timers. no_exit_timer_->enableTimer(std::chrono::hours(1)); } + Stats::IsolatedStoreImpl stats_store_; NiceMock tls_; DangerousDeprecatedTestTime test_time; Event::DispatcherImpl* dispatcher_ = new Event::DispatcherImpl(test_time.timeSystem()); diff --git a/test/test_common/utility.cc b/test/test_common/utility.cc index ce095ac9e8f9a..f664e11a10e47 100644 --- a/test/test_common/utility.cc +++ b/test/test_common/utility.cc @@ -355,8 +355,9 @@ ThreadFactory& threadFactoryForTest() { namespace Api { -ApiPtr createApiForTest() { - return std::make_unique(std::chrono::milliseconds(1000), Thread::threadFactoryForTest()); +ApiPtr createApiForTest(Stats::Store& stat_store) { + return std::make_unique(std::chrono::milliseconds(1000), Thread::threadFactoryForTest(), + stat_store); } } // namespace Api diff --git a/test/test_common/utility.h b/test/test_common/utility.h index bba25fdcd82b7..29770fb4069cb 100644 --- a/test/test_common/utility.h +++ b/test/test_common/utility.h @@ -467,7 +467,7 @@ ThreadFactory& threadFactoryForTest(); } // namespace Thread namespace Api { -ApiPtr createApiForTest(); +ApiPtr createApiForTest(Stats::Store& stat_store); } // namespace Api MATCHER_P(HeaderMapEqualIgnoreOrder, rhs, "") {