Author: musachy Date: Fri Aug 7 23:22:12 2009 New Revision: 802265 URL: http://svn.apache.org/viewvc?rev=802265&view=rev Log: add test for concurrent access to the cache
Modified: struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/jstl.jsp Modified: struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java?rev=802265&r1=802264&r2=802265&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java (original) +++ struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java Fri Aug 7 23:22:12 2009 @@ -36,6 +36,11 @@ import javax.servlet.Servlet; import java.util.HashMap; import java.util.Map; +import java.util.List; +import java.util.ArrayList; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.BrokenBarrierException; public class EmbeddedJSPResultTest extends TestCase { private HttpServletRequest request; @@ -96,14 +101,14 @@ } public void testJSTL() throws Exception { - result.setLocation("org/apache/struts2/jstl.jsp"); - result.execute(null); + result.setLocation("org/apache/struts2/jstl.jsp"); + result.execute(null); - assertEquals("XXXXXXXXXXX", cleanup(response.getContentAsString())); - } + assertEquals("XXXXXXXXXXXY", cleanup(response.getContentAsString())); + } - public void testCachedInstances() throws InterruptedException { + public void testCachedInstances() throws InterruptedException { ServletCache cache = new ServletCache(); Servlet servlet1 = cache.get("org/apache/struts2/simple0.jsp"); Servlet servlet2 = cache.get("org/apache/struts2/simple0.jsp"); @@ -111,6 +116,37 @@ assertSame(servlet1, servlet2); } + public void testCacheInstanceWithManyThreads() throws BrokenBarrierException, InterruptedException { + //start a bunch of thread at the same time using CyclicBarrier and hit the cache + //then wait for all the threads to end and check that they all got a reference to the same object + //and the cache size should be 1 + + DummyServletCache cache = new DummyServletCache(); + int numThreads = 70; + + CyclicBarrier startBarrier = new CyclicBarrier(numThreads + 1); + CyclicBarrier endBarrier = new CyclicBarrier(numThreads + 1); + + List<ServletGetRunnable> runnables = new ArrayList<ServletGetRunnable>(numThreads); + + //create the threads + for (int i = 0; i < numThreads; i++) { + ServletGetRunnable runnable = new ServletGetRunnable(cache, startBarrier, endBarrier, ActionContext.getContext()); + Thread thread = new Thread(runnable); + runnables.add(runnable); + thread.start(); + } + + startBarrier.await(); + endBarrier.await(); + Object servlet = cache.get("org/apache/struts2/simple0.jsp"); + assertEquals(1, cache.size()); + + for (ServletGetRunnable runnable : runnables) { + assertSame(servlet, runnable.getObject()); + } + } + private String cleanup(String str) { return str.replaceAll("\\s", ""); } @@ -178,3 +214,41 @@ class DummyConverter extends XWorkConverter { } + +class DummyServletCache extends ServletCache { + public int size() { + return cache.size(); + } +} + +class ServletGetRunnable implements Runnable { + private ServletCache servletCache; + private Object object; + private CyclicBarrier startBarrier; + private ActionContext actionContext; + private CyclicBarrier endBarrier; + + ServletGetRunnable(ServletCache servletCache, CyclicBarrier startBarrier, CyclicBarrier endBarrier, ActionContext actionContext) { + this.servletCache = servletCache; + this.startBarrier = startBarrier; + this.endBarrier = endBarrier; + this.actionContext = actionContext; + } + + @Override + public void run() { + ActionContext.setContext(actionContext); + //wait to start all therads at once..or try at least + try { + startBarrier.await(); + object = servletCache.get("org/apache/struts2/simple0.jsp"); + endBarrier.await(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public Object getObject() { + return object; + } +} Modified: struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/jstl.jsp URL: http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/jstl.jsp?rev=802265&r1=802264&r2=802265&view=diff ============================================================================== --- struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/jstl.jsp (original) +++ struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/jstl.jsp Fri Aug 7 23:22:12 2009 @@ -3,4 +3,7 @@ <c:set var="number" value="10"/> <c:forEach begin="0" end="${number}"> X -</c:forEach> \ No newline at end of file +</c:forEach> +<c:if test="${number < 15}"> +Y +</c:if> \ No newline at end of file