Skip to content

Commit 017bc92

Browse files
slandelleStephane Landelle
authored andcommitted
Make PutLargeFileTest thread safe
1 parent f052c02 commit 017bc92

File tree

1 file changed

+35
-49
lines changed

1 file changed

+35
-49
lines changed

api/src/test/java/org/asynchttpclient/async/PutLargeFileTest.java

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,54 @@
1212
*/
1313
package org.asynchttpclient.async;
1414

15+
import java.io.File;
16+
import java.io.FileOutputStream;
17+
import java.io.IOException;
18+
import java.nio.charset.Charset;
19+
import java.util.UUID;
20+
21+
import javax.servlet.ServletException;
22+
import javax.servlet.http.HttpServletRequest;
23+
import javax.servlet.http.HttpServletResponse;
24+
1525
import org.asynchttpclient.AsyncHttpClient;
1626
import org.asynchttpclient.AsyncHttpClient.BoundRequestBuilder;
1727
import org.asynchttpclient.AsyncHttpClientConfig;
1828
import org.asynchttpclient.Response;
1929
import org.eclipse.jetty.server.Request;
2030
import org.eclipse.jetty.server.handler.AbstractHandler;
2131
import org.testng.Assert;
22-
import org.testng.annotations.AfterMethod;
2332
import org.testng.annotations.Test;
2433

25-
import javax.servlet.ServletException;
26-
import javax.servlet.ServletInputStream;
27-
import javax.servlet.http.HttpServletRequest;
28-
import javax.servlet.http.HttpServletResponse;
29-
import java.io.File;
30-
import java.io.FileOutputStream;
31-
import java.io.IOException;
32-
import java.util.UUID;
33-
3434
/**
3535
* @author Benjamin Hanzelmann
3636
*/
3737
public abstract class PutLargeFileTest extends AbstractBasicTest {
3838

39-
private File largeFile;
39+
private static final File TMP = new File(System.getProperty("java.io.tmpdir"), "ahc-tests-" + UUID.randomUUID().toString().substring(0, 8));
40+
private static final byte[] PATTERN_BYTES = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes(Charset.forName("UTF-16"));
41+
42+
static {
43+
TMP.mkdirs();
44+
TMP.deleteOnExit();
45+
}
4046

4147
@Test(groups = { "standalone", "default_provider" }, enabled = true)
4248
public void testPutLargeFile() throws Exception {
43-
byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16");
44-
long repeats = (1024 * 1024 * 100 / bytes.length) + 1;
45-
largeFile = createTempFile(bytes, (int) repeats);
46-
int timeout = (int) (largeFile.length() / 1000);
49+
50+
long repeats = (1024 * 1024 * 100 / PATTERN_BYTES.length) + 1;
51+
File file = createTempFile(PATTERN_BYTES, (int) repeats);
52+
long expectedFileSize = PATTERN_BYTES.length * repeats;
53+
Assert.assertEquals(expectedFileSize, file.length(), "Invalid file length");
54+
55+
int timeout = (int) (repeats / 1000);
56+
4757
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().setConnectionTimeoutInMs(timeout).build();
4858
AsyncHttpClient client = getAsyncHttpClient(config);
4959
try {
5060
BoundRequestBuilder rb = client.preparePut(getTargetUrl());
5161

52-
rb.setBody(largeFile);
62+
rb.setBody(file);
5363

5464
Response response = rb.execute().get();
5565
Assert.assertEquals(200, response.getStatusCode());
@@ -60,16 +70,17 @@ public void testPutLargeFile() throws Exception {
6070

6171
@Test(groups = { "standalone", "default_provider" })
6272
public void testPutSmallFile() throws Exception {
63-
byte[] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile".getBytes("UTF-16");
64-
long repeats = (1024 / bytes.length) + 1;
65-
// int timeout = (5000);
66-
largeFile = createTempFile(bytes, (int) repeats);
73+
74+
long repeats = (1024 / PATTERN_BYTES.length) + 1;
75+
File file = createTempFile(PATTERN_BYTES, (int) repeats);
76+
long expectedFileSize = PATTERN_BYTES.length * repeats;
77+
Assert.assertEquals(expectedFileSize, file.length(), "Invalid file length");
6778

6879
AsyncHttpClient client = getAsyncHttpClient(null);
6980
try {
7081
BoundRequestBuilder rb = client.preparePut(getTargetUrl());
7182

72-
rb.setBody(largeFile);
83+
rb.setBody(file);
7384

7485
Response response = rb.execute().get();
7586
Assert.assertEquals(200, response.getStatusCode());
@@ -78,26 +89,12 @@ public void testPutSmallFile() throws Exception {
7889
}
7990
}
8091

81-
@AfterMethod
82-
public void after() {
83-
largeFile.delete();
84-
}
85-
8692
@Override
8793
public AbstractHandler configureHandler() throws Exception {
8894
return new AbstractHandler() {
8995

9096
public void handle(String arg0, Request arg1, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
9197

92-
ServletInputStream in = req.getInputStream();
93-
byte[] b = new byte[8092];
94-
95-
int count = -1;
96-
int total = 0;
97-
while ((count = in.read(b)) != -1) {
98-
total += count;
99-
}
100-
10198
resp.setStatus(200);
10299
resp.getOutputStream().flush();
103100
resp.getOutputStream().close();
@@ -107,24 +104,12 @@ public void handle(String arg0, Request arg1, HttpServletRequest req, HttpServle
107104
};
108105
}
109106

110-
private static final File TMP = new File(System.getProperty("java.io.tmpdir"), "ahc-tests-" + UUID.randomUUID().toString().substring(0, 8));
111-
112107
public static File createTempFile(byte[] pattern, int repeat) throws IOException {
113-
TMP.mkdirs();
114-
TMP.deleteOnExit();
115108
File tmpFile = File.createTempFile("tmpfile-", ".data", TMP);
116109
tmpFile.deleteOnExit();
117-
write(pattern, repeat, tmpFile);
118-
119-
return tmpFile;
120-
}
121-
122-
public static void write(byte[] pattern, int repeat, File file) throws IOException {
123-
file.deleteOnExit();
124-
file.getParentFile().mkdirs();
125110
FileOutputStream out = null;
126111
try {
127-
out = new FileOutputStream(file);
112+
out = new FileOutputStream(tmpFile);
128113
for (int i = 0; i < repeat; i++) {
129114
out.write(pattern);
130115
}
@@ -133,6 +118,7 @@ public static void write(byte[] pattern, int repeat, File file) throws IOExcepti
133118
out.close();
134119
}
135120
}
136-
}
137121

122+
return tmpFile;
123+
}
138124
}

0 commit comments

Comments
 (0)