01: package junit.api;
02:
03: import junit.support.EventRecordingCacheListener;
04: import junit.support.MethodInvocation;
05: import static org.junit.Assert.assertEquals;
06: import static org.junit.Assert.assertNull;
07: import org.junit.Before;
08: import org.junit.Test;
09:
10: import static java.lang.Thread.sleep;
11: import java.util.LinkedList;
12: import java.util.List;
13:
14: /**
15: * Tests cache loading and eviction functionality of the cache
16: *
17: * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
18: */
19: public class CacheEvictionTest extends CacheAPITestBase {
20: private EventRecordingCacheListener listener;
21:
22: @Before
23: public void addCacheLoaderAndListener() {
24: listener = new EventRecordingCacheListener();
25: cache.addListener(listener);
26: }
27:
28: @Test
29: public void forcedEviction() throws InterruptedException {
30: cache.put(key, value, 10); // TTL 100ms
31: assertEquals(value, cache.peek(key));
32:
33: // wait till the TTL expires
34: sleep(200);
35: cache.evict();
36: assertNull(cache.peek(key));
37:
38: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
39: expected.add(new MethodInvocation(
40: CacheAPITestBase.listenerPutMethod, key, value));
41: expected.add(new MethodInvocation(
42: CacheAPITestBase.listenerEvictMethod, key));
43:
44: assertEquals(expected, listener.getEvents());
45: }
46:
47: @Test
48: public void transparentEviction() throws InterruptedException {
49: cache.put(key, value, 10); // TTL 100ms
50: assertEquals(value, cache.peek(key));
51:
52: // wait till the TTL expires
53: sleep(200);
54: assertNull(cache.peek(key));
55:
56: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
57: expected.add(new MethodInvocation(
58: CacheAPITestBase.listenerPutMethod, key, value));
59: expected.add(new MethodInvocation(
60: CacheAPITestBase.listenerExpiryMethod, key));
61:
62: assertEquals(expected, listener.getEvents());
63: }
64: }
|