User: user57  
  Date: 01/07/10 22:52:08

  Modified:    src/main/org/jboss/test/lock/test Main.java
  Added:       src/main/org/jboss/test/lock/test EnterpriseEntityTest.java
  Log:
   o updated lock test to excersise commit-option a, b (c & d but those are
     not enabled at the moment).
   o abstracted the actuall test outside of Worker, into Task so it is easier
     to add new lock tests
   o the test will also do a single bean, bean-to-bean
  
  Revision  Changes    Path
  1.6       +38 -164   jbosstest/src/main/org/jboss/test/lock/test/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /cvsroot/jboss/jbosstest/src/main/org/jboss/test/lock/test/Main.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Main.java 2001/07/10 02:34:04     1.5
  +++ Main.java 2001/07/11 05:52:08     1.6
  @@ -1,142 +1,18 @@
   package org.jboss.test.lock.test;
   
  -import java.rmi.*;
  -
  -import java.util.HashMap;
  -import java.util.StringTokenizer;
  -
  -import javax.naming.Context;
  -import javax.naming.InitialContext;
  -
  -import javax.ejb.FinderException;
  -
  -import org.jboss.test.lock.interfaces.EnterpriseEntityHome;
  -import org.jboss.test.lock.interfaces.EnterpriseEntity;
  -
   import junit.framework.Test;
   import junit.framework.TestCase;
   import junit.framework.TestSuite;
   
   import org.jboss.test.util.Deploy;
   
  -public class Main extends
  -   TestCase
  +public class Main 
  +   extends TestCase
   {
  -     
  -   boolean deployed = false;
  -   int nbThreads;
  -   int iterations;
  -   boolean b2b;
  -   boolean nodeploy;
  -   
  -   HashMap param = new HashMap();
  -   EnterpriseEntity entity;
  -     
  -   public static void main(String arg[]) throws Exception {
  -      Main main = new Main(arg);
  -      main.testLock();
  +   public Main(String name) {
  +      super(name);
      }
  -
  -   public Main(String arg[]) {
  -      super("main");
  -      // scanning parameters
  -      int i = 0;
  -      while (i < arg.length) {
  -         StringTokenizer st = new StringTokenizer (arg[i++], "=");
  -         param.put (st.nextToken (), st.hasMoreTokens () ? st.nextToken () : "");
  -      }
  -      if (param.containsKey("help") || param.containsKey("-help")) {
  -         System.out.println("Valid Parameters:");
  -         System.out.println("  threads=<t>     run <t> threads concurrently 
(default 2)");
  -         System.out.println("  iterations=<n>  each thread will perform <n> calls 
(default 10)");
  -         System.out.println("  b2b             use bean to bean calls (default: 
single bean call)");
  -         System.out.println();
  -         System.exit(0);
  -      }
  -   }
  -
  -    public Main(String name)
  -    {
  -        super(name);
  -    }
  -
  -    protected void setUp() throws Exception
  -    {
  -        init();       // get params
  -        if( nodeploy == false )
  -            deploy();     // deploy the bean
  -    }
  -
  -    public void testLock() throws Exception
  -    {                
  -        Context ctx = new InitialContext();
  -        EnterpriseEntityHome home = 
(EnterpriseEntityHome)ctx.lookup("EnterpriseEntity");
  -                     
  -        try {
  -            entity = home.findByPrimaryKey("seb");
  -        } catch (FinderException e) {
  -            entity = home.create("seb");
  -        }
  -                     
  -        if (b2b) entity.setNextEntity("daniel");
  -                     
  -        Worker[] threads = new Worker[nbThreads];
  -                     
  -        System.out.println("Spawning " + nbThreads + " threads for " + 
  -                iterations + " iterations" + 
  -                (b2b ? " with bean to bean call" : " with single bean call")); 
  -                     
  -        for (int i=0; i<nbThreads; i++) {
  -            threads[i] = new Worker("thread no " + (i+1));
  -            threads[i].start();
  -        }
  -                     
  -        for (int i=0; i<nbThreads; i++) threads[i].join();   
  -    }
  -
  -     
  -     private void init() {
  -             nbThreads = 2;
  -             try {
  -                     nbThreads = Integer.parseInt((String)param.get("threads"));
  -             } catch (Exception e) {
  -                     // ignore
  -             }
  -     
  -             iterations = 10;
  -             try {
  -                     iterations = Integer.parseInt((String)param.get("iterations"));
  -             } catch (Exception e) {
  -                     // ignore
  -             }
  -             
  -             b2b = param.containsKey("b2b");
  -             nodeploy = param.containsKey("nodeploy");
  -             
  -     }
  -     
  -     
  -     protected void deploy() throws Exception {
  -        /*
  -             if (deployed) return;
  -                     
  -             System.out.println("_____________________________________________");
  -             System.out.println();
  -             System.out.println("jBoss, the EJB Open Source Server");
  -             System.out.println("Copyright (C), The jBoss Organization, 2000");
  -             System.out.println("_____________________________________________");
  -             System.out.println();
  -             System.out.println("Welcome to the lock test v0.1");
  -             System.out.println("_____________________________________________");
  -             System.out.println();
  -             System.out.print("Deploying the bean...");
  -             System.out.flush();
  -             new org.jboss.jmx.client.Deployer().deploy("../deploy/locktest.jar");
  -             deployed = true;
  -             System.out.println("done!");
  -             */
  -     }
  -     
  +   
      /**
       * Setup the test suite.
       */
  @@ -147,48 +23,46 @@
         String filename = "locktest.jar";
         suite.addTest(new Deploy.Deployer(filename));
   
  -      suite.addTest(new TestSuite(Main.class));
  +      suite.addTest(new TestSuite(Entity_Option_A_Test.class));
  +      suite.addTest(new TestSuite(Entity_Option_B_Test.class));
  +      // suite.addTest(new TestSuite(Entity_Option_C_Test.class));
  +      // suite.addTest(new TestSuite(Entity_Option_D_Test.class));            
   
         // add a test case to undeploy our support applications
         suite.addTest(new Deploy.Undeployer(filename));
         
         return suite;
      }
  -     
  -   public class Worker extends Thread {
  -      public String name;
  -      public boolean running;
  -      
  -      public Worker(String name) {
  -         this.name = name;
  -         running = true;
  +
  +   public static class Entity_Option_A_Test
  +      extends EnterpriseEntityTest
  +   {
  +      public Entity_Option_A_Test(String name) {
  +         super(name, "EnterpriseEntity_A");
         }
  -      
  -      public void run() {
  -         long start = System.currentTimeMillis();
  -         int i;
  -         
  -         for (i=0; i<iterations; i++) {
  -            if (!running) break;
  -            
  -            try { 
  -               if (b2b) {
  -                  entity.setAndCopyField(name+" i="+i);
  -                  
  -               } else {
  -                  entity.setField(name+" i="+i);
  -                  
  -               }
  -               System.out.println(name + " " + (i+1) + " iterations done");
  -            } catch (Exception e) {
  -               System.out.println(name + " caught an exception, dying");
  -               e.printStackTrace();
  -               running = false;
  -            }
  -         }
  -         
  -         long end = System.currentTimeMillis();
  -         System.out.println(name + ": did " + i + " iterations in " + (end-start) + 
"ms");
  +   }
  +
  +   public static class Entity_Option_B_Test
  +      extends EnterpriseEntityTest
  +   {
  +      public Entity_Option_B_Test(String name) {
  +         super(name, "EnterpriseEntity_B");
  +      }
  +   }
  +
  +   public static class Entity_Option_C_Test
  +      extends EnterpriseEntityTest
  +   {
  +      public Entity_Option_C_Test(String name) {
  +         super(name, "EnterpriseEntity_C");
  +      }
  +   }
  +
  +   public static class Entity_Option_D_Test
  +      extends EnterpriseEntityTest
  +   {
  +      public Entity_Option_D_Test(String name) {
  +         super(name, "EnterpriseEntity_D");
         }
      }
   }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/lock/test/EnterpriseEntityTest.java
  
  Index: EnterpriseEntityTest.java
  ===================================================================
  package org.jboss.test.lock.test;
  
  import java.rmi.*;
  
  import java.util.HashMap;
  import java.util.StringTokenizer;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  
  import javax.ejb.FinderException;
  
  import org.jboss.test.lock.interfaces.EnterpriseEntityHome;
  import org.jboss.test.lock.interfaces.EnterpriseEntity;
  
  import junit.framework.Test;
  import junit.framework.Assert;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  
  import org.jboss.test.util.Deploy;
  
  public abstract class EnterpriseEntityTest
     extends TestCase
  {
     public static final int DEFAULT_THREAD_COUNT = 10;
     public static final int DEFAULT_ITERATIONS = 20;
  
     private String jndiname;
     
     /** The number of threads to test with. */
     private int nbThreads;
  
     /** The number of iterations each thread will go through */
     private int iterations;
  
     private Worker[] threads;
     
     private HashMap param = new HashMap();
     
     private EnterpriseEntity entity;
  
     private boolean failed;
     
     public EnterpriseEntityTest(final String name,
                                 final String jndiname)
     {
        super(name);
        this.jndiname = jndiname;
     }
  
     protected void setUp() throws Exception
     {
        nbThreads = DEFAULT_THREAD_COUNT;
        iterations = DEFAULT_ITERATIONS;
  
        // get a handle on an entity bean
        Context ctx = new InitialContext();
        try {
           EnterpriseEntityHome home =
              (EnterpriseEntityHome)ctx.lookup(jndiname);
                        
           try {
              entity = home.findByPrimaryKey("seb");
           } catch (FinderException e) {
              entity = home.create("seb");
           }
        }
        finally {
           ctx.close();
        }
  
        // setup the threads
        threads = new Worker[nbThreads];
     }
  
     protected void tearDown() throws Exception {
        // ???
     }
  
     protected void startAll(Task prototype) throws Exception {
        for (int i=0; i<nbThreads; i++) {
           Task task = (Task)prototype.clone();
           threads[i] = new Worker("Thread #" + (i+1), task);
           threads[i].start();
        }
     }
  
     protected void joinAll() throws Exception {
        // wait for all the threads to finish
        for (int i=0; i<nbThreads; i++) {
           threads[i].join();
        }
     }
  
     protected void run(Task prototype) throws Exception {
        startAll(prototype);
        joinAll();
        Assert.assert(!failed);
     }
     
     protected synchronized void setFailed() {
        failed = true;
     }
  
     protected boolean hasFailed() {
        return failed;
     }
     
     public void testSingleBean() throws Exception
     {          
        System.out.println("Spawning " + nbThreads + " threads for " + 
                           iterations + " iterations with single bean call");
  
        Task prototype = new Task() {
              public void run(String name, int i) throws Exception {
                 entity.setField(name+" i="+i);
              }
           };
  
        run(prototype);
     }
  
     public void testB2B() throws Exception
     {          
        System.out.println("Spawning " + nbThreads + " threads for " + 
                           iterations + " iterations with bean to bean call");
  
        entity.setNextEntity("daniel");
                        
        Task prototype = new Task() {      
              public void run(String name, int i) throws Exception {
                 entity.setAndCopyField(name+" i="+i);
              }
           };
  
        run(prototype);      
     }
     
     
     /////////////////////////////////////////////////////////////////////////
     //                        Iteration Worker & Task                      //
     /////////////////////////////////////////////////////////////////////////
     
     public abstract class Task
        implements Cloneable
     {
        public abstract void run(String name, int i) throws Exception;
  
        public Object clone() {
           try {
              return super.clone();
           }
           catch (CloneNotSupportedException e) {
              throw new InternalError();
           }
        }
     }
     
     public class Worker
        extends Thread
     {
        public String name;
        public boolean running;
        public Task task;
        
        public Worker(final String name, final Task task) {
           this.name = name;
           this.task = task;
           running = true;
        }
        
        public void run() {
           long start = System.currentTimeMillis();
           int i;
           
           for (i=0; i<iterations; i++) {
              if (!running || hasFailed()) break;
              
              try {
                 task.run(name, i); 
                 // System.out.println(name + " " + (i+1) + " iterations done");
              } catch (Throwable t) {
                 System.out.println(name + " caught an exception, dying");
                 t.printStackTrace();
                 running = false;
                 setFailed();
              }
           }
           
           long end = System.currentTimeMillis();
           System.out.println(name + ": did " + i +
                              " iterations in " + (end-start) + "ms");
        }
     }
  }
  
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to