Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 197   Methods: 9
NCLOC: 151   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ThreadedOptimisticCreateIfNotExistsInterceptorTest.java 100% 96.4% 88.9% 96%
coverage coverage
 1    /*
 2    * Created on 17-Feb-2005
 3    *
 4    *
 5    *
 6    */
 7    package org.jboss.cache.optimistic;
 8   
 9    import junit.framework.Test;
 10    import junit.framework.TestSuite;
 11    import org.jboss.cache.CacheImpl;
 12    import org.jboss.cache.Fqn;
 13    import org.jboss.cache.OptimisticTransactionEntry;
 14    import org.jboss.cache.interceptors.Interceptor;
 15    import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
 16    import org.jboss.cache.loader.SamplePojo;
 17    import org.jboss.cache.misc.TestingUtil;
 18    import org.jboss.cache.transaction.DummyTransactionManager;
 19   
 20    import javax.transaction.Transaction;
 21    import javax.transaction.TransactionManager;
 22   
 23    /**
 24    * @author xenephon
 25    */
 26    public class ThreadedOptimisticCreateIfNotExistsInterceptorTest extends AbstractOptimisticTestCase
 27    {
 28   
 29    /**
 30    * @param name
 31    */
 32  2 public ThreadedOptimisticCreateIfNotExistsInterceptorTest(String name)
 33    {
 34  2 super(name);
 35   
 36    }
 37   
 38  200 protected synchronized void setTransactionsInInvocationCtx(TransactionManager mgr, CacheImpl cache) throws Exception
 39    {
 40  200 cache.getInvocationContext().setTransaction(mgr.getTransaction());
 41  200 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction());
 42    }
 43   
 44  200 protected void resetInvocationCtx(CacheImpl cache)
 45    {
 46  200 cache.getInvocationContext().setTransaction(null);
 47  200 cache.getInvocationContext().setGlobalTransaction(null);
 48    }
 49   
 50  1 public void testDifferentTransactions() throws Exception
 51    {
 52   
 53  1 int numThreads = 100;
 54  1 final int minSleep = 0;
 55  1 final int maxSleep = 1000;
 56  1 TestListener listener = new TestListener();
 57  1 final CacheImpl cache = createCacheWithListener(listener);
 58   
 59  1 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
 60  1 interceptor.setCache(cache);
 61  1 Interceptor dummy = new MockInterceptor();
 62  1 dummy.setCache(cache);
 63  1 interceptor.setNext(dummy);
 64   
 65  1 cache.setInterceptorChain(interceptor);
 66   
 67    // should just be the root node
 68  1 assertEquals(0, cache.getNumberOfNodes());
 69   
 70  1 Runnable run = new Runnable()
 71    {
 72   
 73  100 public void run()
 74    {
 75  100 try
 76    {
 77    //start a new transaction in this thread
 78  100 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 79  100 mgr.begin();
 80  100 setTransactionsInInvocationCtx(mgr, cache);
 81  100 SamplePojo pojo = new SamplePojo(21, "test");
 82   
 83  100 cache.put("/one", "key1", pojo);
 84   
 85  100 randomSleep(minSleep, maxSleep);
 86   
 87  100 cache.put("/one/two", "key2", pojo);
 88   
 89  100 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) cache.getTransactionTable().get(cache.getCurrentTransaction());
 90  100 assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
 91  100 assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/")) != null);
 92  100 assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one")) != null);
 93  100 assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one/two")) != null);
 94  100 mgr.commit();
 95  100 resetInvocationCtx(cache);
 96    }
 97    catch (Exception e)
 98    {
 99  0 e.printStackTrace();
 100    }
 101    }
 102    };
 103  1 Thread[] threads = new Thread[numThreads];
 104  1 for (int i = 0; i < numThreads; i++)
 105    {
 106  100 Thread t = new Thread(run);
 107  100 t.start();
 108  100 threads[i] = t;
 109    }
 110  1 for (int i = 0; i < numThreads; i++)
 111    {
 112  100 threads[i].join();
 113    }
 114  1 cache.stop();
 115    }
 116   
 117  1 public void testDifferentThreadsSameTransaction() throws Exception
 118    {
 119  1 int numThreads = 100;
 120  1 final int minSleep = 0;
 121  1 final int maxSleep = 500;
 122  1 TestListener listener = new TestListener();
 123  1 final CacheImpl cache = createCacheWithListener(listener);
 124   
 125  1 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
 126  1 interceptor.setCache(cache);
 127  1 Interceptor dummy = new MockInterceptor();
 128  1 dummy.setCache(cache);
 129  1 interceptor.setNext(dummy);
 130   
 131  1 cache.setInterceptorChain(interceptor);
 132   
 133  1 final DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 134  1 mgr.begin();
 135  1 final Transaction tx = mgr.getTransaction();
 136   
 137  1 Runnable run = new Runnable()
 138    {
 139   
 140  100 public void run()
 141    {
 142  100 try
 143    {
 144    //start a new transaction in this thread
 145   
 146  100 mgr.setTransaction(tx);
 147  100 SamplePojo pojo = new SamplePojo(21, "test");
 148   
 149  100 setTransactionsInInvocationCtx(mgr, cache);
 150  100 cache.put("/one", "key1", pojo);
 151  100 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) cache.getTransactionTable().get(cache.getCurrentTransaction());
 152   
 153  100 randomSleep(minSleep, maxSleep);
 154   
 155  100 cache.put("/one/two", "key2", pojo);
 156  100 assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
 157  100 assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/")) != null);
 158  100 assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one")) != null);
 159  100 assertTrue(entry.getTransactionWorkSpace().getNode(Fqn.fromString("/one/two")) != null);
 160    }
 161    catch (Exception e)
 162    {
 163  0 e.printStackTrace();
 164    }
 165    finally
 166    {
 167  100 resetInvocationCtx(cache);
 168    }
 169    }
 170    };
 171  1 Thread[] threads = new Thread[numThreads];
 172  1 for (int i = 0; i < numThreads; i++)
 173    {
 174  100 Thread t = new Thread(run);
 175  100 t.start();
 176  100 threads[i] = t;
 177    }
 178  1 for (int i = 0; i < numThreads; i++)
 179    {
 180  100 threads[i].join();
 181    }
 182  1 mgr.commit();
 183   
 184  1 TestingUtil.sleepThread((long) 4000);
 185  1 cache.stop();
 186    }
 187   
 188  1 public static Test suite()
 189    {
 190  1 return new TestSuite(ThreadedOptimisticCreateIfNotExistsInterceptorTest.class);
 191    }
 192   
 193  0 public static void main(String[] args)
 194    {
 195  0 junit.textui.TestRunner.run(suite());
 196    }
 197    }