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