Skip to content

Commit 3f58ea5

Browse files
committed
Refactor MultipartBodyTest (make sure it works with different buffer sizes)
1 parent d694b5a commit 3f58ea5

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

client/src/test/java/org/asynchttpclient/request/body/multipart/MultipartBodyTest.java

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,47 +34,77 @@
3434

3535
public class MultipartBodyTest {
3636

37-
@Test
38-
public void transferWithCopy() throws Exception {
39-
try (MultipartBody multipartBody = buildMultipart()) {
40-
long tranferred = transferWithCopy(multipartBody);
41-
assertEquals(tranferred, multipartBody.getContentLength());
42-
}
43-
}
37+
private static final List<Part> PARTS = new ArrayList<>();
4438

45-
@Test
46-
public void transferZeroCopy() throws Exception {
47-
try (MultipartBody multipartBody = buildMultipart()) {
48-
long tranferred = transferZeroCopy(multipartBody);
49-
assertEquals(tranferred, multipartBody.getContentLength());
39+
static {
40+
try {
41+
PARTS.add(new FilePart("filePart", getTestfile()));
42+
} catch (URISyntaxException e) {
43+
throw new ExceptionInInitializerError(e);
5044
}
45+
PARTS.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName"));
46+
PARTS.add(new StringPart("stringPart", "testString"));
5147
}
5248

53-
private File getTestfile() throws URISyntaxException {
49+
private static File getTestfile() throws URISyntaxException {
5450
final ClassLoader cl = MultipartBodyTest.class.getClassLoader();
5551
final URL url = cl.getResource("textfile.txt");
5652
assertNotNull(url);
5753
return new File(url.toURI());
5854
}
5955

60-
private MultipartBody buildMultipart() throws URISyntaxException {
61-
List<Part> parts = new ArrayList<>();
62-
parts.add(new FilePart("filePart", getTestfile()));
63-
parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName"));
64-
parts.add(new StringPart("stringPart", "testString"));
65-
return MultipartUtils.newMultipartBody(parts, HttpHeaders.EMPTY_HEADERS);
56+
private static long MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE;
57+
58+
static {
59+
try (MultipartBody dummyBody = buildMultipart()) {
60+
// separator is random
61+
MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE = dummyBody.getContentLength() + 100;
62+
} catch (IOException e) {
63+
throw new ExceptionInInitializerError(e);
64+
}
65+
}
66+
67+
private static MultipartBody buildMultipart() {
68+
return MultipartUtils.newMultipartBody(PARTS, HttpHeaders.EMPTY_HEADERS);
6669
}
6770

68-
private long transferWithCopy(MultipartBody multipartBody) throws IOException {
69-
final ByteBuf buffer = Unpooled.buffer(8192);
70-
while (multipartBody.transferTo(buffer) != BodyState.STOP) {
71+
@Test
72+
public void transferWithCopy() throws Exception {
73+
for (int bufferLength = 1; bufferLength < MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE + 1; bufferLength++) {
74+
try (MultipartBody multipartBody = buildMultipart()) {
75+
long tranferred = transferWithCopy(multipartBody, bufferLength);
76+
assertEquals(tranferred, multipartBody.getContentLength());
77+
}
78+
}
79+
}
80+
81+
@Test
82+
public void transferZeroCopy() throws Exception {
83+
for (int bufferLength = 1; bufferLength < MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE + 1; bufferLength++) {
84+
try (MultipartBody multipartBody = buildMultipart()) {
85+
long tranferred = transferZeroCopy(multipartBody, bufferLength);
86+
assertEquals(tranferred, multipartBody.getContentLength());
87+
}
88+
}
89+
}
90+
91+
private static long transferWithCopy(MultipartBody multipartBody, int bufferSize) throws IOException {
92+
long transferred = 0;
93+
final ByteBuf buffer = Unpooled.buffer(bufferSize);
94+
try {
95+
while (multipartBody.transferTo(buffer) != BodyState.STOP) {
96+
transferred += buffer.readableBytes();
97+
buffer.clear();
98+
}
99+
return transferred;
100+
} finally {
101+
buffer.release();
71102
}
72-
return buffer.readableBytes();
73103
}
74104

75-
private static long transferZeroCopy(MultipartBody multipartBody) throws IOException {
105+
private static long transferZeroCopy(MultipartBody multipartBody, int bufferSize) throws IOException {
76106

77-
final ByteBuffer buffer = ByteBuffer.allocate(8192);
107+
final ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
78108
final AtomicLong transferred = new AtomicLong();
79109

80110
WritableByteChannel mockChannel = new WritableByteChannel() {

0 commit comments

Comments
 (0)