* should never read anything else than the original value (uncommitted values should be
* isolated)
*/
public void testReadCommitted() throws Exception
{
final Latch readerCanRead = new Latch();
final Latch readerDone = new Latch();
final Latch writerCanWrite = new Latch();
final Latch writerCanRollback = new Latch();
final Latch writerDone = new Latch();
cache.put(FQN, KEY, VALUE);
assertEquals(VALUE, cache.get(FQN, KEY));
// start a reader thread; need a transaction so its initial
// read lock is maintained while the writer does its work
Thread readerThread = new Thread(new Runnable()
{
public void run()
{
Transaction tx = null;
try
{
tx = startTransaction();
// Read the value, thus acquiring a read lock on FQN
assertEquals(VALUE, cache.get(FQN, KEY));
writerCanWrite.release();
// wait until the writer thread changes the value in a transaction, but it did not
// yet commit or roll back.
readerCanRead.acquire();
try
{
// I shouldn't be able to see the "dirty" value
assertEquals("thread w/ read lock " +
"can see subsequent uncommitted changes",
VALUE, cache.get(FQN, KEY));
}
catch (TimeoutException good)
{
// this is what should happen due to writer's WL
}
// let the writer know it can rollback
writerCanRollback.release();
// I should still be able to see the "clean" value
assertEquals(VALUE, cache.get(FQN, KEY));
}
catch (AssertionFailedError e)
{
readerError = e;
}
catch(Throwable t)
{
t.printStackTrace();
readerFailed = true;
}
finally
{
System.out.println("reader thread exits");
if (tx != null)
try { tx.commit(); } catch (Exception e) {}
writerCanWrite.release();
writerCanRollback.release();
readerDone.release();
}
}
}, "READER");
readerThread.start();
// start a writer thread and a transaction
Thread writerThread = new Thread(new Runnable()
{
public void run()
{
try
{
// wait until the reader thread reads and allows me to start
writerCanWrite.attempt(3000);
Transaction tx2 = startTransaction();
// change VALUE in a transaction
cache.put(FQN, KEY, "this-shouldnt-be-visible");
// notify the reading thread
readerCanRead.release();
// wait until the reader thread reads and allows me to rollback or until I timeout
writerCanRollback.attempt(3000);
System.out.println("rolling back");
tx2.rollback();
}
catch (AssertionFailedError e)
{
writerError = e;
}
catch(Throwable t)
{
t.printStackTrace();
writerFailed = true;
}
finally
{
System.out.println("writer thread exits");
readerCanRead.release();
writerDone.release();
}
}
}, "WRITER");
writerThread.start();
// wait for both threads to finish
readerDone.acquire();
writerDone.acquire();
// If any assertion failed, throw on the AssertionFailedError
if (readerError != null)
{
throw readerError;