1 22 package org.jboss.test.jca.test; 23 24 import javax.management.Attribute ; 25 import javax.management.ObjectName ; 26 27 import junit.framework.Test; 28 29 import org.jboss.logging.Logger; 30 import org.jboss.test.JBossTestCase; 31 import org.jboss.test.jca.bank.interfaces.Account; 32 import org.jboss.test.jca.bank.interfaces.Teller; 33 import org.jboss.test.jca.bank.interfaces.TellerHome; 34 35 44 45 public class CachedConnectionBankStressTestCase extends JBossTestCase 46 { 47 48 private TellerHome th; 49 private Teller t; 50 51 private Exception exc; 52 53 private int iter; 54 55 public CachedConnectionBankStressTestCase (String name) 56 { 57 super(name); 58 } 59 60 protected void setUp() throws Exception 61 { 62 ObjectName CCM = new ObjectName ("jboss.jca:service=CachedConnectionManager"); 63 getServer().setAttribute(CCM, new Attribute ("SpecCompliant", Boolean.TRUE)); 64 th = (TellerHome)getInitialContext().lookup("Teller"); 65 t = th.create(); 66 t.setUp(); 67 } 68 69 protected void tearDown() throws Exception 70 { 71 if (t != null) 72 { 73 t.tearDown(); 74 } ObjectName CCM = new ObjectName ("jboss.jca:service=CachedConnectionManager"); 76 getServer().setAttribute(CCM, new Attribute ("SpecCompliant", Boolean.FALSE)); 77 78 } 79 80 public static Test suite() throws Exception 81 { 82 return getDeploySetup(CachedConnectionBankStressTestCase.class, "jcabanktest.jar"); 83 } 84 85 public void testCachedConnectionBank() throws Exception 86 { 87 Account[] accounts = new Account[getThreadCount()]; 88 for (int i = 0; i < getThreadCount(); i++) 89 { 90 accounts[i] = t.createAccount(new Integer (i)); 91 } final Object lock = new Object (); 93 94 iter = 0; 95 getLog().info("Start test. "+getThreadCount()+ " threads, "+getIterationCount()+" iterations"); 96 long start = System.currentTimeMillis(); 97 98 for (int i = 0; i < getThreadCount() - 1; i++) 99 { 100 new Thread (new TransferThread(accounts[i], 102 accounts[(i + 1) % getThreadCount()], 103 getIterationCount(), 104 lock)).start(); 105 synchronized (lock) 106 { 107 iter++; 108 } 109 } 110 111 synchronized(lock) 112 { 113 while(iter > 0) 114 { 115 lock.wait(); 116 } 117 } 118 119 if (exc != null) throw exc; 120 121 for (int i = 1; i < getThreadCount() - 1; i++) 122 { 123 assertTrue("nonzero final balance for" + i, accounts[i].getBalance() == 0); 124 } 126 127 long end = System.currentTimeMillis(); 128 129 getLog().info("Time:"+(end-start)); 130 getLog().info("Avg. time/call(ms):"+((end-start)/(getThreadCount()*getIterationCount()))); 131 } 132 133 134 135 136 public class TransferThread implements Runnable 137 { 138 Logger log = Logger.getLogger(getClass().getName()); 139 Account to; 140 Account from; 141 int iterationCount; 142 Object lock; 143 144 public TransferThread(final Account to, 145 final Account from, 146 final int iterationCount, 147 final Object lock) throws Exception 148 { 149 this.to = to; 150 this.from = from; 151 this.iterationCount = iterationCount; 152 this.lock = lock; 153 } 154 155 public void run() 156 { 157 try 158 { 159 160 for (int j = 0; j < iterationCount; j++) 161 { 162 if (exc != null) break; 163 164 t.transfer(from,to, 1); 165 } 166 } catch (Exception e) 167 { 168 exc = e; 169 } 170 171 synchronized(lock) 172 { 173 iter--; 174 log.info("Only "+iter+" left"); 175 lock.notifyAll(); 176 } 177 } 178 } 179 } | Popular Tags |