KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.transaction;
2
3 import EDU.oswego.cs.dl.util.concurrent.Latch;
4 import junit.framework.AssertionFailedError;
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jboss.cache.CacheImpl;
9 import org.jboss.cache.DummyTransactionManagerLookup;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.lock.IsolationLevel;
12 import org.jboss.cache.lock.TimeoutException;
13
14 import javax.transaction.NotSupportedException JavaDoc;
15 import javax.transaction.SystemException JavaDoc;
16 import javax.transaction.Transaction JavaDoc;
17
18 /**
19  * Tests READ_COMMITED isolation level.
20  *
21  * @author <a HREF="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
22  * @version $Id: IsolationLevelRepeatableReadTest.java,v 1.3 2006/12/30 17:49:53 msurtani Exp $
23  */

24
25 public class IsolationLevelRepeatableReadTest extends TestCase
26 {
27
28    private CacheImpl cache = null;
29    private final Fqn FQN = Fqn.fromString("/a");
30    private final String JavaDoc KEY = "key";
31    private final String JavaDoc VALUE = "value";
32
33    private volatile boolean writerFailed;
34    private volatile AssertionFailedError writerError;
35
36    protected void setUp() throws Exception JavaDoc
37    {
38       super.setUp();
39
40       writerFailed = false;
41       writerError = null;
42
43       cache = new CacheImpl();
44       cache.getConfiguration().setCacheMode("LOCAL");
45       cache.getConfiguration().setIsolationLevel(IsolationLevel.REPEATABLE_READ);
46       cache.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
47       cache.getConfiguration().setLockAcquisitionTimeout(1000);
48       cache.start();
49    }
50
51
52    protected void tearDown() throws Exception JavaDoc
53    {
54       super.tearDown();
55       cache.stop();
56    }
57
58    /**
59     * Test creates a cache node then starts a separate thread that removes
60     * the node inside a tx. Test confirms that the removal cannot be seen
61     * before the test commits.
62     *
63     * @throws Exception
64     */

65    public void testNodeRemoved() throws Exception JavaDoc
66    {
67       final Latch readerCanRead = new Latch();
68       final Latch readerDone = new Latch();
69       final Latch writerDone = new Latch();
70
71       cache.put(FQN, KEY, VALUE);
72       assertEquals(VALUE, cache.get(FQN, KEY));
73
74       // start a writer thread and a transaction
75

76       Thread JavaDoc writerThread = new Thread JavaDoc(new Runnable JavaDoc()
77       {
78          public void run()
79          {
80             try
81             {
82                Transaction JavaDoc tx = startTransaction();
83
84                // change VALUE in a transaction
85
cache.remove(FQN);
86
87                // notify the reading thread
88
readerCanRead.release();
89
90                readerDone.acquire();
91
92                tx.commit();
93             }
94             catch (AssertionFailedError e)
95             {
96                writerError = e;
97             }
98             catch (Throwable JavaDoc t)
99             {
100                t.printStackTrace();
101                writerFailed = true;
102             }
103             finally
104             {
105                System.out.println("writer thread exits");
106                readerCanRead.release();
107                writerDone.release();
108             }
109          }
110       }, "WRITER");
111       writerThread.start();
112
113       try
114       {
115          // wait until the writer thread changes the value in a transaction,
116
// but it did not yet commit or roll back.
117
readerCanRead.acquire();
118
119          // I shouldn't be able to see the "dirty" value
120
assertEquals("2nd thread cannot see uncommitted changes",
121                  VALUE, cache.get(FQN, KEY));
122       }
123       catch (TimeoutException t)
124       {
125          // ignore, this is good
126
}
127       finally
128       {
129          System.out.println("reader thread exits");
130          readerDone.release();
131       }
132
133       // wait for the writer to finish
134
writerDone.acquire();
135
136       assertNull("Node was not removed", cache.get(FQN));
137
138       // If any assertion failed, throw on the AssertionFailedError
139

140       if (writerError != null)
141       {
142          throw writerError;
143       }
144
145       if (writerFailed)
146       {
147          fail("The writer thread exited incorrectly. Watch the log for previous stack traces");
148       }
149
150    }
151
152    private Transaction JavaDoc startTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc
153    {
154       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
155       mgr.begin();
156       return mgr.getTransaction();
157    }
158
159    public static Test suite()
160    {
161
162       return new TestSuite(IsolationLevelRepeatableReadTest.class);
163
164    }
165
166 }
167
Popular Tags