12
12
*/
13
13
package org .asynchttpclient .async ;
14
14
15
- import org .asynchttpclient .AsyncHttpClient ;
16
- import org .asynchttpclient .FluentCaseInsensitiveStringsMap ;
17
- import org .asynchttpclient .Response ;
18
- import org .asynchttpclient .generators .FileBodyGenerator ;
19
- import org .asynchttpclient .listener .TransferCompletionHandler ;
20
- import org .asynchttpclient .listener .TransferListener ;
21
- import org .eclipse .jetty .server .handler .AbstractHandler ;
22
- import org .testng .annotations .Test ;
15
+ import static org .testng .Assert .assertEquals ;
16
+ import static org .testng .Assert .assertNotNull ;
17
+ import static org .testng .Assert .assertNull ;
18
+ import static org .testng .Assert .fail ;
23
19
24
- import javax .servlet .ServletException ;
25
- import javax .servlet .http .HttpServletRequest ;
26
- import javax .servlet .http .HttpServletResponse ;
27
20
import java .io .File ;
28
21
import java .io .FileOutputStream ;
29
22
import java .io .IOException ;
30
- import java .nio .ByteBuffer ;
23
+ import java .nio .charset . Charset ;
31
24
import java .util .Enumeration ;
32
25
import java .util .UUID ;
33
26
import java .util .concurrent .atomic .AtomicBoolean ;
34
27
import java .util .concurrent .atomic .AtomicInteger ;
28
+ import java .util .concurrent .atomic .AtomicLong ;
35
29
import java .util .concurrent .atomic .AtomicReference ;
36
30
37
- import static org .testng .Assert .assertEquals ;
38
- import static org .testng .Assert .assertNotNull ;
39
- import static org .testng .Assert .assertNull ;
40
- import static org .testng .Assert .fail ;
31
+ import javax .servlet .ServletException ;
32
+ import javax .servlet .http .HttpServletRequest ;
33
+ import javax .servlet .http .HttpServletResponse ;
34
+
35
+ import org .asynchttpclient .AsyncHttpClient ;
36
+ import org .asynchttpclient .AsyncHttpClientConfig ;
37
+ import org .asynchttpclient .FluentCaseInsensitiveStringsMap ;
38
+ import org .asynchttpclient .Response ;
39
+ import org .asynchttpclient .generators .FileBodyGenerator ;
40
+ import org .asynchttpclient .listener .TransferCompletionHandler ;
41
+ import org .asynchttpclient .listener .TransferListener ;
42
+ import org .eclipse .jetty .server .handler .AbstractHandler ;
43
+ import org .testng .Assert ;
44
+ import org .testng .annotations .Test ;
41
45
42
46
public abstract class TransferListenerTest extends AbstractBasicTest {
47
+
43
48
private static final File TMP = new File (System .getProperty ("java.io.tmpdir" ), "ahc-tests-" + UUID .randomUUID ().toString ().substring (0 , 8 ));
49
+ private static final byte [] PATTERN_BYTES = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile" .getBytes (Charset .forName ("UTF-16" ));
50
+
51
+ static {
52
+ TMP .mkdirs ();
53
+ TMP .deleteOnExit ();
54
+ }
44
55
45
56
private class BasicHandler extends AbstractHandler {
46
57
@@ -81,7 +92,7 @@ public void basicGetTest() throws Throwable {
81
92
final AtomicReference <Throwable > throwable = new AtomicReference <Throwable >();
82
93
final AtomicReference <FluentCaseInsensitiveStringsMap > hSent = new AtomicReference <FluentCaseInsensitiveStringsMap >();
83
94
final AtomicReference <FluentCaseInsensitiveStringsMap > hRead = new AtomicReference <FluentCaseInsensitiveStringsMap >();
84
- final AtomicReference <ByteBuffer > bb = new AtomicReference <ByteBuffer >();
95
+ final AtomicReference <byte [] > bb = new AtomicReference <byte [] >();
85
96
final AtomicBoolean completed = new AtomicBoolean (false );
86
97
87
98
TransferCompletionHandler tl = new TransferCompletionHandler ();
@@ -95,11 +106,11 @@ public void onResponseHeadersReceived(FluentCaseInsensitiveStringsMap headers) {
95
106
hRead .set (headers );
96
107
}
97
108
98
- public void onBytesReceived (ByteBuffer buffer ) {
99
- bb .set (buffer );
109
+ public void onBytesReceived (byte [] b ) {
110
+ bb .set (b );
100
111
}
101
112
102
- public void onBytesSent (ByteBuffer buffer ) {
113
+ public void onBytesSent (long amount , long current , long total ) {
103
114
}
104
115
105
116
public void onRequestResponseCompleted () {
@@ -130,20 +141,24 @@ public void onThrowable(Throwable t) {
130
141
131
142
@ Test (groups = { "standalone" , "default_provider" })
132
143
public void basicPutTest () throws Throwable {
133
- AsyncHttpClient c = getAsyncHttpClient (null );
134
- try {
135
- final AtomicReference <Throwable > throwable = new AtomicReference <Throwable >();
136
- final AtomicReference <FluentCaseInsensitiveStringsMap > hSent = new AtomicReference <FluentCaseInsensitiveStringsMap >();
137
- final AtomicReference <FluentCaseInsensitiveStringsMap > hRead = new AtomicReference <FluentCaseInsensitiveStringsMap >();
138
- final AtomicInteger bbReceivedLenght = new AtomicInteger (0 );
139
- final AtomicInteger bbSentLenght = new AtomicInteger (0 );
144
+ final AtomicReference <Throwable > throwable = new AtomicReference <Throwable >();
145
+ final AtomicReference <FluentCaseInsensitiveStringsMap > hSent = new AtomicReference <FluentCaseInsensitiveStringsMap >();
146
+ final AtomicReference <FluentCaseInsensitiveStringsMap > hRead = new AtomicReference <FluentCaseInsensitiveStringsMap >();
147
+ final AtomicInteger bbReceivedLenght = new AtomicInteger (0 );
148
+ final AtomicLong bbSentLenght = new AtomicLong (0L );
140
149
141
- final AtomicBoolean completed = new AtomicBoolean (false );
150
+ final AtomicBoolean completed = new AtomicBoolean (false );
151
+
152
+ byte [] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile" .getBytes ("UTF-16" );
153
+ long repeats = (1024 * 100 * 10 / bytes .length ) + 1 ;
154
+ File file = createTempFile (bytes , (int ) repeats );
155
+ long expectedFileSize = PATTERN_BYTES .length * repeats ;
156
+ Assert .assertEquals (expectedFileSize , file .length (), "Invalid file length" );
142
157
143
- byte [] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile" .getBytes ("UTF-16" );
144
- long repeats = (1024 * 100 * 10 / bytes .length ) + 1 ;
145
- File largeFile = createTempFile (bytes , (int ) repeats );
158
+ int timeout = (int ) (repeats / 1000 );
159
+ AsyncHttpClient client = getAsyncHttpClient (new AsyncHttpClientConfig .Builder ().setConnectionTimeoutInMs (timeout ).build ());
146
160
161
+ try {
147
162
TransferCompletionHandler tl = new TransferCompletionHandler ();
148
163
tl .addTransferListener (new TransferListener () {
149
164
@@ -155,12 +170,12 @@ public void onResponseHeadersReceived(FluentCaseInsensitiveStringsMap headers) {
155
170
hRead .set (headers );
156
171
}
157
172
158
- public void onBytesReceived (ByteBuffer buffer ) {
159
- bbReceivedLenght .addAndGet (buffer . capacity () );
173
+ public void onBytesReceived (byte [] b ) {
174
+ bbReceivedLenght .addAndGet (b . length );
160
175
}
161
176
162
- public void onBytesSent (ByteBuffer buffer ) {
163
- bbSentLenght .addAndGet (buffer . capacity () );
177
+ public void onBytesSent (long amount , long current , long total ) {
178
+ bbSentLenght .addAndGet (amount );
164
179
}
165
180
166
181
public void onRequestResponseCompleted () {
@@ -173,37 +188,38 @@ public void onThrowable(Throwable t) {
173
188
});
174
189
175
190
try {
176
- Response response = c .preparePut (getTargetUrl ()).setBody (largeFile ).execute (tl ).get ();
191
+ Response response = client .preparePut (getTargetUrl ()).setBody (file ).execute (tl ).get ();
177
192
178
193
assertNotNull (response );
179
194
assertEquals (response .getStatusCode (), 200 );
180
195
assertNotNull (hRead .get ());
181
196
assertNotNull (hSent .get ());
182
- assertEquals (bbReceivedLenght .get (), largeFile . length () );
183
- assertEquals (bbSentLenght .get (), largeFile . length () );
197
+ assertEquals (bbReceivedLenght .get (), expectedFileSize , "Number of received bytes incorrect" );
198
+ assertEquals (bbSentLenght .get (), expectedFileSize , "Number of sent bytes incorrect" );
184
199
} catch (IOException ex ) {
185
200
fail ("Should have timed out" );
186
201
}
187
202
} finally {
188
- c .close ();
203
+ client .close ();
189
204
}
190
205
}
191
206
192
207
@ Test (groups = { "standalone" , "default_provider" })
193
208
public void basicPutBodyTest () throws Throwable {
194
- AsyncHttpClient c = getAsyncHttpClient (null );
209
+ AsyncHttpClient client = getAsyncHttpClient (null );
195
210
try {
196
211
final AtomicReference <Throwable > throwable = new AtomicReference <Throwable >();
197
212
final AtomicReference <FluentCaseInsensitiveStringsMap > hSent = new AtomicReference <FluentCaseInsensitiveStringsMap >();
198
213
final AtomicReference <FluentCaseInsensitiveStringsMap > hRead = new AtomicReference <FluentCaseInsensitiveStringsMap >();
199
214
final AtomicInteger bbReceivedLenght = new AtomicInteger (0 );
200
- final AtomicInteger bbSentLenght = new AtomicInteger ( 0 );
215
+ final AtomicLong bbSentLenght = new AtomicLong ( 0L );
201
216
202
217
final AtomicBoolean completed = new AtomicBoolean (false );
203
218
204
- byte [] bytes = "RatherLargeFileRatherLargeFileRatherLargeFileRatherLargeFile" .getBytes ("UTF-16" );
205
- long repeats = (1024 * 100 * 10 / bytes .length ) + 1 ;
206
- File largeFile = createTempFile (bytes , (int ) repeats );
219
+ long repeats = (1024 * 100 * 10 / PATTERN_BYTES .length ) + 1 ;
220
+ File file = createTempFile (PATTERN_BYTES , (int ) repeats );
221
+ long expectedFileSize = PATTERN_BYTES .length * repeats ;
222
+ Assert .assertEquals (expectedFileSize , file .length (), "Invalid file length" );
207
223
208
224
TransferCompletionHandler tl = new TransferCompletionHandler ();
209
225
tl .addTransferListener (new TransferListener () {
@@ -216,12 +232,12 @@ public void onResponseHeadersReceived(FluentCaseInsensitiveStringsMap headers) {
216
232
hRead .set (headers );
217
233
}
218
234
219
- public void onBytesReceived (ByteBuffer buffer ) {
220
- bbReceivedLenght .addAndGet (buffer . capacity () );
235
+ public void onBytesReceived (byte [] b ) {
236
+ bbReceivedLenght .addAndGet (b . length );
221
237
}
222
238
223
- public void onBytesSent (ByteBuffer buffer ) {
224
- bbSentLenght .addAndGet (buffer . capacity () );
239
+ public void onBytesSent (long amount , long current , long total ) {
240
+ bbSentLenght .addAndGet (amount );
225
241
}
226
242
227
243
public void onRequestResponseCompleted () {
@@ -234,19 +250,19 @@ public void onThrowable(Throwable t) {
234
250
});
235
251
236
252
try {
237
- Response response = c .preparePut (getTargetUrl ()).setBody (new FileBodyGenerator (largeFile )).execute (tl ).get ();
253
+ Response response = client .preparePut (getTargetUrl ()).setBody (new FileBodyGenerator (file )).execute (tl ).get ();
238
254
239
255
assertNotNull (response );
240
256
assertEquals (response .getStatusCode (), 200 );
241
257
assertNotNull (hRead .get ());
242
258
assertNotNull (hSent .get ());
243
- assertEquals (bbReceivedLenght .get (), largeFile . length () );
244
- assertEquals (bbSentLenght .get (), largeFile . length () );
259
+ assertEquals (bbReceivedLenght .get (), expectedFileSize , "Number of received bytes incorrect" );
260
+ assertEquals (bbSentLenght .get (), expectedFileSize , "Number of sent bytes incorrect" );
245
261
} catch (IOException ex ) {
246
262
fail ("Should have timed out" );
247
263
}
248
264
} finally {
249
- c .close ();
265
+ client .close ();
250
266
}
251
267
}
252
268
@@ -255,20 +271,11 @@ public String getTargetUrl() {
255
271
}
256
272
257
273
public static File createTempFile (byte [] pattern , int repeat ) throws IOException {
258
- TMP .mkdirs ();
259
- TMP .deleteOnExit ();
260
274
File tmpFile = File .createTempFile ("tmpfile-" , ".data" , TMP );
261
- write (pattern , repeat , tmpFile );
262
-
263
- return tmpFile ;
264
- }
265
-
266
- public static void write (byte [] pattern , int repeat , File file ) throws IOException {
267
- file .deleteOnExit ();
268
- file .getParentFile ().mkdirs ();
275
+ tmpFile .deleteOnExit ();
269
276
FileOutputStream out = null ;
270
277
try {
271
- out = new FileOutputStream (file );
278
+ out = new FileOutputStream (tmpFile );
272
279
for (int i = 0 ; i < repeat ; i ++) {
273
280
out .write (pattern );
274
281
}
@@ -277,5 +284,7 @@ public static void write(byte[] pattern, int repeat, File file) throws IOExcepti
277
284
out .close ();
278
285
}
279
286
}
287
+
288
+ return tmpFile ;
280
289
}
281
290
}
0 commit comments