[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/statetransfer ...

Bela Ban bela.ban at jboss.com
Sat Sep 9 07:48:31 EDT 2006


  User: bela    
  Date: 06/09/09 07:48:31

  Added:       tests/functional/org/jboss/cache/statetransfer 
                        StateTransferUnderLoadTest.java
  Log:
  ns
  
  Revision  Changes    Path
  1.1      date: 2006/09/09 11:48:31;  author: bela;  state: Exp;JBossCache/tests/functional/org/jboss/cache/statetransfer/StateTransferUnderLoadTest.java
  
  Index: StateTransferUnderLoadTest.java
  ===================================================================
  package org.jboss.cache.statetransfer;
  
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import org.jboss.cache.Cache;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.factories.DefaultCacheFactory;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jgroups.util.Util;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.transaction.SystemException;
  import javax.transaction.UserTransaction;
  import java.util.Properties;
  
  /**
   * Tests state transfer while the other node keeps sending transactional, synchronous method calls
   * @author Bela Ban
   * @version $Id: StateTransferUnderLoadTest.java,v 1.1 2006/09/09 11:48:31 bela Exp $
   */
  public class StateTransferUnderLoadTest extends TestCase {
     Cache cache1, cache2;
     Properties p=null;
     String old_factory=null;
     final String FACTORY="org.jboss.cache.transaction.DummyContextFactory";
  
  
     public StateTransferUnderLoadTest(String name) {
        super(name);
     }
  
     public void setUp() throws Exception {
        super.setUp();
        old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
        DummyTransactionManager.getInstance();
        if(p == null) {
           p=new Properties();
           p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
        }
     }
  
  
     public void tearDown() throws Exception {
        super.tearDown();
        if(cache2 != null) {
           cache2.stop();
           cache2=null;
        }
        if(cache1 != null) {
           cache1.stop();
           cache1=null;
        }
  
        // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
        DummyTransactionManager.destroy();
        if(old_factory != null) {
           System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
           old_factory=null;
        }
     }
  
  
     public void testStateTransferUnderLoad() throws Exception {
        Writer writer;
        Configuration cfg1, cfg2;
        cfg1=new Configuration();
        cfg2=new Configuration();
        cfg1.setCacheMode(Configuration.CacheMode.REPL_SYNC);
        cfg2.setCacheMode(Configuration.CacheMode.REPL_SYNC);
        cfg1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
        cfg2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
  
        cache1=new DefaultCacheFactory().createCache(cfg1, true);
        cache2=new DefaultCacheFactory().createCache(cfg2, false);
        UserTransaction tx1=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
        writer=new Writer(cache1, tx1);
        try {
           writer.start();
  
  
           cache2.create();
           for(int i=0; i < 10; i++) {
  
              cache2.start();
              // gets state
  
              Util.sleep(1000);
              cache2.stop();
           }
  
  
        }
        finally {
           if(writer != null)
              writer.stop();
        }
     }
  
  
     static class Writer implements Runnable {
        Thread  thread;
        Cache   cache;
        boolean running=false;
        UserTransaction tx;
  
  
        public Writer(Cache cache, UserTransaction tx) {
           this.cache=cache;
           this.tx=tx;
        }
  
        public void start() {
           thread=new Thread(this, "cache writer");
           running=true;
           thread.start();
        }
  
        public void stop() {
           running=false;
        }
  
        public void run() {
           Fqn fqn=Fqn.fromString("/a/b/c");
           while(running) {
              try {
                 tx.begin();
                 cache.put(fqn, "key", "value");
                 tx.commit();
              }
              catch(Exception e) {
                 e.printStackTrace();
                 try {tx.rollback();} catch(SystemException e1) {}
              }
              finally {
                 Util.sleepRandom(1000);
              }
           }
        }
  
     }
  
  
     private static void log(String msg) {
        System.out.println(Thread.currentThread().getName() + ": " + msg);
     }
  
  
  
     public static Test suite() throws Exception {
        return new TestSuite(StateTransferUnderLoadTest.class);
     }
  
     public static void main(String[] args) throws Exception {
        junit.textui.TestRunner.run(StateTransferUnderLoadTest.suite());
     }
  
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list