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.config.Configuration.CacheMode; |
8 |
| import org.jboss.cache.factories.UnitTestCacheConfigurationFactory; |
9 |
| import org.jboss.cache.misc.TestingUtil; |
10 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
11 |
| import org.jboss.cache.notifications.annotation.NodeEvicted; |
12 |
| import org.jboss.cache.notifications.event.Event; |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| public class ReplicatedLRUPolicyTest extends TestCase |
18 |
| { |
19 |
| CacheImpl cache_, cache1_, cache2_; |
20 |
| int wakeupIntervalMillis_ = 0; |
21 |
| EvictionListener listener_; |
22 |
| |
23 |
3
| public ReplicatedLRUPolicyTest(String s)
|
24 |
| { |
25 |
3
| super(s);
|
26 |
3
| listener_ = new EvictionListener();
|
27 |
| } |
28 |
| |
29 |
3
| public void setUp() throws Exception
|
30 |
| { |
31 |
3
| super.setUp();
|
32 |
3
| cache_ = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
33 |
3
| initCaches(cache_);
|
34 |
3
| cache_.getConfiguration().setUseRegionBasedMarshalling(true);
|
35 |
3
| cache_.start();
|
36 |
3
| cache_.getNotifier().addCacheListener(listener_);
|
37 |
0
| listener_.resetCounter();
|
38 |
| |
39 |
0
| cache2_ = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
40 |
0
| cache2_.setConfiguration(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC));
|
41 |
0
| cache2_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
42 |
0
| cache2_.getConfiguration().setUseRegionBasedMarshalling(true);
|
43 |
0
| cache2_.start();
|
44 |
| |
45 |
0
| wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
|
46 |
0
| log("wakeupInterval is " + wakeupIntervalMillis_);
|
47 |
0
| if (wakeupIntervalMillis_ <= 0)
|
48 |
| { |
49 |
0
| fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
|
50 |
| } |
51 |
| } |
52 |
| |
53 |
3
| void initCaches(CacheImpl cache) throws Exception
|
54 |
| { |
55 |
3
| cache.setConfiguration(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC, true));
|
56 |
3
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
57 |
| } |
58 |
| |
59 |
0
| public void tearDown() throws Exception
|
60 |
| { |
61 |
0
| super.tearDown();
|
62 |
0
| cache_.stop();
|
63 |
0
| cache2_.stop();
|
64 |
| } |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
0
| public void testBasic() throws Exception
|
70 |
| { |
71 |
0
| String rootStr = "/org/jboss/test/data/";
|
72 |
0
| String str = rootStr + "0";
|
73 |
0
| cache_.put(str, str, str);
|
74 |
| |
75 |
0
| TestingUtil.sleepThread(30000);
|
76 |
0
| Object node = cache_.peek(Fqn.fromString(str), false);
|
77 |
0
| assertNull("DataNode should be evicted already ", node);
|
78 |
0
| assertEquals("Eviction counter ", 1, listener_.getCounter());
|
79 |
0
| String val = (String) cache2_.get(str, str);
|
80 |
0
| assertNotNull("DataNode should not be evicted here ", val);
|
81 |
0
| assertEquals("Eviction counter ", 1, listener_.getCounter());
|
82 |
| } |
83 |
| |
84 |
0
| public void testEviction() throws Exception
|
85 |
| { |
86 |
0
| String rootStr = "/org/jboss/test/data/";
|
87 |
0
| for (int i = 0; i < 10; i++)
|
88 |
| { |
89 |
0
| String str = rootStr + i;
|
90 |
0
| Fqn fqn = Fqn.fromString(str);
|
91 |
0
| cache_.put(fqn, str, str);
|
92 |
| } |
93 |
| |
94 |
0
| TestingUtil.sleepThread(2 * wakeupIntervalMillis_);
|
95 |
0
| String val = (String) cache_.get(rootStr + "3", rootStr + "3");
|
96 |
0
| assertNull("DataNode should be evicted already ", val);
|
97 |
0
| val = (String) cache2_.get(rootStr + "3", rootStr + "3");
|
98 |
0
| assertNotNull("DataNode should not be evicted here ", val);
|
99 |
| } |
100 |
| |
101 |
0
| public void testEvictionReplication() throws Exception
|
102 |
| { |
103 |
0
| String rootStr = "/org/jboss/test/data/";
|
104 |
0
| for (int i = 0; i < 10; i++)
|
105 |
| { |
106 |
0
| String str = rootStr + i;
|
107 |
0
| Fqn fqn = Fqn.fromString(str);
|
108 |
0
| cache_.put(fqn, str, str);
|
109 |
| } |
110 |
| |
111 |
0
| TestingUtil.sleepThread(wakeupIntervalMillis_ - 1000);
|
112 |
0
| String str = rootStr + "7";
|
113 |
0
| Fqn fqn = Fqn.fromString(str);
|
114 |
0
| cache_.get(fqn, str);
|
115 |
0
| TestingUtil.sleepThread(wakeupIntervalMillis_);
|
116 |
| |
117 |
0
| String val = (String) cache_.get(rootStr + "3", rootStr + "3");
|
118 |
0
| assertNull("DataNode should be empty ", val);
|
119 |
0
| val = (String) cache2_.get(rootStr + "7", rootStr + "7");
|
120 |
0
| assertNotNull("DataNode should not be null", val);
|
121 |
| } |
122 |
| |
123 |
0
| void log(String msg)
|
124 |
| { |
125 |
0
| System.out.println("-- " + msg);
|
126 |
| } |
127 |
| |
128 |
| @CacheListener |
129 |
| class EvictionListener |
130 |
| { |
131 |
| int counter = 0; |
132 |
| |
133 |
0
| public int getCounter()
|
134 |
| { |
135 |
0
| return counter;
|
136 |
| } |
137 |
| |
138 |
0
| public void resetCounter()
|
139 |
| { |
140 |
0
| counter = 0;
|
141 |
| } |
142 |
| |
143 |
0
| @NodeEvicted
|
144 |
| public void nodeEvicted(Event e) |
145 |
| { |
146 |
0
| System.out.println(e);
|
147 |
0
| if (e.isPre()) counter++;
|
148 |
| } |
149 |
| } |
150 |
| } |