--- jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java 2003/09/20 18:20:07 1.3 +++ jsr166/src/test/tck/AtomicReferenceFieldUpdaterTest.java 2003/09/25 11:02:41 1.4 @@ -17,14 +17,12 @@ public class AtomicReferenceFieldUpdater public static void main(String[] args){ junit.textui.TestRunner.run(suite()); } - - public static Test suite() { return new TestSuite(AtomicReferenceFieldUpdaterTest.class); } /** - * + * Contruction with non-existent field throws RuntimeException */ public void testConstructor(){ try{ @@ -38,7 +36,7 @@ public class AtomicReferenceFieldUpdater /** - * + * construction with field not of given type throws RuntimeException */ public void testConstructor2(){ try{ @@ -51,7 +49,7 @@ public class AtomicReferenceFieldUpdater } /** - * + * Constructor with non-volatile field throws exception */ public void testConstructor3(){ try{ @@ -64,7 +62,7 @@ public class AtomicReferenceFieldUpdater } /** - * + * get returns the last value set or assigned */ public void testGetSet(){ AtomicReferenceFieldUpdatera = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); @@ -77,7 +75,7 @@ public class AtomicReferenceFieldUpdater } /** - * + * compareAndSet succeeds in changing value if equal to expected else fails */ public void testCompareAndSet(){ AtomicReferenceFieldUpdatera = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); @@ -92,7 +90,32 @@ public class AtomicReferenceFieldUpdater } /** - * + * compareAndSet in one thread enables another waiting for value + * to succeed + */ + public void testCompareAndSetInMultipleThreads() { + x = one; + final AtomicReferenceFieldUpdatera = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); + + Thread t = new Thread(new Runnable() { + public void run() { + while(!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three)) Thread.yield(); + }}); + try { + t.start(); + assertTrue(a.compareAndSet(this, one, two)); + t.join(LONG_DELAY_MS); + assertFalse(t.isAlive()); + assertEquals(a.get(this), three); + } + catch(Exception e) { + unexpectedException(); + } + } + + /** + * repeated weakCompareAndSet succeeds in changing value when equal + * to expected */ public void testWeakCompareAndSet(){ AtomicReferenceFieldUpdatera = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x"); @@ -105,7 +128,7 @@ public class AtomicReferenceFieldUpdater } /** - * + * getAndSet returns previous value and sets to given value */ public void testGetAndSet(){ AtomicReferenceFieldUpdatera = AtomicReferenceFieldUpdater.newUpdater(getClass(), Integer.class, "x");