KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.transaction;
2
3
4 import EDU.oswego.cs.dl.util.concurrent.Latch;
5 import junit.framework.AssertionFailedError;
6 import junit.framework.Test;
7 import junit.framework.TestCase;
8 import junit.framework.TestSuite;
9 import org.jboss.cache.Cache;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.DummyTransactionManagerLookup;
12 import org.jboss.cache.Fqn;
13 import org.jboss.cache.config.Configuration;
14 import org.jboss.cache.config.Configuration.CacheMode;
15 import org.jboss.cache.factories.DefaultCacheFactory;
16 import org.jboss.cache.lock.IsolationLevel;
17 import org.jboss.cache.lock.TimeoutException;
18
19 import javax.transaction.NotSupportedException JavaDoc;
20 import javax.transaction.SystemException JavaDoc;
21 import javax.transaction.Transaction JavaDoc;
22
23
24 /**
25  * Tests READ_COMMITED isolation level.
26  *
27  * @author <a HREF="mailto:ovidiu@jboss.org">Ovidiu Feodorov</a>
28  * @version $Id: IsolationLevelSerializableTest.java,v 1.4 2007/01/02 18:26:05 msurtani Exp $
29  */

30
31 public class IsolationLevelSerializableTest extends TestCase
32 {
33
34    private Cache cache = null;
35    private final Fqn FQN = Fqn.fromString("/a");
36    private final String JavaDoc KEY = "key";
37    private final String JavaDoc VALUE = "value";
38
39    private volatile boolean writerFailed;
40    private volatile AssertionFailedError writerError;
41
42    protected void setUp() throws Exception JavaDoc
43    {
44       super.setUp();
45
46       writerFailed = false;
47
48       writerError = null;
49
50       Configuration config = new Configuration();
51       config.setCacheMode(CacheMode.LOCAL);
52       config.setIsolationLevel(IsolationLevel.SERIALIZABLE);
53       config.setLockAcquisitionTimeout(1000);
54       config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
55       cache = DefaultCacheFactory.getInstance().createCache(config);
56    }
57
58
59    protected void tearDown() throws Exception JavaDoc
60    {
61       super.tearDown();
62
63       cache.stop();
64       cache.destroy();
65       cache = null;
66    }
67
68    /**
69     * Test creates a cache node then starts a separate thread that removes
70     * the node inside a tx. Test confirms that the removal cannot be seen
71     * before the test commits.
72     *
73     * @throws Exception
74     */

75    public void testNodeRemoved() throws Exception JavaDoc
76    {
77       final Latch readerCanRead = new Latch();
78       final Latch readerDone = new Latch();
79       final Latch writerDone = new Latch();
80
81       cache.put(FQN, KEY, VALUE);
82       assertEquals(VALUE, cache.get(FQN, KEY));
83
84       // start a writer thread and a transaction
85

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

150       if (writerError != null)
151       {
152          throw writerError;
153       }
154
155       if (writerFailed)
156       {
157          fail("The writer thread exited incorrectly. Watch the log for previous stack traces");
158       }
159
160    }
161
162    private Transaction JavaDoc startTransaction() throws SystemException JavaDoc, NotSupportedException JavaDoc
163    {
164       DummyTransactionManager mgr = DummyTransactionManager.getInstance();
165       mgr.begin();
166       return mgr.getTransaction();
167    }
168
169    public static Test suite()
170    {
171
172       return new TestSuite(IsolationLevelSerializableTest.class);
173
174    }
175
176 }
177
178
Popular Tags