Testing maximumSize? #717
-
I've created a cache with a max size of 500:
I'm trying to test that the max size is working:
When I run the test the cache is being used in the last when but I was expecting the actual method to be called since the cache should now have hit the 500 entry limit. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I am not really following the code to be certain what the question is. I think you are observing more than 500 entries cached, which is allowed temporarily by The cache defers eviction to the background executor, which by default is The Testing wiki page might be of interest. Please let me know if I misunderstood and did not answer your question. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to test the 500 capacity by seeding the cache with one entry (the first I guess I'm thinking too simplistically due to what you mentioned with things running in separate threads and also possibly that there's no guarantee that the first entry would be the one to be evicted. I personally don't like doing test like this as it's really testing the library which we know is already thoroughly tested, but our team wants to test the cache is configured correctly. Is there a way to verify the various settings of a cache? We are using Spring's caching so I don't necessarily have direct access to the caffeine cache but rather Spring's |
Beta Was this translation helpful? Give feedback.
-
For anybody looking for help testing a caffeine cache configuration while using Spring's
|
Beta Was this translation helpful? Give feedback.
I am not really following the code to be certain what the question is. I think you are observing more than 500 entries cached, which is allowed temporarily by
Caffeine.maximumSize
javadoc to better cope with concurrency.The cache defers eviction to the background executor, which by default is
ForkJoinPool.commonPool()
. In your case the test thread is likely running before the maintenance kicks in. If you useCaffeine.executor(Runnable::run)
then it will run on the caller. The cache's own operations are inexpensive, but due to user callbacks we can't know those penalties. Since usually one does not inspect the cache this is a good default, but can be annoying for tests.The Testing wiki p…