1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.passivation; |
9 |
| |
10 |
| import junit.framework.Test; |
11 |
| import junit.framework.TestCase; |
12 |
| import junit.framework.TestSuite; |
13 |
| import org.jboss.cache.CacheImpl; |
14 |
| import org.jboss.cache.DefaultCacheFactory; |
15 |
| import org.jboss.cache.Fqn; |
16 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
17 |
| import org.jboss.cache.loader.DummyInMemoryCacheLoader; |
18 |
| import org.jboss.cache.misc.TestingUtil; |
19 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
20 |
| import org.jboss.cache.notifications.annotation.NodeActivated; |
21 |
| import org.jboss.cache.notifications.annotation.NodePassivated; |
22 |
| import org.jboss.cache.notifications.event.Event; |
23 |
| import org.jboss.cache.notifications.event.NodeEvent; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| public class BasicPassivationTest extends TestCase |
30 |
| { |
31 |
| CacheImpl cache_; |
32 |
| int wakeupIntervalMillis_ = 0; |
33 |
| final String ROOT_STR = "/test"; |
34 |
| Throwable t1_ex, t2_ex; |
35 |
| final long DURATION = 10000; |
36 |
| boolean isTrue; |
37 |
| final String FQNSTR = "/org/jboss/3"; |
38 |
| int activationCount = 0; |
39 |
| int passivationCount = 0; |
40 |
| |
41 |
3
| public BasicPassivationTest(String s)
|
42 |
| { |
43 |
3
| super(s);
|
44 |
| } |
45 |
| |
46 |
3
| public void setUp() throws Exception
|
47 |
| { |
48 |
3
| super.setUp();
|
49 |
3
| initCaches();
|
50 |
3
| wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
|
51 |
3
| log("wakeupInterval is " + wakeupIntervalMillis_);
|
52 |
3
| if (wakeupIntervalMillis_ < 0)
|
53 |
| { |
54 |
0
| fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
|
55 |
| } |
56 |
| |
57 |
3
| t1_ex = t2_ex = null;
|
58 |
3
| isTrue = true;
|
59 |
| } |
60 |
| |
61 |
3
| void initCaches() throws Exception
|
62 |
| { |
63 |
3
| cache_ = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
64 |
3
| cache_.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-passivation-service.xml"));
|
65 |
3
| cache_.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
66 |
3
| Object listener = new TestCacheListener();
|
67 |
3
| cache_.getConfiguration().getCacheLoaderConfig().getFirstCacheLoaderConfig().setClassName(DummyInMemoryCacheLoader.class.getName());
|
68 |
3
| cache_.start();
|
69 |
3
| cache_.getNotifier().addCacheListener(listener);
|
70 |
| } |
71 |
| |
72 |
3
| public void tearDown() throws Exception
|
73 |
| { |
74 |
3
| super.tearDown();
|
75 |
3
| cache_.stop();
|
76 |
| } |
77 |
| |
78 |
1
| public void testBasic()
|
79 |
| { |
80 |
1
| activationCount = 0;
|
81 |
1
| passivationCount = 0;
|
82 |
1
| Fqn fqn = Fqn.fromString(FQNSTR);
|
83 |
1
| try
|
84 |
| { |
85 |
1
| cache_.put(fqn, FQNSTR, FQNSTR);
|
86 |
| } |
87 |
| catch (Exception e) |
88 |
| { |
89 |
0
| fail("Failed to insert data" + e);
|
90 |
0
| e.printStackTrace();
|
91 |
| } |
92 |
1
| System.out.println(cache_.toString());
|
93 |
1
| TestingUtil.sleepThread(21000);
|
94 |
1
| System.out.println(cache_.toString());
|
95 |
1
| try
|
96 |
| { |
97 |
1
| assertFalse(cache_.exists(FQNSTR, FQNSTR));
|
98 |
1
| String val = (String) cache_.get(FQNSTR, FQNSTR);
|
99 |
1
| assertNotNull("DataNode should not be empty ", val);
|
100 |
| } |
101 |
| catch (Exception e) |
102 |
| { |
103 |
0
| e.printStackTrace();
|
104 |
0
| fail("Failed to get" + e);
|
105 |
| } |
106 |
1
| assertEquals("activation count:", 1, activationCount);
|
107 |
1
| assertEquals("passivation count:", 1, passivationCount);
|
108 |
| } |
109 |
| |
110 |
1
| public void testDualPassivation() throws Exception
|
111 |
| { |
112 |
1
| Fqn fqn = Fqn.fromString(FQNSTR);
|
113 |
1
| cache_.put(fqn, "key", "value");
|
114 |
1
| cache_.evict(fqn);
|
115 |
1
| cache_.evict(fqn);
|
116 |
1
| assertEquals("Proper value after 2 passivations", "value", cache_.get(fqn, "key"));
|
117 |
| } |
118 |
| |
119 |
1
| public void testIntermingledPassivation() throws Exception
|
120 |
| { |
121 |
1
| Fqn fqn = Fqn.fromString(FQNSTR);
|
122 |
1
| cache_.put(fqn, "key1", "value");
|
123 |
1
| cache_.evict(fqn);
|
124 |
1
| cache_.put(fqn, "key2", "value");
|
125 |
1
| cache_.evict(fqn);
|
126 |
1
| assertEquals("Proper value after 2 passivations", "value", cache_.get(fqn, "key1"));
|
127 |
| |
128 |
| } |
129 |
| |
130 |
11
| void log(String msg)
|
131 |
| { |
132 |
11
| System.out.println("-- " + msg);
|
133 |
| } |
134 |
| |
135 |
1
| public static Test suite()
|
136 |
| { |
137 |
1
| return new TestSuite(org.jboss.cache.passivation.BasicPassivationTest.class);
|
138 |
| } |
139 |
| |
140 |
0
| public static void main(String[] args)
|
141 |
| { |
142 |
0
| junit.textui.TestRunner.run(org.jboss.cache.passivation.BasicPassivationTest.suite());
|
143 |
| } |
144 |
| |
145 |
| @CacheListener |
146 |
| public class TestCacheListener |
147 |
| { |
148 |
20
| @NodeActivated
|
149 |
| @NodePassivated |
150 |
| public void callback(NodeEvent ne) |
151 |
| { |
152 |
12
| if (ne.isPre()) return;
|
153 |
8
| if (!ne.getFqn().isChildOrEquals(Fqn.fromString(FQNSTR)))
|
154 |
0
| return;
|
155 |
| |
156 |
8
| log("got event " + ne);
|
157 |
8
| if (ne.getType() == Event.Type.NODE_ACTIVATED)
|
158 |
4
| activationCount++;
|
159 |
4
| else if (ne.getType() == Event.Type.NODE_PASSIVATED)
|
160 |
4
| passivationCount++;
|
161 |
| } |
162 |
| } |
163 |
| } |