KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > eviction > OptimisticEvictionTest


1 package org.jboss.cache.eviction;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.CacheImpl;
5 import org.jboss.cache.DummyTransactionManagerLookup;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.factories.XmlConfigurationParser;
8 import org.jboss.cache.interceptors.EvictionInterceptor;
9 import org.jboss.cache.misc.TestingUtil;
10
11 import javax.transaction.TransactionManager JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * Tests the eviction and the possible lack of locking nodes.
17  * The configuration is with an aggressive eviction policy, 100 objects 2 seconds interval.
18  * <p/>
19  * It is possible that the number needs to be changed a little, depending on the machine speed.
20  *
21  * @author fhenning
22  */

23 public class OptimisticEvictionTest extends TestCase
24 {
25
26    //Maximum number of runs 2^20
27
private static final int NUMBER_OF_RUNS = 1 << 20;
28    //Initial number of nodes
29
private static final int NUMBER_NODES = 256;
30
31    private Fqn region = Fqn.fromString("testingRegion");
32    private TransactionManager JavaDoc txManager;
33    private CacheImpl cache;
34
35    protected void setUp() throws Exception JavaDoc
36    {
37       super.setUp();
38
39       txManager = new DummyTransactionManagerLookup().getTransactionManager();
40       cache = new CacheImpl();
41       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/optimistic-eviction.xml"));
42       cache.start();
43    }
44
45    protected void tearDown() throws Exception JavaDoc
46    {
47       if (cache != null)
48       {
49          cache.stop();
50          cache = null;
51       }
52       super.tearDown();
53    }
54
55
56    public void testEvictionError() throws Exception JavaDoc
57    {
58       //Initialize the cache via a map
59
for (int i = 0; i < NUMBER_NODES; i++)
60       {
61          cache.put(new Fqn(region, i), i, i);
62       }
63
64       for (int i = 0; i < NUMBER_OF_RUNS; i++)
65       {
66          txManager.begin();
67          cache.get(region, i % NUMBER_NODES);
68          txManager.commit();
69       }
70    }
71
72
73    public void testEvictionOccurence() throws Exception JavaDoc
74    {
75       cache.put("/timeBased/test", "key", "value");
76       assertTrue(cache.exists("/timeBased/test"));
77
78       // wait for it to be evicted.
79
TestingUtil.sleepThread(3000);
80       assertTrue(!cache.exists("/timeBased/test"));
81    }
82
83    public void testInterceptorChain() throws Exception JavaDoc
84    {
85       List JavaDoc interceptors = cache.getInterceptors();
86       System.out.println(interceptors);
87       Iterator JavaDoc i = interceptors.iterator();
88       boolean found = false;
89
90       while (i.hasNext())
91       {
92          Object JavaDoc o = i.next();
93          if (o instanceof EvictionInterceptor)
94          {
95             found = true;
96          }
97       }
98
99       assertTrue("Eviction interceptor should be in interceptor chain.", found);
100    }
101
102 }
103
Popular Tags