1 7 package org.jboss.test.cache.test.eviction; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 import org.jboss.cache.Fqn; 13 import org.jboss.cache.PropertyConfigurator; 14 import org.jboss.cache.TreeCache; 15 16 19 public class ReplicatedLRUPolicyUnitTestCase extends TestCase 20 { 21 TreeCache cache_, cache1_, cache2_; 22 int wakeupIntervalMillis_ = 0; 23 24 public ReplicatedLRUPolicyUnitTestCase(String s) 25 { 26 super(s); 27 } 28 29 public void setUp() throws Exception 30 { 31 super.setUp(); 32 cache_ = new TreeCache(); 33 initCaches(cache_); 34 cache_.startService(); 37 cache2_ = new TreeCache(); 40 PropertyConfigurator config = new PropertyConfigurator(); 41 config.configure(cache2_, "META-INF/replSync-service.xml"); cache2_.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 43 cache2_.startService(); 44 45 wakeupIntervalMillis_ = cache_.getEvictionThreadWakeupIntervalSeconds() *1000; 46 log("wakeupInterval is " +wakeupIntervalMillis_); 47 if(wakeupIntervalMillis_ <=0) 48 fail("testEviction(): eviction thread wake up interval is illegal " +wakeupIntervalMillis_); 49 } 50 51 void initCaches(TreeCache cache) throws Exception 52 { 53 PropertyConfigurator config = new PropertyConfigurator(); 54 config.configure(cache, "META-INF/replSync-eviction-service.xml"); cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 56 } 57 58 public void tearDown() throws Exception 59 { 60 super.tearDown(); 61 cache_.stopService(); 62 cache2_.stopService(); 63 } 64 65 68 public void testEviction() { 69 String rootStr = "/org/jboss/test/data/"; 70 for(int i=0; i < 10; i++) { 71 String str = rootStr +i; 72 Fqn fqn = Fqn.fromString(str); 73 try { 74 cache_.put(fqn, str, str); 75 } catch (Exception e) { 76 fail("Failed to insert data" +e); 77 e.printStackTrace(); 78 } 79 } 80 81 _sleep(2 *wakeupIntervalMillis_); 82 try { 83 String val = (String )cache_.get(rootStr +"3", rootStr+"3"); 84 assertNull("Node should be evicted already ", val); 85 val = (String )cache2_.get(rootStr +"3", rootStr+"3"); 86 assertNotNull("Node should not be evicted here ", val); 87 } catch (Exception e) { 88 e.printStackTrace(); 89 } 90 } 91 92 public void testEvictionReplication() { 93 String rootStr = "/org/jboss/test/data/"; 94 for(int i=0; i < 10; i++) { 95 String str = rootStr +i; 96 Fqn fqn = Fqn.fromString(str); 97 try { 98 cache_.put(fqn, str, str); 99 } catch (Exception e) { 100 fail("Failed to insert data" +e); 101 e.printStackTrace(); 102 } 103 } 104 105 _sleep(wakeupIntervalMillis_ -1000); 106 String str = rootStr +"7"; 107 Fqn fqn = Fqn.fromString(str); 108 try { 109 cache_.get(fqn, str); 110 } catch (Exception e) { 111 fail("Failed to get data. Exception: " +e); 112 } 113 _sleep(wakeupIntervalMillis_); 114 115 try { 116 String val = (String )cache_.get(rootStr +"3", rootStr+"3"); 117 assertNull("Node should be empty ", val); 118 val = (String )cache2_.get(rootStr +"7", rootStr+"7"); 119 assertNotNull("Node should not be null", val); 120 } catch (Exception e) { 121 122 e.printStackTrace(); 123 } 124 } 125 126 void _sleep(long msecs) { 127 try { 128 Thread.sleep(msecs); 129 } catch (InterruptedException e) { 130 e.printStackTrace(); } 132 } 133 void log(String msg) 134 { 135 System.out.println("-- " + msg); 136 } 137 138 public static Test suite() 139 { 140 return new TestSuite(ReplicatedLRUPolicyUnitTestCase.class); 141 } 142 143 public static void main(String [] args) 144 { 145 junit.textui.TestRunner.run(suite()); 146 } 147 148 } 149 | Popular Tags |