Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 210   Methods: 13
NCLOC: 163   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
StateTransferUnderLoadTest.java 75% 97% 84.6% 91.6%
coverage coverage
 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.DefaultCacheFactory;
 9    import org.jboss.cache.Fqn;
 10    import org.jboss.cache.config.Configuration;
 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    /**
 21    * Tests state transfer while the other node keeps sending transactional, synchronous method calls
 22    *
 23    * @author Bela Ban
 24    * @version $Id: StateTransferUnderLoadTest.java,v 1.8 2007/01/11 13:49:07 msurtani Exp $
 25    */
 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  2 public StateTransferUnderLoadTest(String name)
 35    {
 36  2 super(name);
 37    }
 38   
 39  2 public void setUp() throws Exception
 40    {
 41  2 super.setUp();
 42  2 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
 43  2 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
 44  2 DummyTransactionManager.getInstance();
 45  2 if (p == null)
 46    {
 47  2 p = new Properties();
 48  2 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
 49    }
 50    }
 51   
 52   
 53  2 public void tearDown() throws Exception
 54    {
 55  2 super.tearDown();
 56  2 if (cache2 != null)
 57    {
 58  2 cache2.stop();
 59  2 cache2 = null;
 60    }
 61  2 if (cache1 != null)
 62    {
 63  2 cache1.stop();
 64  2 cache1 = null;
 65    }
 66   
 67    // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
 68  2 DummyTransactionManager.destroy();
 69  2 if (old_factory != null)
 70    {
 71  1 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
 72  1 old_factory = null;
 73    }
 74    }
 75   
 76  1 public void testStateTransferDeadlocksPessimistic() throws Exception
 77    {
 78  1 runTest(false);
 79    }
 80   
 81  1 public void testStateTransferDeadlocksOptimistic() throws Exception
 82    {
 83  1 runTest(true);
 84    }
 85   
 86   
 87  2 private void runTest(boolean optimistic) throws Exception
 88    {
 89  2 Writer writer;
 90  2 Configuration cfg1, cfg2;
 91  2 cfg1 = new Configuration();
 92  2 cfg2 = new Configuration();
 93  2 cfg1.setCacheMode(Configuration.CacheMode.REPL_SYNC);
 94  2 cfg2.setCacheMode(Configuration.CacheMode.REPL_SYNC);
 95   
 96  2 if (optimistic)
 97    {
 98  1 cfg1.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
 99  1 cfg2.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
 100    }
 101   
 102  2 cfg1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
 103  2 cfg2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
 104   
 105  2 cache1 = DefaultCacheFactory.getInstance().createCache(cfg1, true);
 106  2 cache2 = DefaultCacheFactory.getInstance().createCache(cfg2, false);
 107  2 UserTransaction tx1 = (UserTransaction) new InitialContext(p).lookup("UserTransaction");
 108  2 writer = new Writer(cache1, tx1);
 109  2 try
 110    {
 111  2 writer.start();
 112   
 113   
 114  2 cache2.create();
 115  2 for (int i = 0; i < 100; i++)
 116    {
 117   
 118  101 cache2.start();
 119    // gets state
 120   
 121    // check if state was retrieved successfully
 122  100 int num_nodes = ((CacheSPI) cache2).getNumberOfNodes();
 123  100 assertTrue(num_nodes >= 1);
 124   
 125  100 TestingUtil.sleepThread(100);
 126  100 cache2.stop();
 127    }
 128   
 129   
 130    }
 131    finally
 132    {
 133  2 if (writer != null)
 134    {
 135  2 writer.stop();
 136    }
 137    }
 138    }
 139   
 140   
 141    static class Writer implements Runnable
 142    {
 143    Thread thread;
 144    Cache cache;
 145    boolean running = false;
 146    UserTransaction tx;
 147   
 148   
 149  2 public Writer(Cache cache, UserTransaction tx)
 150    {
 151  2 this.cache = cache;
 152  2 this.tx = tx;
 153    }
 154   
 155  2 public void start()
 156    {
 157  2 thread = new Thread(this, "cache writer");
 158  2 running = true;
 159  2 thread.start();
 160    }
 161   
 162  2 public void stop()
 163    {
 164  2 running = false;
 165    }
 166   
 167  2 public void run()
 168    {
 169  2 Fqn fqn = Fqn.fromString("/a/b/c");
 170  2 while (running)
 171    {
 172  67 try
 173    {
 174  67 tx.begin();
 175  67 cache.put(fqn, "key", "value");
 176  67 tx.commit();
 177    }
 178    catch (Exception e)
 179    {
 180  1 e.printStackTrace();
 181  1 try {tx.rollback();} catch (SystemException e1) {}
 182    }
 183    finally
 184    {
 185  67 TestingUtil.sleepRandom(100);
 186    }
 187    }
 188    }
 189   
 190    }
 191   
 192   
 193  0 private static void log(String msg)
 194    {
 195  0 System.out.println(Thread.currentThread().getName() + ": " + msg);
 196    }
 197   
 198   
 199  1 public static Test suite() throws Exception
 200    {
 201  1 return new TestSuite(StateTransferUnderLoadTest.class);
 202    }
 203   
 204  0 public static void main(String[] args) throws Exception
 205    {
 206  0 junit.textui.TestRunner.run(StateTransferUnderLoadTest.suite());
 207    }
 208   
 209   
 210    }