1 |
| package org.jboss.cache.eviction; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.CacheImpl; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.interceptors.EvictionInterceptor; |
8 |
| import org.jboss.cache.misc.TestingUtil; |
9 |
| import org.jboss.cache.transaction.DummyTransactionManagerLookup; |
10 |
| |
11 |
| import javax.transaction.TransactionManager; |
12 |
| import java.util.Iterator; |
13 |
| import java.util.List; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class OptimisticEvictionTest extends TestCase |
24 |
| { |
25 |
| |
26 |
| |
27 |
| private static final int NUMBER_OF_RUNS = 1 << 20; |
28 |
| |
29 |
| private static final int NUMBER_NODES = 256; |
30 |
| |
31 |
| private Fqn region = Fqn.fromString("testingRegion"); |
32 |
| private TransactionManager txManager; |
33 |
| private CacheImpl cache; |
34 |
| |
35 |
4
| protected void setUp() throws Exception
|
36 |
| { |
37 |
4
| super.setUp();
|
38 |
| |
39 |
4
| txManager = new DummyTransactionManagerLookup().getTransactionManager();
|
40 |
4
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache("META-INF/optimistic-eviction.xml");
|
41 |
| } |
42 |
| |
43 |
4
| protected void tearDown() throws Exception
|
44 |
| { |
45 |
4
| if (cache != null)
|
46 |
| { |
47 |
| |
48 |
4
| try
|
49 |
| { |
50 |
4
| if (cache.getTransactionManager().getTransaction() != null)
|
51 |
| { |
52 |
0
| cache.getTransactionManager().rollback();
|
53 |
| } |
54 |
| } |
55 |
| catch (Exception e) |
56 |
| { |
57 |
| |
58 |
| } |
59 |
4
| cache.stop();
|
60 |
4
| cache = null;
|
61 |
| } |
62 |
4
| super.tearDown();
|
63 |
| } |
64 |
| |
65 |
| |
66 |
1
| public void testEvictionError() throws Exception
|
67 |
| { |
68 |
| |
69 |
1
| for (int i = 0; i < NUMBER_NODES; i++)
|
70 |
| { |
71 |
256
| cache.put(new Fqn(region, i), i, i);
|
72 |
| } |
73 |
| |
74 |
1
| for (int i = 0; i < NUMBER_OF_RUNS; i++)
|
75 |
| { |
76 |
1048576
| txManager.begin();
|
77 |
1048576
| cache.get(region, i % NUMBER_NODES);
|
78 |
1048576
| txManager.commit();
|
79 |
| } |
80 |
| } |
81 |
| |
82 |
| |
83 |
1
| public void testEvictionOccurence() throws Exception
|
84 |
| { |
85 |
1
| cache.put("/timeBased/test", "key", "value");
|
86 |
1
| assertTrue(cache.exists("/timeBased/test"));
|
87 |
| |
88 |
| |
89 |
1
| TestingUtil.sleepThread(3000);
|
90 |
1
| assertTrue(!cache.exists("/timeBased/test"));
|
91 |
| } |
92 |
| |
93 |
1
| public void testInterceptorChain() throws Exception
|
94 |
| { |
95 |
1
| List interceptors = cache.getInterceptors();
|
96 |
1
| System.out.println(interceptors);
|
97 |
1
| Iterator i = interceptors.iterator();
|
98 |
1
| boolean found = false;
|
99 |
| |
100 |
1
| while (i.hasNext())
|
101 |
| { |
102 |
10
| Object o = i.next();
|
103 |
10
| if (o instanceof EvictionInterceptor)
|
104 |
| { |
105 |
1
| found = true;
|
106 |
| } |
107 |
| } |
108 |
| |
109 |
1
| assertTrue("Eviction interceptor should be in interceptor chain.", found);
|
110 |
| } |
111 |
| |
112 |
1
| public void testCompleteRemoval() throws Exception
|
113 |
| { |
114 |
1
| String rootStr = "/timeBased/";
|
115 |
| |
116 |
| |
117 |
| |
118 |
1
| Fqn parent = Fqn.fromString(rootStr + "parent");
|
119 |
1
| cache.put(parent, "key", "value");
|
120 |
1
| cache.put(new Fqn(parent, "child"), "key", "value");
|
121 |
| |
122 |
| |
123 |
| |
124 |
1
| TestingUtil.sleepThread(5500);
|
125 |
1
| assertFalse("Parent completely removed", cache.getRoot().hasChild(parent));
|
126 |
| } |
127 |
| |
128 |
| } |