Writing a test case to load guava cache

I am trying to test asynchronous cache load on top of my database. I am using the Google guava download cache which in turn uses the Cache loader updating the given key asynchronously (as described in https://code.google.com/p/guava-libraries/wiki/CachesExplained )

I tried following steps to check

  • Update duration: 1sec
  • Make a call to the cache, the key is missing. Call to DB layout made
  • Thread.sleep (2000L)
  • Make another call for the same key. Since the key is present and the update period has ended, a deprecated value should be returned and an asynchronous update should be started.
  • Thread.sleep (2000L)

  • Make another call for the same key.

Since I am making fun of the Tao, I expect it to be named at least twice. However, when I try to check the layout, it doesn't say that the layout was only called once. How to ensure that dao is called twice? I'm mocking the Thread Factory by creating an Executor that gets called to create a new thread as expected. Why was the second dao call never made? Did I miss something? I am using the new FixedThread Pool executor and decorating it as a listening decorator.

What is the correct way to test such refreshing caches? Any help is greatly appreciated.

+3


source to share


1 answer


Using

CacheBuilder.ticker(youtTicker)

      

and pass a FakeTicker

to specify the time in advance.



If you really want to test multi-threading behavior, forget about threads and test it synchronously.

Have a look at the source code, for example CacheRefreshTest .

Perhaps you can spot the problem by simplifying your test ... what can I tell without your exact code.

+6


source







All Articles