You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by vi...@apache.org on 2016/11/27 17:25:26 UTC
svn commit: r1771613 -
/tomcat/trunk/test/org/apache/catalina/connector/TestSendFile.java
Author: violetagg
Date: Sun Nov 27 17:25:25 2016
New Revision: 1771613
URL: http://svn.apache.org/viewvc?rev=1771613&view=rev
Log:
Unit test that reproduces bug 60409.
Modified:
tomcat/trunk/test/org/apache/catalina/connector/TestSendFile.java
Modified: tomcat/trunk/test/org/apache/catalina/connector/TestSendFile.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/connector/TestSendFile.java?rev=1771613&r1=1771612&r2=1771613&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/connector/TestSendFile.java (original)
+++ tomcat/trunk/test/org/apache/catalina/connector/TestSendFile.java Sun Nov 27 17:25:25 2016
@@ -22,10 +22,14 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -33,6 +37,8 @@ import javax.servlet.http.HttpServletReq
import javax.servlet.http.HttpServletResponse;
import static org.junit.Assert.assertEquals;
+
+import org.junit.Assert;
import org.junit.Test;
import org.apache.catalina.Context;
@@ -149,4 +155,93 @@ public class TestSendFile extends Tomcat
}
}
+
+ @Test
+ public void testBug60409() throws Exception {
+ Tomcat tomcat = getTomcatInstance();
+
+ Context ctx = tomcat.addContext("", TEMP_DIR);
+ File file = generateFile(TEMP_DIR, "", EXPECTED_CONTENT_LENGTH);
+ CountDownLatch latch = new CountDownLatch(2);
+ Tomcat.addServlet(ctx, "test", new Bug60409Servlet(file, latch));
+ ctx.addServletMappingDecoded("/", "test");
+
+ tomcat.start();
+
+ ByteChunk bc = new ByteChunk();
+ getUrl("http://localhost:" + getPort() + "/test/?" + Globals.SENDFILE_SUPPORTED_ATTR
+ + "=true", bc, null);
+
+ List<Throwable> exceptions = new ArrayList<>();
+ new Thread(new RequestExecutor("http://localhost:" + getPort() + "/test/", exceptions))
+ .start();
+ new Thread(new RequestExecutor("http://localhost:" + getPort() + "/test/", exceptions))
+ .start();
+
+ latch.await(3000, TimeUnit.MILLISECONDS);
+
+ if (exceptions.size() > 0) {
+ Assert.fail();
+ }
+ }
+
+ private static final class Bug60409Servlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+ private final File file;
+ private final CountDownLatch latch;
+
+ Bug60409Servlet(File file, CountDownLatch latch) {
+ this.file = file;
+ this.latch = latch;
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ resp.setContentType("'application/octet-stream");
+ resp.setCharacterEncoding("ISO-8859-1");
+ resp.setContentLengthLong(file.length());
+ if (Boolean.TRUE.equals(req.getAttribute(Globals.SENDFILE_SUPPORTED_ATTR))) {
+ req.setAttribute(Globals.SENDFILE_FILENAME_ATTR, file.getAbsolutePath());
+ req.setAttribute(Globals.SENDFILE_FILE_START_ATTR, new Long(0));
+ req.setAttribute(Globals.SENDFILE_FILE_END_ATTR, new Long(file.length()));
+ file.delete();
+ } else {
+ byte[] c = new byte[1024];
+ Random rd = new Random();
+ rd.nextBytes(c);
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ resp.getOutputStream().write(c);
+ latch.countDown();
+ }
+ }
+
+ }
+
+ private static final class RequestExecutor implements Runnable {
+ private final String url;
+ private final List<Throwable> exceptions;
+
+ public RequestExecutor(String url, List<Throwable> exceptions) {
+ this.url = url;
+ this.exceptions = exceptions;
+ }
+
+ @Override
+ public void run() {
+ try {
+ ByteChunk result = new ByteChunk();
+ int rc = getUrl(url, result, null);
+ Assert.assertTrue(rc == HttpServletResponse.SC_OK);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ exceptions.add(e);
+ }
+ }
+
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org