1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.passivation; |
9 |
| |
10 |
| import junit.framework.TestCase; |
11 |
| import org.apache.commons.logging.Log; |
12 |
| import org.apache.commons.logging.LogFactory; |
13 |
| import org.jboss.cache.CacheImpl; |
14 |
| import org.jboss.cache.DefaultCacheFactory; |
15 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
16 |
| import org.jboss.cache.loader.DummyInMemoryCacheLoader; |
17 |
| import org.jboss.cache.misc.TestingUtil; |
18 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
19 |
| import org.jboss.cache.notifications.annotation.NodeActivated; |
20 |
| import org.jboss.cache.notifications.annotation.NodeLoaded; |
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 |
| public class LocalPassivationIntegrationTest extends TestCase |
29 |
| { |
30 |
| CacheImpl cache_; |
31 |
| protected final static Log log = LogFactory.getLog(LocalPassivationIntegrationTest.class); |
32 |
| int wakeupIntervalMillis_ = 0; |
33 |
| PassivationListener listener_; |
34 |
| private static final int LISTENER_WAIT_TIME = 200; |
35 |
| |
36 |
1
| public LocalPassivationIntegrationTest(String s)
|
37 |
| { |
38 |
1
| super(s);
|
39 |
1
| listener_ = new PassivationListener();
|
40 |
| } |
41 |
| |
42 |
1
| public void setUp() throws Exception
|
43 |
| { |
44 |
1
| super.setUp();
|
45 |
1
| cache_ = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
46 |
1
| initCaches(cache_);
|
47 |
1
| cache_.getConfiguration().setUseRegionBasedMarshalling(true);
|
48 |
| |
49 |
1
| cache_.start();
|
50 |
| |
51 |
1
| cache_.getNotifier().addCacheListener(listener_);
|
52 |
1
| listener_.resetCounter();
|
53 |
| |
54 |
1
| wakeupIntervalMillis_ = cache_.getConfiguration().getEvictionConfig().getWakeupIntervalSeconds() * 1000;
|
55 |
1
| log("wakeupInterval is " + wakeupIntervalMillis_);
|
56 |
1
| if (wakeupIntervalMillis_ <= 0)
|
57 |
| { |
58 |
0
| fail("testEviction(): eviction thread wake up interval is illegal " + wakeupIntervalMillis_);
|
59 |
| } |
60 |
| } |
61 |
| |
62 |
1
| void initCaches(CacheImpl cache) throws Exception
|
63 |
| { |
64 |
1
| cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/local-passivation-service.xml"));
|
65 |
1
| cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
66 |
1
| cache.getConfiguration().getCacheLoaderConfig().getFirstCacheLoaderConfig().setClassName(DummyInMemoryCacheLoader.class.getName());
|
67 |
| } |
68 |
| |
69 |
1
| public void tearDown() throws Exception
|
70 |
| { |
71 |
1
| super.tearDown();
|
72 |
1
| cache_.stop();
|
73 |
| } |
74 |
| |
75 |
| |
76 |
| |
77 |
1
| public void testActivationEvent() throws Exception
|
78 |
| { |
79 |
1
| String rootStr = "/org/jboss/test/data/";
|
80 |
1
| String str = rootStr + "0";
|
81 |
1
| cache_.remove("/");
|
82 |
1
| listener_.resetCounter();
|
83 |
| |
84 |
1
| cache_.put(str, str, str);
|
85 |
| |
86 |
1
| TestingUtil.sleepThread(20000);
|
87 |
1
| assertFalse("UnversionedNode should not exist", cache_.exists(str, str));
|
88 |
1
| String val = (String) cache_.get(str, str);
|
89 |
1
| assertNotNull("DataNode should be activated ", val);
|
90 |
1
| TestingUtil.sleepThread(LISTENER_WAIT_TIME);
|
91 |
1
| assertEquals("Eviction counter ", 1, listener_.getCounter());
|
92 |
| } |
93 |
| |
94 |
1
| void log(String msg)
|
95 |
| { |
96 |
1
| System.out.println("-- " + msg);
|
97 |
| } |
98 |
| |
99 |
| @CacheListener |
100 |
| public class PassivationListener |
101 |
| { |
102 |
| int counter = 0; |
103 |
| int loadedCounter = 0; |
104 |
| |
105 |
1
| public int getCounter()
|
106 |
| { |
107 |
1
| return counter;
|
108 |
| } |
109 |
| |
110 |
2
| public void resetCounter()
|
111 |
| { |
112 |
2
| counter = 0;
|
113 |
2
| loadedCounter = 0;
|
114 |
| } |
115 |
| |
116 |
3
| @NodeActivated
|
117 |
| public void nodeActivated(NodeEvent ne) |
118 |
| { |
119 |
3
| if (!ne.isPre())
|
120 |
| { |
121 |
1
| counter++;
|
122 |
1
| System.out.println("nodeActivate(): counter: " + counter);
|
123 |
| } |
124 |
| } |
125 |
| |
126 |
2
| @NodePassivated
|
127 |
| public void nodePassivated(NodeEvent ne) |
128 |
| { |
129 |
2
| if (ne.isPre())
|
130 |
| { |
131 |
1
| System.out.println("nodePassivate(): " + ne.getFqn());
|
132 |
| } |
133 |
| } |
134 |
| |
135 |
2
| @NodeLoaded
|
136 |
| public void nodeLoaded(Event e) |
137 |
| { |
138 |
2
| if (!e.isPre())
|
139 |
| { |
140 |
1
| loadedCounter++;
|
141 |
| } |
142 |
| } |
143 |
| |
144 |
| } |
145 |
| } |