12
12
*/
13
13
package org .asynchttpclient .async ;
14
14
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
+
15
25
import org .asynchttpclient .AsyncHttpClient ;
16
26
import org .asynchttpclient .AsyncHttpClient .BoundRequestBuilder ;
17
27
import org .asynchttpclient .AsyncHttpClientConfig ;
18
28
import org .asynchttpclient .Response ;
19
29
import org .eclipse .jetty .server .Request ;
20
30
import org .eclipse .jetty .server .handler .AbstractHandler ;
21
31
import org .testng .Assert ;
22
- import org .testng .annotations .AfterMethod ;
23
32
import org .testng .annotations .Test ;
24
33
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
-
34
34
/**
35
35
* @author Benjamin Hanzelmann
36
36
*/
37
37
public abstract class PutLargeFileTest extends AbstractBasicTest {
38
38
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
+ }
40
46
41
47
@ Test (groups = { "standalone" , "default_provider" }, enabled = true )
42
48
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
+
47
57
AsyncHttpClientConfig config = new AsyncHttpClientConfig .Builder ().setConnectionTimeoutInMs (timeout ).build ();
48
58
AsyncHttpClient client = getAsyncHttpClient (config );
49
59
try {
50
60
BoundRequestBuilder rb = client .preparePut (getTargetUrl ());
51
61
52
- rb .setBody (largeFile );
62
+ rb .setBody (file );
53
63
54
64
Response response = rb .execute ().get ();
55
65
Assert .assertEquals (200 , response .getStatusCode ());
@@ -60,16 +70,17 @@ public void testPutLargeFile() throws Exception {
60
70
61
71
@ Test (groups = { "standalone" , "default_provider" })
62
72
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" );
67
78
68
79
AsyncHttpClient client = getAsyncHttpClient (null );
69
80
try {
70
81
BoundRequestBuilder rb = client .preparePut (getTargetUrl ());
71
82
72
- rb .setBody (largeFile );
83
+ rb .setBody (file );
73
84
74
85
Response response = rb .execute ().get ();
75
86
Assert .assertEquals (200 , response .getStatusCode ());
@@ -78,26 +89,12 @@ public void testPutSmallFile() throws Exception {
78
89
}
79
90
}
80
91
81
- @ AfterMethod
82
- public void after () {
83
- largeFile .delete ();
84
- }
85
-
86
92
@ Override
87
93
public AbstractHandler configureHandler () throws Exception {
88
94
return new AbstractHandler () {
89
95
90
96
public void handle (String arg0 , Request arg1 , HttpServletRequest req , HttpServletResponse resp ) throws IOException , ServletException {
91
97
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
-
101
98
resp .setStatus (200 );
102
99
resp .getOutputStream ().flush ();
103
100
resp .getOutputStream ().close ();
@@ -107,24 +104,12 @@ public void handle(String arg0, Request arg1, HttpServletRequest req, HttpServle
107
104
};
108
105
}
109
106
110
- private static final File TMP = new File (System .getProperty ("java.io.tmpdir" ), "ahc-tests-" + UUID .randomUUID ().toString ().substring (0 , 8 ));
111
-
112
107
public static File createTempFile (byte [] pattern , int repeat ) throws IOException {
113
- TMP .mkdirs ();
114
- TMP .deleteOnExit ();
115
108
File tmpFile = File .createTempFile ("tmpfile-" , ".data" , TMP );
116
109
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 ();
125
110
FileOutputStream out = null ;
126
111
try {
127
- out = new FileOutputStream (file );
112
+ out = new FileOutputStream (tmpFile );
128
113
for (int i = 0 ; i < repeat ; i ++) {
129
114
out .write (pattern );
130
115
}
@@ -133,6 +118,7 @@ public static void write(byte[] pattern, int repeat, File file) throws IOExcepti
133
118
out .close ();
134
119
}
135
120
}
136
- }
137
121
122
+ return tmpFile ;
123
+ }
138
124
}
0 commit comments