View Javadoc

1   package net.sf.ehcache.transaction.xa;
2   
3   import junit.framework.TestCase;
4   import net.sf.ehcache.transaction.xa.processor.XAThreadPool;
5   
6   import java.util.Map;
7   import java.util.concurrent.Callable;
8   import java.util.concurrent.ConcurrentHashMap;
9   import java.util.concurrent.atomic.AtomicInteger;
10  
11  /***
12   * @author lorban
13   */
14  public class XAThreadPoolTest extends TestCase {
15  
16      public void test() throws Exception {
17          final int COUNTER = 5000;
18          final int CONCURRENCY = 50;
19  
20          XAThreadPool xaThreadPool = new XAThreadPool();
21  
22          XAThreadPool.MultiRunner[] runners = new XAThreadPool.MultiRunner[CONCURRENCY];
23          for (int i = 0; i < CONCURRENCY; i++) {
24              runners[i] = xaThreadPool.getMultiRunner();
25          }
26  
27          final Map<String, AtomicInteger> results = new ConcurrentHashMap<String, AtomicInteger>();
28  
29          Callable myCallable = new Callable() {
30              public Object call() throws Exception {
31                  String threadName = Thread.currentThread().getName();
32  
33                  AtomicInteger counter = results.get(threadName);
34                  if (counter == null) {
35                      counter = new AtomicInteger();
36                      results.put(threadName, counter);
37                  }
38  
39                  counter.incrementAndGet();
40  
41                  return null;
42              }
43          };
44  
45          // execution
46          for (int i = 0; i < COUNTER; i++) {
47              for (int j = 0; j < CONCURRENCY; j++) {
48                  runners[j].execute(myCallable);
49              }
50          }
51  
52          // release
53          for (int j = 0; j < CONCURRENCY; j++) {
54              runners[j].release();
55  
56              try {
57                  runners[j].execute(myCallable);
58                  fail("expected IllegalStateException");
59              } catch (IllegalStateException e) {
60                  // expected
61              }
62          }
63  
64          // assertions
65          assertEquals(CONCURRENCY, results.size());
66          for (Map.Entry<String, AtomicInteger> entry : results.entrySet()) {
67              assertEquals(COUNTER, entry.getValue().get());
68          }
69      }
70  }