1
2
3
4
5
6
7
8
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
66 throw new RuntimeException();
67 }
68 totalAccepted.incrementAndGet();
69 if (totalAccepted.get() == 100)
70 {
71 latch.countDown();
72 }
73 }
74 }
75 }