Skip to content

Instantly share code, notes, and snippets.

@nekman
Created September 5, 2012 07:40
Show Gist options
  • Select an option

  • Save nekman/3632732 to your computer and use it in GitHub Desktop.

Select an option

Save nekman/3632732 to your computer and use it in GitHub Desktop.
package se.labs.cache.tests;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import se.labs.cache.ItemCacheImpl;
import se.labs.cache.ItemCache;
import se.labs.cache.ItemCacheLoader;
public class ItemCacheTest {
@Test
public void shouldReloadWhenExpired() throws Exception {
@SuppressWarnings("unchecked")
ItemCacheLoader<String> loader = mock(ItemCacheLoader.class);
when(loader.reload()).thenReturn("1");
ItemCache<String> stringCache = new ItemCacheImpl<String>(loader, 1);
assertThat(stringCache.getItem(), is("1"));
// Next time the cache reloads, return "2".
when(loader.reload()).thenReturn("2");
// Sleep two seconds, to make sure that the cache gets reloaded.
Thread.sleep(2000);
assertThat(stringCache.getItem(), is("2"));
}
@Test
public void shouldReloadWhenInvalidated() throws Exception {
@SuppressWarnings("unchecked")
ItemCacheLoader<Integer> loader = mock(ItemCacheLoader.class);
when(loader.reload()).thenReturn(1);
ItemCache<Integer> intCache = new ItemCacheImpl<Integer>(loader);
assertThat(intCache.getItem(), is(1));
// Next time the cache reloads, return 2.
when(loader.reload()).thenReturn(2);
// Call invalidate, to make sure that the cache gets reloaded.
intCache.invalidate();
assertThat(intCache.getItem(), is(2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment