1 package org.jboss.cache.statetransfer; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.Cache; 7 import org.jboss.cache.CacheSPI; 8 import org.jboss.cache.Fqn; 9 import org.jboss.cache.config.Configuration; 10 import org.jboss.cache.factories.DefaultCacheFactory; 11 import org.jboss.cache.misc.TestingUtil; 12 import org.jboss.cache.transaction.DummyTransactionManager; 13 14 import javax.naming.Context ; 15 import javax.naming.InitialContext ; 16 import javax.transaction.SystemException ; 17 import javax.transaction.UserTransaction ; 18 import java.util.Properties ; 19 20 26 public class StateTransferUnderLoadTest extends TestCase 27 { 28 Cache cache1, cache2; 29 Properties p = null; 30 String old_factory = null; 31 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 32 33 34 public StateTransferUnderLoadTest(String name) 35 { 36 super(name); 37 } 38 39 public void setUp() throws Exception 40 { 41 super.setUp(); 42 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 43 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 44 DummyTransactionManager.getInstance(); 45 if (p == null) 46 { 47 p = new Properties (); 48 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 49 } 50 } 51 52 53 public void tearDown() throws Exception 54 { 55 super.tearDown(); 56 if (cache2 != null) 57 { 58 cache2.stop(); 59 cache2 = null; 60 } 61 if (cache1 != null) 62 { 63 cache1.stop(); 64 cache1 = null; 65 } 66 67 DummyTransactionManager.destroy(); 69 if (old_factory != null) 70 { 71 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 72 old_factory = null; 73 } 74 } 75 76 public void testStateTransferDeadlocksPessimistic() throws Exception 77 { 78 runTest(false); 79 } 80 81 public void testStateTransferDeadlocksOptimistic() throws Exception 82 { 83 runTest(true); 84 } 85 86 87 private void runTest(boolean optimistic) throws Exception 88 { 89 Writer writer; 90 Configuration cfg1, cfg2; 91 cfg1 = new Configuration(); 92 cfg2 = new Configuration(); 93 cfg1.setCacheMode(Configuration.CacheMode.REPL_SYNC); 94 cfg2.setCacheMode(Configuration.CacheMode.REPL_SYNC); 95 96 if (optimistic) 97 { 98 cfg1.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC); 99 cfg2.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC); 100 } 101 102 cfg1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 103 cfg2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 104 105 cache1 = DefaultCacheFactory.getInstance().createCache(cfg1, true); 106 cache2 = DefaultCacheFactory.getInstance().createCache(cfg2, false); 107 UserTransaction tx1 = (UserTransaction ) new InitialContext (p).lookup("UserTransaction"); 108 writer = new Writer(cache1, tx1); 109 try 110 { 111 writer.start(); 112 113 114 cache2.create(); 115 for (int i = 0; i < 100; i++) 116 { 117 118 cache2.start(); 119 121 int num_nodes = ((CacheSPI) cache2).getNumberOfNodes(); 123 assertTrue(num_nodes >= 1); 124 125 TestingUtil.sleepThread(100); 126 cache2.stop(); 127 } 128 129 130 } 131 finally 132 { 133 if (writer != null) 134 writer.stop(); 135 } 136 } 137 138 139 static class Writer implements Runnable 140 { 141 Thread thread; 142 Cache cache; 143 boolean running = false; 144 UserTransaction tx; 145 146 147 public Writer(Cache cache, UserTransaction tx) 148 { 149 this.cache = cache; 150 this.tx = tx; 151 } 152 153 public void start() 154 { 155 thread = new Thread (this, "cache writer"); 156 running = true; 157 thread.start(); 158 } 159 160 public void stop() 161 { 162 running = false; 163 } 164 165 public void run() 166 { 167 Fqn fqn = Fqn.fromString("/a/b/c"); 168 while (running) 169 { 170 try 171 { 172 tx.begin(); 173 cache.put(fqn, "key", "value"); 174 tx.commit(); 175 } 176 catch (Exception e) 177 { 178 e.printStackTrace(); 179 try {tx.rollback();} catch (SystemException e1) {} 180 } 181 finally 182 { 183 TestingUtil.sleepRandom(100); 184 } 185 } 186 } 187 188 } 189 190 191 private static void log(String msg) 192 { 193 System.out.println(Thread.currentThread().getName() + ": " + msg); 194 } 195 196 197 public static Test suite() throws Exception 198 { 199 return new TestSuite(StateTransferUnderLoadTest.class); 200 } 201 202 public static void main(String [] args) throws Exception 203 { 204 junit.textui.TestRunner.run(StateTransferUnderLoadTest.suite()); 205 } 206 207 208 } 209 | Popular Tags |