From: <iro...@us...> - 2009-01-26 22:39:05
|
Revision: 90 http://pojomatic.svn.sourceforge.net/pojomatic/?rev=90&view=rev Author: iroberts Date: 2009-01-26 22:02:07 +0000 (Mon, 26 Jan 2009) Log Message: ----------- modesty: increase coverage Modified Paths: -------------- trunk/Pojomatic/src/test/java/org/pojomatic/internal/SelfPopulatingMapTest.java Modified: trunk/Pojomatic/src/test/java/org/pojomatic/internal/SelfPopulatingMapTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/internal/SelfPopulatingMapTest.java 2009-01-20 00:34:08 UTC (rev 89) +++ trunk/Pojomatic/src/test/java/org/pojomatic/internal/SelfPopulatingMapTest.java 2009-01-26 22:02:07 UTC (rev 90) @@ -2,6 +2,8 @@ import static org.junit.Assert.*; +import java.util.concurrent.atomic.AtomicBoolean; + import org.junit.Test; public class SelfPopulatingMapTest { @@ -40,4 +42,43 @@ } assertSame(results[0], results[1]); } + + @Test + public void testBadConstructionFirstTime() throws Exception { + final AtomicBoolean firstTime = new AtomicBoolean(false); + final SelfPopulatingMap<String, String> selfPopulatingMap = + new SelfPopulatingMap<String, String>() { + @Override protected String create(String key) { + try { + Thread.sleep(10); // ensure that two threads have time to collide. + } + catch (InterruptedException e) {} + if (firstTime.getAndSet(true)) { + return new String(key); + } + else { + throw new RuntimeException("failing on first attempt"); + } + } + }; + int numThreads = 2; + Thread[] threads = new Thread[numThreads]; + final String[] results = new String[numThreads]; + for (int i = 0; i < threads.length; i++) { + final int threadNumber = i; + threads[i] = new Thread() { + @Override public void run() { + results[threadNumber] = selfPopulatingMap.get("x"); + } + }; + } + for (Thread t: threads) { + t.start(); + } + for (Thread t: threads) { + t.join(); + } + assertNull(results[0]); + assertEquals("x", results[1]); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |