View Javadoc

1   /*
2    * $Id: RollbackTestCase.java 22855 2011-09-04 17:45:44Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.transport.vm.functional.transactions;
11  
12  
13  
14  import org.mule.module.client.MuleClient;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  import org.mule.util.concurrent.Latch;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Random;
21  import java.util.concurrent.atomic.AtomicInteger;
22  
23  import org.junit.Test;
24  import org.junit.Assert;
25  
26  public class RollbackTestCase extends FunctionalTestCase
27  {
28      @Override
29      protected String getConfigResources()
30      {
31          return "org/mule/test/config/rollback-config.xml";
32      }
33  
34      static Latch latch;
35      static AtomicInteger totalSeen;
36      static AtomicInteger totalAccepted;
37  
38      @Test
39      public void testRollback() throws Exception
40      {
41          totalSeen = new AtomicInteger(0);
42          totalAccepted = new AtomicInteger(0);
43          latch = new Latch();
44          MuleClient client = new MuleClient(muleContext);
45          Map props = new HashMap();
46          for (int i = 0; i < 100; i++)
47          {
48              client.dispatch("vm://async", "Hello " + i, props);
49          }
50          latch.await();
51          Assert.assertEquals(100, totalAccepted.get());
52          Assert.assertTrue(totalSeen.get() > 100);
53      }
54  
55      public static class AggregatingComponent
56      {
57          private Random r = new Random();
58  
59          public void process(String s)
60          {
61              totalSeen.incrementAndGet();
62              int r = this.r.nextInt(10);
63              if (r > 8)
64              {
65                  // Fail and roll the tx back 10% of them
66                  throw new RuntimeException();
67              }
68              totalAccepted.incrementAndGet();
69              if (totalAccepted.get() == 100)
70              {
71                  latch.countDown();
72              }
73          }
74      }
75  }