/*
 * JBoss, the OpenSource J2EE webOS
 * 
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.test.tm.test;

import javax.transaction.Transaction;

/**
 * A simple transaction local stress test.
 * 
 * @author <a href="adrian@jboss.com">Adrian Brock</a>
 * @version $Revision: 1.1.2.2 $
 */
public class SimpleTransactionLocalStressTestCase extends AbstractTransactionLocalStressTest
{
   public void testSimpleTransactionLocalStressTestcase() throws Throwable
   {
      tm.setTransactionTimeout(0);
      tm.begin();
      final Transaction tx = tm.suspend();
      SimpleTransactionLocalRunnable[] runnables = new SimpleTransactionLocalRunnable[getThreadCount()];
      for (int i = 0; i < runnables.length; ++i)
         runnables[i] = new SimpleTransactionLocalRunnable(tx);

      runConcurrentTest(runnables, new ConcurrentTestCallback()
            {
               public void finished() throws Throwable
               {
                  tm.resume(tx);
                  tm.commit();
               }
            });
   }
   
   public class SimpleTransactionLocalRunnable extends ConcurrentTransactionLocalRunnable
   {
      public SimpleTransactionLocalRunnable(Transaction tx)
      {
         super(tx);
      }

      public void doRun()
      {
         try
         {
            local.lock(tx);
            try
            {
               local.set(this);
               local.get();
        }
            finally
            {
               local.unlock(tx);
            }
         }
         catch (Throwable t)
         {
            failure = t;
         }
      }
   }
   
   public SimpleTransactionLocalStressTestCase(String name)
   {
      super(name);
   }
}
