/* SPDX-FileCopyrightText: 2025 Blender Foundation * * SPDX-License-Identifier: Apache-2.0 */ #include #include "util/cache_limiter.h" CCL_NAMESPACE_BEGIN struct TestResource { int value; TestResource(int v) : value(v) {} }; TEST(CacheLimiter, Basic) { CacheLimiter limiter(2); CacheHandle handle1; /* Create a handle. */ { auto guard = handle1.acquire(limiter, []() { return std::make_unique(1); }); EXPECT_EQ(guard.get()->value, 1); } /* Verify handle is not created again. */ { bool created = false; auto guard = handle1.acquire(limiter, [&created]() { created = true; return std::make_unique(2); }); EXPECT_EQ(guard.get()->value, 1); EXPECT_FALSE(created); } } TEST(CacheLimiter, Eviction) { CacheLimiter limiter(1); CacheHandle handle1; CacheHandle handle2; /* Create two handles. */ { auto guard = handle1.acquire(limiter, []() { return std::make_unique(1); }); EXPECT_EQ(guard.get()->value, 1); } { bool created = false; auto guard = handle2.acquire(limiter, [&created]() { created = true; return std::make_unique(2); }); EXPECT_EQ(guard.get()->value, 2); EXPECT_TRUE(created); } /* Verify first handle got evicted and is created again. */ { bool created = false; auto guard = handle1.acquire(limiter, [&created]() { created = true; return std::make_unique(1); }); EXPECT_EQ(guard.get()->value, 1); EXPECT_TRUE(created); } } TEST(CacheLimiter, NoEvictionWhenUsed) { CacheLimiter limiter(1); CacheHandle handle1; CacheHandle handle2; /* Acquire and hold two guards at the same time. */ auto guard1 = handle1.acquire(limiter, []() { return std::make_unique(1); }); { bool created = false; auto guard2 = handle2.acquire(limiter, [&created]() { created = true; return std::make_unique(2); }); EXPECT_EQ(guard2.get()->value, 2); EXPECT_TRUE(created); } /* Check first handle is still valid. */ { EXPECT_EQ(guard1.get()->value, 1); } } CCL_NAMESPACE_END