diff options
author | Benety Goh <benety@mongodb.com> | 2020-05-25 10:28:13 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-05-26 10:51:46 +0000 |
commit | f702704ffe0279713698f15fe0ffcca46a1d3186 (patch) | |
tree | 47361d91f79745ecca4fb2843974ff47d2d0808d | |
parent | 5ecf380910f427e079617ffb0aa4dba1a6e388b4 (diff) | |
download | mongo-f702704ffe0279713698f15fe0ffcca46a1d3186.tar.gz |
SERVER-48184 Rewrite CacheSizeZero from ReadThroughCacheTest to use the MockThreadPool
(cherry picked from commit 34166ec206a7c0d1bb7da719f5f1efdf53cc2c4e)
-rw-r--r-- | src/mongo/util/read_through_cache_test.cpp | 77 |
1 files changed, 43 insertions, 34 deletions
diff --git a/src/mongo/util/read_through_cache_test.cpp b/src/mongo/util/read_through_cache_test.cpp index 2a82c0e416b..caad00aaf06 100644 --- a/src/mongo/util/read_through_cache_test.cpp +++ b/src/mongo/util/read_through_cache_test.cpp @@ -110,24 +110,6 @@ TEST_F(ReadThroughCacheTest, FetchInvalidateAndRefetch) { } } -TEST_F(ReadThroughCacheTest, CacheSizeZero) { - int countLookups = 0; - CacheWithThreadPool cache( - getServiceContext(), 0, [&](OperationContext*, const std::string& key) { - ASSERT_EQ("TestKey", key); - countLookups++; - - return CachedValue{100 * countLookups}; - }); - - for (int i = 1; i <= 3; i++) { - auto value = cache.acquire(_opCtx, "TestKey"); - ASSERT(value); - ASSERT_EQ(100 * i, value->counter); - ASSERT_EQ(i, countLookups); - } -} - TEST_F(ReadThroughCacheTest, InvalidateCacheSizeZeroReissuesLookup) { int countLookups = 0; CacheWithThreadPool cache( @@ -296,23 +278,30 @@ TEST_F(ReadThroughCacheTestAsync, AcquireWithAShutdownThreadPool) { ASSERT_THROWS_CODE(future.get(), DBException, ErrorCodes::ShutdownInProgress); } -TEST_F(ReadThroughCacheTestAsync, InvalidateCalledBeforeLookupTaskExecutes) { - struct MockThreadPool : public ThreadPoolInterface { - void startup() override {} - void shutdown() override {} - void join() override {} - void schedule(Task task) override { - ASSERT(!mostRecentTask); - mostRecentTask = std::move(task); - } - void runMostRecentTask() { - ASSERT(mostRecentTask); - auto f = std::move(mostRecentTask); - f(Status::OK()); - } +class MockThreadPool : public ThreadPoolInterface { +public: + ~MockThreadPool() { + ASSERT(!_mostRecentTask); + } + void startup() override {} + void shutdown() override {} + void join() override {} + void schedule(Task task) override { + ASSERT(!_mostRecentTask); + _mostRecentTask = std::move(task); + } + void runMostRecentTask() { + ASSERT(_mostRecentTask); + auto f = std::move(_mostRecentTask); + f(Status::OK()); + } + +private: + Task _mostRecentTask; +}; - Task mostRecentTask; - } threadPool; +TEST_F(ReadThroughCacheTestAsync, InvalidateCalledBeforeLookupTaskExecutes) { + MockThreadPool threadPool; Cache cache(getServiceContext(), threadPool, 1, [&](OperationContext*, const std::string&) { return CachedValue(123); @@ -330,5 +319,25 @@ TEST_F(ReadThroughCacheTestAsync, InvalidateCalledBeforeLookupTaskExecutes) { ASSERT_EQ(123, future.get()->counter); } +TEST_F(ReadThroughCacheTestAsync, CacheSizeZero) { + MockThreadPool threadPool; + int countLookups = 0; + Cache cache(getServiceContext(), threadPool, 0, [&](OperationContext*, const std::string& key) { + ASSERT_EQ("TestKey", key); + countLookups++; + + return CachedValue{100 * countLookups}; + }); + + for (int i = 1; i <= 3; i++) { + auto future = cache.acquireAsync("TestKey"); + threadPool.runMostRecentTask(); + auto value = future.get(); + ASSERT(value); + ASSERT_EQ(100 * i, value->counter); + ASSERT_EQ(i, countLookups); + } +} + } // namespace } // namespace mongo |