Author: pmouawad Date: Thu Aug 3 21:11:20 2017 New Revision: 1804046 URL: http://svn.apache.org/viewvc?rev=1804046&view=rev Log: Bug 61321 - HTTP Cache Manager : with cache enabled & Retrieving embedded resources, JMeter fetches resource from server rather than cache at each alternate iteration Add test case Bugzilla Id: 61321
Modified: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerBase.java jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerHC4.java Modified: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerBase.java URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerBase.java?rev=1804046&r1=1804045&r2=1804046&view=diff ============================================================================== --- jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerBase.java (original) +++ jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerBase.java Thu Aug 3 21:11:20 2017 @@ -86,7 +86,7 @@ public abstract class TestCacheManagerBa protected abstract void setRequestHeaders(); - private void sleepTill(long deadline) { + protected void sleepTill(long deadline) { while (System.currentTimeMillis() < deadline) { try { Thread.sleep(100); @@ -344,7 +344,7 @@ public abstract class TestCacheManagerBa assertNotNull("Should find entry", getThreadCacheEntry(LOCAL_HOST)); assertFalse("Should not find valid entry", this.cacheManager.inCache(url)); } - + @Test public void testGetClearEachIteration() throws Exception { assertFalse("Should default not to clear after each iteration.", this.cacheManager.getClearEachIteration()); @@ -406,7 +406,7 @@ public abstract class TestCacheManagerBa assertTrue("ThreadCache should be emptied by call to clear.", getThreadCache().isEmpty()); } - private HTTPSampleResult getSampleResultWithSpecifiedResponseCode(String code) { + protected HTTPSampleResult getSampleResultWithSpecifiedResponseCode(String code) { HTTPSampleResult sampleResult = new HTTPSampleResult(); sampleResult.setResponseCode(code); sampleResult.setHTTPMethod("GET"); @@ -422,7 +422,7 @@ public abstract class TestCacheManagerBa return threadLocal.get(); } - private CacheManager.CacheEntry getThreadCacheEntry(String url) throws Exception { + protected CacheManager.CacheEntry getThreadCacheEntry(String url) throws Exception { return getThreadCache().get(url); } Modified: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerHC4.java URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerHC4.java?rev=1804046&r1=1804045&r2=1804046&view=diff ============================================================================== --- jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerHC4.java (original) +++ jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestCacheManagerHC4.java Thu Aug 3 21:11:20 2017 @@ -18,8 +18,16 @@ package org.apache.jmeter.protocol.http.control; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + import java.net.URISyntaxException; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Locale; @@ -34,8 +42,9 @@ import org.apache.http.message.AbstractH import org.apache.http.message.BasicHeader; import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; import org.apache.jmeter.protocol.http.util.HTTPConstants; - -import static org.junit.Assert.assertEquals; +import org.apache.jmeter.protocol.http.util.HTTPConstantsInterface; +import org.junit.Assert; +import org.junit.Test; /** * Test {@link CacheManager} that uses HTTPHC4Impl @@ -243,5 +252,52 @@ public class TestCacheManagerHC4 extends assertEquals("Wrong name in header for " + requestHeader, requestHeader, header.getName()); assertEquals("Wrong value for header " + header, expectedValue, header.getValue()); } + + @Test + public void testBug61321() throws Exception { + this.cacheManager.setUseExpires(false); + this.cacheManager.testIterationStart(null); + assertNull("Should not find entry", getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry", this.cacheManager.inCache(url)); + cacheResult(sampleResultOK); + assertNotNull("Should find entry", getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should find valid entry", this.cacheManager.inCache(url)); + cacheManager.setHeaders(url, httpMethod); + checkIfModifiedSinceHeader(httpMethod); + + this.httpMethod = new HttpPostStub(); + sampleResultOK = getSampleResultWithSpecifiedResponseCode("304"); + setLastModified(null); + cacheResult(sampleResultOK); + assertNotNull("Should find entry", getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry", this.cacheManager.inCache(url)); + cacheManager.setHeaders(url, httpMethod); + checkIfModifiedSinceHeader(httpMethod); + + this.httpMethod = new HttpPostStub(); + sampleResultOK = getSampleResultWithSpecifiedResponseCode("304"); + setLastModified(null); + cacheResult(sampleResultOK); + assertNotNull("Should find entry", getThreadCacheEntry(LOCAL_HOST)); + assertFalse("Should not find valid entry", this.cacheManager.inCache(url)); + cacheManager.setHeaders(url, httpMethod); + checkIfModifiedSinceHeader(httpMethod); + } + + /** + * + */ + protected void checkIfModifiedSinceHeader(HttpRequestBase httpMethod) { + SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); + try { + assertEquals("Should have found 1 header "+HTTPConstantsInterface.IF_MODIFIED_SINCE, + 1, + httpMethod.getHeaders(HTTPConstantsInterface.IF_MODIFIED_SINCE).length); + Date date = dateFormat.parse(httpMethod.getHeaders(HTTPConstantsInterface.IF_MODIFIED_SINCE)[0].getValue()); + assertNotNull("Should have found a valid entry", date); + } catch(ParseException e) { + Assert.fail("Invalid header format for:"+ HTTPConstantsInterface.IF_MODIFIED_SINCE); + } + } }