Skip to content

Commit

Permalink
Merge pull request #4679 from jaikiran/qk-4627
Browse files Browse the repository at this point in the history
Testcase to verify that the StaticHandler can handle requests with If-Modified-Since header without running into thread safety issues
  • Loading branch information
gsmet authored Oct 29, 2019
2 parents 0bd8185 + ac86a9f commit f90efa8
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
import static org.hamcrest.CoreMatchers.containsString;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
Expand Down Expand Up @@ -39,4 +51,46 @@ public void test() {
.statusCode(200)
.body(containsString("Lorem"));
}

/**
* Tests that multiple simultaneous requests to a static resource, with each request
* using the {@code If-Modified-Since} header, doesn't cause server side errors.
*
* @throws Exception
* @see <a href="https://github.com/quarkusio/quarkus/issues/4627"/>
*/
@Test
public void testMultipleThreadedRequestWithIfModifiedSince() throws Exception {
// RFC1123 date formatter
final DateFormat dtf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
dtf.setTimeZone(TimeZone.getTimeZone("GMT"));
// date in past, so that we always get a 200 response, instead of 304
final Date fiveMinInPast = new Date(System.currentTimeMillis() - (5 * 60 * 1000));
final String modifiedSinceHeader = dtf.format(fiveMinInPast);
final int numRequests = 10;
final ExecutorService executorService = Executors.newFixedThreadPool(numRequests);
try {
final List<Future<Void>> results = new ArrayList<>();
for (int i = 0; i < numRequests; i++) {
// issue the requests
results.add(executorService.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
RestAssured.given().header("If-Modified-Since", modifiedSinceHeader)
.get("/web/index.html")
.then()
.body(containsString("<h1>Hello</h1>"))
.statusCode(200);
return null;
}
}));
}
// wait for completion
for (int i = 0; i < numRequests; i++) {
results.get(i).get(1, TimeUnit.MINUTES);
}
} finally {
executorService.shutdownNow();
}
}
}

0 comments on commit f90efa8

Please sign in to comment.