KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > memory > PessimisticMapWrapperTest


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/test/org/apache/commons/transaction/memory/PessimisticMapWrapperTest.java,v 1.3 2005/01/13 01:34:25 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.3 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.memory;
30
31 import junit.framework.*;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.logging.*;
36
37 import org.apache.commons.transaction.locking.LockException;
38 import org.apache.commons.transaction.util.Jdk14Logger;
39 import org.apache.commons.transaction.util.LoggerFacade;
40 import org.apache.commons.transaction.util.RendezvousBarrier;
41
42 /**
43  * Tests for map wrapper.
44  *
45  * @version $Revision$
46  */

47 public class PessimisticMapWrapperTest extends MapWrapperTest {
48
49     private static final Logger logger = Logger.getLogger(PessimisticMapWrapperTest.class.getName());
50     private static final LoggerFacade sLogger = new Jdk14Logger(logger);
51
52     protected static final long TIMEOUT = Long.MAX_VALUE;
53
54     private static int deadlockCnt = 0;
55
56     public static Test suite() {
57         TestSuite suite = new TestSuite(PessimisticMapWrapperTest.class);
58         return suite;
59     }
60
61     public static void main(java.lang.String JavaDoc[] args) {
62         junit.textui.TestRunner.run(suite());
63     }
64
65     public PessimisticMapWrapperTest(String JavaDoc testName) {
66         super(testName);
67     }
68
69     protected TransactionalMapWrapper getNewWrapper(Map JavaDoc map) {
70         return new PessimisticMapWrapper(map, sLogger);
71     }
72
73     // XXX no need for this code, just to make clear those tests are run as well
74
public void testBasic() throws Throwable JavaDoc {
75         super.testBasic();
76     }
77
78     public void testComplex() throws Throwable JavaDoc {
79         super.testComplex();
80     }
81
82     public void testSets() throws Throwable JavaDoc {
83         super.testSets();
84     }
85
86     public void testMulti() throws Throwable JavaDoc {
87         logger.info("Checking concurrent transaction features");
88
89         final Map JavaDoc map1 = new HashMap JavaDoc();
90
91         final PessimisticMapWrapper txMap1 = (PessimisticMapWrapper) getNewWrapper(map1);
92
93         final RendezvousBarrier beforeCommitBarrier =
94             new RendezvousBarrier("Before Commit", 2, BARRIER_TIMEOUT, sLogger);
95
96         final RendezvousBarrier afterCommitBarrier = new RendezvousBarrier("After Commit", 2, BARRIER_TIMEOUT, sLogger);
97
98         Thread JavaDoc thread1 = new Thread JavaDoc(new Runnable JavaDoc() {
99             public void run() {
100                 txMap1.startTransaction();
101                 txMap1.put("key1", "value2");
102                 synchronized (txMap1) {
103                     txMap1.commitTransaction();
104                     report("value2", (String JavaDoc) txMap1.get("key1"));
105                 }
106             }
107         }, "Thread1");
108
109         txMap1.put("key1", "value1");
110
111         txMap1.startTransaction();
112
113         report("value1", (String JavaDoc) txMap1.get("key1"));
114
115         thread1.start();
116
117         // we have serializable as isolation level, that's why I will still see the old value
118
report("value1", (String JavaDoc) txMap1.get("key1"));
119
120         txMap1.put("key1", "value3");
121
122         // after commit it must be our value
123
synchronized (txMap1) {
124             txMap1.commitTransaction();
125             report("value3", (String JavaDoc) txMap1.get("key1"));
126         }
127     }
128
129     public void testConflict() throws Throwable JavaDoc {
130         logger.info("Checking concurrent transaction features");
131
132         final Map JavaDoc map1 = new HashMap JavaDoc();
133
134         final PessimisticMapWrapper txMap1 = (PessimisticMapWrapper) getNewWrapper(map1);
135
136         final RendezvousBarrier restart = new RendezvousBarrier("restart",
137                 TIMEOUT, sLogger);
138
139         for (int i = 0; i < 25; i++) {
140
141             final RendezvousBarrier deadlockBarrier1 = new RendezvousBarrier("deadlock" + i,
142                     TIMEOUT, sLogger);
143
144             Thread JavaDoc thread1 = new Thread JavaDoc(new Runnable JavaDoc() {
145                 public void run() {
146                     txMap1.startTransaction();
147                     try {
148                         // first both threads get a lock, this one on res2
149
txMap1.put("key2", "value2");
150                         synchronized (deadlockBarrier1) {
151                             deadlockBarrier1.meet();
152                             deadlockBarrier1.reset();
153                         }
154                         // if I am first, the other thread will be dead, i.e.
155
// exactly one
156
txMap1.put("key1", "value2");
157                         txMap1.commitTransaction();
158                     } catch (LockException le) {
159                         assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
160                         deadlockCnt++;
161                         txMap1.rollbackTransaction();
162                     } catch (InterruptedException JavaDoc ie) {
163                     } finally {
164                         try {
165                         synchronized (restart) {
166                             restart.meet();
167                             restart.reset();
168                         }
169                         } catch (InterruptedException JavaDoc ie) {}
170
171                     }
172                 }
173             }, "Thread1");
174
175             thread1.start();
176
177             txMap1.startTransaction();
178             try {
179                 // first both threads get a lock, this one on res2
180
txMap1.get("key1");
181                 synchronized (deadlockBarrier1) {
182                     deadlockBarrier1.meet();
183                     deadlockBarrier1.reset();
184                 }
185                 // if I am first, the other thread will be dead, i.e. exactly
186
// one
187
txMap1.get("key2");
188                 txMap1.commitTransaction();
189             } catch (LockException le) {
190                 assertEquals(le.getCode(), LockException.CODE_DEADLOCK_VICTIM);
191                 deadlockCnt++;
192                 txMap1.rollbackTransaction();
193             } finally {
194                 try {
195                 synchronized (restart) {
196                     restart.meet();
197                     restart.reset();
198                 }
199                 } catch (InterruptedException JavaDoc ie) {}
200
201             }
202
203             // XXX in special scenarios the current implementation might cause both
204
// owners to be deadlock victims
205
if (deadlockCnt != 1) {
206                 sLogger.logWarning("More than one thread was deadlock victim!");
207             }
208             assertTrue(deadlockCnt >= 1);
209             deadlockCnt = 0;
210         }
211     }
212
213     public void testTxControl() throws Throwable JavaDoc {
214         super.testTxControl();
215     }
216
217 }
218
Popular Tags