Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5b51e2e

Browse files
committed
Add test for zero-copy multipart
1 parente32431d commit5b51e2e

File tree

1 file changed

+65
-46
lines changed

1 file changed

+65
-46
lines changed
Lines changed: 65 additions & 46 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
@@ -12,43 +13,47 @@
1213
*/
1314
packagecom.ning.http.client.multipart;
1415

15-
importstaticjava.nio.charset.StandardCharsets.*;
16+
importstaticjava.nio.charset.StandardCharsets.UTF_8;
1617

1718
importorg.testng.Assert;
1819
importorg.testng.annotations.Test;
1920

20-
importcom.ning.http.client.Body;
2121
importcom.ning.http.client.FluentCaseInsensitiveStringsMap;
22-
importcom.ning.http.client.multipart.ByteArrayPart;
23-
importcom.ning.http.client.multipart.FilePart;
24-
importcom.ning.http.client.multipart.Part;
25-
importcom.ning.http.client.multipart.StringPart;
2622

2723
importjava.io.File;
2824
importjava.io.IOException;
2925
importjava.net.URISyntaxException;
3026
importjava.net.URL;
3127
importjava.nio.ByteBuffer;
28+
importjava.nio.channels.WritableByteChannel;
3229
importjava.util.ArrayList;
3330
importjava.util.List;
31+
importjava.util.concurrent.atomic.AtomicLong;
3432

3533
publicclassMultipartBodyTest {
3634

37-
@Test(groups ="fast")
38-
publicvoidtestBasics() {
39-
finalList<Part>parts =newArrayList<>();
35+
@Test
36+
publicvoidtransferWithCopy()throwsIOException {
37+
try (MultipartBodymultipartBody =buildMultipart()) {
38+
longtranferred =transferWithCopy(multipartBody);
39+
Assert.assertEquals(tranferred,multipartBody.getContentLength());
40+
}
41+
}
4042

41-
// add a file
42-
finalFiletestFile =getTestfile();
43-
parts.add(newFilePart("filePart",testFile));
43+
@Test
44+
publicvoidtransferZeroCopy()throwsIOException {
45+
try (MultipartBodymultipartBody =buildMultipart()) {
46+
longtranferred =transferZeroCopy(multipartBody);
47+
Assert.assertEquals(tranferred,multipartBody.getContentLength());
48+
}
49+
}
4450

45-
// add a byte array
51+
privatestaticMultipartBodybuildMultipart() {
52+
List<Part>parts =newArrayList<>();
53+
parts.add(newFilePart("filePart",getTestfile()));
4654
parts.add(newByteArrayPart("baPart","testMultiPart".getBytes(UTF_8),"application/test",UTF_8,"fileName"));
47-
48-
// add a string
4955
parts.add(newStringPart("stringPart","testString"));
50-
51-
compareContentLength(parts);
56+
returnMultipartUtils.newMultipartBody(parts,newFluentCaseInsensitiveStringsMap());
5257
}
5358

5459
privatestaticFilegetTestfile() {
@@ -64,35 +69,49 @@ private static File getTestfile() {
6469
returnfile;
6570
}
6671

67-
privatestaticvoidcompareContentLength(finalList<Part>parts) {
68-
Assert.assertNotNull(parts);
69-
// get expected values
70-
finalBodymultipartBody =MultipartUtils.newMultipartBody(parts,newFluentCaseInsensitiveStringsMap());
71-
finallongexpectedContentLength =multipartBody.getContentLength();
72-
try {
73-
finalByteBufferbuffer =ByteBuffer.allocate(8192);
74-
booleanlast =false;
75-
longtotalBytes =0;
76-
while (!last) {
77-
longreadBytes =0;
78-
try {
79-
readBytes =multipartBody.read(buffer);
80-
}catch (IOExceptionie) {
81-
Assert.fail("read failure");
82-
}
83-
if (readBytes >=0) {
84-
totalBytes +=readBytes;
85-
}else {
86-
last =true;
87-
}
88-
buffer.clear();
72+
privatestaticlongtransferWithCopy(MultipartBodymultipartBody)throwsIOException {
73+
74+
finalByteBufferbuffer =ByteBuffer.allocate(8192);
75+
longtotalBytes =0;
76+
while (true) {
77+
longreadBytes =multipartBody.read(buffer);
78+
if (readBytes <0) {
79+
break;
80+
}
81+
buffer.clear();
82+
totalBytes +=readBytes;
83+
}
84+
returntotalBytes;
85+
}
86+
87+
privatestaticlongtransferZeroCopy(MultipartBodymultipartBody)throwsIOException {
88+
89+
finalByteBufferbuffer =ByteBuffer.allocate(8192);
90+
finalAtomicLongtransferred =newAtomicLong();
91+
92+
WritableByteChannelmockChannel =newWritableByteChannel() {
93+
@Override
94+
publicbooleanisOpen() {
95+
returntrue;
8996
}
90-
Assert.assertEquals(totalBytes,expectedContentLength);
91-
}finally {
92-
try {
93-
multipartBody.close();
94-
}catch (IOExceptionignore) {
97+
98+
@Override
99+
publicvoidclose()throwsIOException {
100+
}
101+
102+
@Override
103+
publicintwrite(ByteBuffersrc)throwsIOException {
104+
intwritten =src.remaining();
105+
transferred.set(transferred.get() +written);
106+
src.position(src.limit());
107+
returnwritten;
95108
}
109+
};
110+
111+
while (transferred.get() <multipartBody.getContentLength()) {
112+
multipartBody.transferTo(0,mockChannel);
113+
buffer.clear();
96114
}
115+
returntransferred.get();
97116
}
98117
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp