Reputation: 307
I am trying to test the asynchronous loading of cache over my database. I am using Google guava loading cache, which in turn uses Cache loader refreshing the given key asynchronously (as explained in https://code.google.com/p/guava-libraries/wiki/CachesExplained)
I was trying the following steps to test
Thread.sleep(2000L)
Make another call for the same key.
Since I am mocking the dao, I expect it to be called atleast twice. However, when I am trying to verify the mock, it fails saying that mock was just called once. How do I ensure the dao is called twice? I am mocking the Thread Factory, while creating Executor which is being called for creating a new thread, as expected. Why is the second call to dao never made? Am I missing anything here? I am using new FixedThread Pool executor and decorating it as listening decorator.
What is the right way to test such refreshing caches? Any help is much appreciated.
Upvotes: 3
Views: 8271
Reputation: 46482
Use
CacheBuilder.ticker(youtTicker)
and pass a FakeTicker
to advance the time at will.
Unless you really want to test the multithreaded behavior, forget threads and test it synchronously.
Look at the source code of e.g., CacheRefreshTest.
Maybe you can spot the problem by simplifying your test... that's all I can say without your exact code.
Upvotes: 10