Skip to content

Commit becc8b7

Browse files
committed
Reimplement multipart test + add zero-copy test
1 parent 1858481 commit becc8b7

File tree

1 file changed

+62
-30
lines changed

1 file changed

+62
-30
lines changed
Lines changed: 62 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*
2-
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
2+
* Copyright (c) 2016 AsyncHttpClient Project. All rights reserved.
33
*
44
* This program is licensed to you under the Apache License Version 2.0,
55
* and you may not use this file except in compliance with the Apache License Version 2.0.
6-
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
78
*
89
* Unless required by applicable law or agreed to in writing,
910
* software distributed under the Apache License Version 2.0 is distributed on an
@@ -22,52 +23,83 @@
2223
import java.io.IOException;
2324
import java.net.URISyntaxException;
2425
import java.net.URL;
26+
import java.nio.ByteBuffer;
27+
import java.nio.channels.WritableByteChannel;
2528
import java.util.ArrayList;
2629
import java.util.List;
30+
import java.util.concurrent.atomic.AtomicLong;
2731

28-
import org.apache.commons.io.IOUtils;
2932
import org.asynchttpclient.request.body.Body.BodyState;
3033
import org.testng.annotations.Test;
3134

3235
public class MultipartBodyTest {
3336

34-
@Test(groups = "standalone")
35-
public void testBasics() throws Exception {
36-
final List<Part> parts = new ArrayList<>();
37-
38-
// add a file
39-
final File testFile = getTestfile();
40-
System.err.println(testFile.length());
41-
parts.add(new FilePart("filePart", testFile));
42-
43-
// add a byte array
44-
parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName"));
45-
46-
// add a string
47-
parts.add(new StringPart("stringPart", "testString"));
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+
}
4844

49-
compareContentLength(parts);
45+
@Test
46+
public void transferZeroCopy() throws Exception {
47+
try (MultipartBody multipartBody = buildMultipart()) {
48+
long tranferred = transferZeroCopy(multipartBody);
49+
assertEquals(tranferred, multipartBody.getContentLength());
50+
}
5051
}
5152

52-
private static File getTestfile() throws URISyntaxException {
53+
private File getTestfile() throws URISyntaxException {
5354
final ClassLoader cl = MultipartBodyTest.class.getClassLoader();
5455
final URL url = cl.getResource("textfile.txt");
5556
assertNotNull(url);
5657
return new File(url.toURI());
5758
}
5859

59-
private static void compareContentLength(final List<Part> parts) throws IOException {
60-
assertNotNull(parts);
61-
// get expected values
62-
final MultipartBody multipartBody = MultipartUtils.newMultipartBody(parts, HttpHeaders.EMPTY_HEADERS);
63-
final long expectedContentLength = multipartBody.getContentLength();
64-
try {
65-
final ByteBuf buffer = Unpooled.buffer(8192);
66-
while (multipartBody.transferTo(buffer) != BodyState.STOP) {
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);
66+
}
67+
68+
private long transferWithCopy(MultipartBody multipartBody) throws IOException {
69+
final ByteBuf buffer = Unpooled.buffer(8192);
70+
while (multipartBody.transferTo(buffer) != BodyState.STOP) {
71+
}
72+
return buffer.readableBytes();
73+
}
74+
75+
private static long transferZeroCopy(MultipartBody multipartBody) throws IOException {
76+
77+
final ByteBuffer buffer = ByteBuffer.allocate(8192);
78+
final AtomicLong transferred = new AtomicLong();
79+
80+
WritableByteChannel mockChannel = new WritableByteChannel() {
81+
@Override
82+
public boolean isOpen() {
83+
return true;
6784
}
68-
assertEquals(buffer.readableBytes(), expectedContentLength);
69-
} finally {
70-
IOUtils.closeQuietly(multipartBody);
85+
86+
@Override
87+
public void close() throws IOException {
88+
}
89+
90+
@Override
91+
public int write(ByteBuffer src) throws IOException {
92+
int written = src.remaining();
93+
transferred.set(transferred.get() + written);
94+
src.position(src.limit());
95+
return written;
96+
}
97+
};
98+
99+
while (transferred.get() < multipartBody.getContentLength()) {
100+
multipartBody.transferTo(mockChannel);
101+
buffer.clear();
71102
}
103+
return transferred.get();
72104
}
73105
}

0 commit comments

Comments
 (0)