KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > transaction > AbortionTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.transaction;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.interceptors.OrderedSynchronizationHandler;
12 import org.jboss.cache.misc.TestingUtil;
13 import org.jgroups.JChannel;
14 import org.jgroups.blocks.RpcDispatcher;
15
16 import javax.transaction.RollbackException JavaDoc;
17 import javax.transaction.Synchronization JavaDoc;
18 import javax.transaction.SystemException JavaDoc;
19 import javax.transaction.Transaction JavaDoc;
20 import javax.transaction.TransactionManager JavaDoc;
21
22 /**
23  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
24  */

25 public class AbortionTest extends TestCase
26 {
27    private MyTC cache1, cache2, cache3;
28
29    protected void setUp() throws Exception JavaDoc
30    {
31       super.setUp();
32       System.out.println("********* START: SET UP *************");
33       cache1 = initCache(false);
34       TestingUtil.sleepThread(1500); // to ensure cache1 is the coordinator
35
cache2 = initCache(false);
36       cache3 = initCache(true);
37       System.out.println("********* END: SET UP *************");
38    }
39
40
41    protected void tearDown() throws Exception JavaDoc
42    {
43       System.out.println("********* START: TEAR DOWN *************");
44       destroyCache(cache3);
45       destroyCache(cache2);
46       destroyCache(cache1);
47       cache1 = null;
48       cache2 = null;
49       cache3 = null;
50       super.tearDown();
51       System.out.println("********* END: TEAR DOWN *************");
52    }
53
54    private MyTC initCache(boolean notifying) throws Exception JavaDoc
55    {
56       MyTC c = new MyTC();
57       c.getConfiguration().setCacheMode("REPL_SYNC");
58       c.getConfiguration().setClusterConfig(getJGroupsStack());
59       c.getConfiguration().setFetchInMemoryState(false);
60       if (!notifying)
61       {
62          c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
63       }
64       else
65       {
66          c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.NotifyingTransactionManager");
67       }
68       c.start();
69       return c;
70    }
71
72    // we need a 'special' stack that does not attempt redelivery since we kill a channel midway during a tx in this test.
73
private String JavaDoc getJGroupsStack()
74    {
75 // return "UDP(mcast_addr=224.0.0.36;mcast_port=55566;ip_ttl=32;" +
76
// "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
77
// "PING(timeout=10;num_initial_members=1):" +
78
// "pbcast.NAKACK(gc_lag=50;max_xmit_size=8192;retransmit_timeout=10):" +
79
// "UNICAST(timeout=600):" +
80
// "FRAG(frag_size=8192;down_thread=false;up_thread=false):" +
81
// "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
82
// "shun=false;print_local_addr=true):" +
83
// "pbcast.STATE_TRANSFER";
84
return JChannel.DEFAULT_PROTOCOL_STACK;
85    }
86
87    private void destroyCache(MyTC c)
88    {
89       if (c != null)
90       {
91          c.stop();
92          c.destroy();
93       }
94    }
95
96    public void testSyncCaches() throws Exception JavaDoc
97    {
98       performTest(false, false);
99    }
100
101    public void testSyncCachesSyncCommitRollback() throws Exception JavaDoc
102    {
103       performTest(true, false);
104    }
105
106    /**
107     * Note that this tests a *remote* beforeCompletion abort - which is a part of the calling instance's afterCompletion.
108     *
109     * @throws Exception
110     */

111    public void testAbortBeforeCompletion() throws Exception JavaDoc
112    {
113       performTest(true, true);
114    }
115
116    private void performTest(boolean syncCommitRollback, boolean abortBeforeCompletion) throws Exception JavaDoc
117    {
118       cache1.getConfiguration().setSyncCommitPhase(syncCommitRollback);
119       cache1.getConfiguration().setSyncRollbackPhase(syncCommitRollback);
120       cache2.getConfiguration().setSyncCommitPhase(syncCommitRollback);
121       cache2.getConfiguration().setSyncRollbackPhase(syncCommitRollback);
122       cache3.getConfiguration().setSyncCommitPhase(syncCommitRollback);
123       cache3.getConfiguration().setSyncRollbackPhase(syncCommitRollback);
124
125       TransactionManager JavaDoc mgr1 = cache1.getTransactionManager();
126       TransactionManager JavaDoc mgr2 = cache2.getTransactionManager();
127       NotifyingTransactionManager mgr3 = (NotifyingTransactionManager) cache3.getTransactionManager();
128
129       assertSame(mgr1, mgr2);
130       assertNotSame(mgr1, mgr3);
131       assertNotSame(mgr2, mgr3);
132
133       assertTrue(mgr1 instanceof DummyTransactionManager);
134       assertTrue(mgr2 instanceof DummyTransactionManager);
135       assertTrue(mgr3 instanceof NotifyingTransactionManager);
136
137
138       cache1.put("/test", "key", "value");
139
140       assertEquals("value", cache1.get("/test", "key"));
141       assertEquals("value", cache2.get("/test", "key"));
142       assertEquals("value", cache3.get("/test", "key"));
143
144       // replicates
145
final boolean fAbortBeforeCompletion = abortBeforeCompletion;
146       mgr3.notification = new NotifyingTransactionManager.Notification()
147       {
148          public void notify(Transaction JavaDoc tx) throws SystemException JavaDoc, RollbackException JavaDoc
149          {
150             final Transaction JavaDoc finalTx = tx;
151             System.out.println("Notify called.");
152             // add an aborting sync handler.
153
Synchronization JavaDoc abort = new Synchronization JavaDoc()
154             {
155
156                Transaction JavaDoc t = finalTx;
157
158                public void beforeCompletion()
159                {
160                   if (fAbortBeforeCompletion)
161                   {
162                      cache3.myChannel.close();
163                      System.out.println("Returning from abort.beforeCompletion");
164                      try
165                      {
166                         finalTx.setRollbackOnly();
167                      }
168                      catch (SystemException JavaDoc e)
169                      {
170                         throw new RuntimeException JavaDoc("Unable to set rollback", e);
171                      }
172                      throw new RuntimeException JavaDoc("Dummy exception");
173                   }
174                }
175
176                public void afterCompletion(int i)
177                {
178                   if (!fAbortBeforeCompletion)
179                   {
180                      cache3.myChannel.close();
181                      System.out.println("Returning from abort.afterCompletion");
182                      throw new RuntimeException JavaDoc("Dummy exception");
183                   }
184                }
185             };
186
187             OrderedSynchronizationHandler osh = OrderedSynchronizationHandler.getInstance(tx);
188             osh.registerAtHead(abort);
189             System.out.println("Added sync handler.");
190          }
191       };
192
193       mgr1.begin();
194       Transaction JavaDoc tx = mgr1.getTransaction();
195       cache1.put("/test", "key", "value2");
196       tx.commit();
197
198       TestingUtil.sleepThread(5000);
199
200       // only test cache1 and cache2. Assume cache3 has crashed out.
201
assertEquals(0, cache1.getNumberOfLocksHeld());
202       assertEquals(0, cache2.getNumberOfLocksHeld());
203       assertEquals("put in transaction should NOT have been rolled back", "value2", cache1.get("/test", "key"));
204       assertEquals("put in transaction should NOT have been rolled back", "value2", cache2.get("/test", "key"));
205
206    }
207
208
209    public static class MyTC extends CacheImpl
210    {
211       JChannel myChannel;
212       RpcDispatcher myDispatcher;
213
214       public MyTC() throws Exception JavaDoc
215       {
216          super();
217       }
218
219       public void start() throws Exception JavaDoc
220       {
221          super.start();
222          myChannel = channel;
223          myDispatcher = disp;
224       }
225    }
226
227 }
228
Popular Tags