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