47
47
48
48
/**
49
49
* Tests default asynchronous life cycle.
50
- *
50
+ *
51
51
* @author Hubert Iwaniuk
52
52
*/
53
53
public abstract class AsyncStreamLifecycleTest extends AbstractBasicTest {
@@ -63,8 +63,7 @@ public void tearDownGlobal() throws Exception {
63
63
@ Override
64
64
public AbstractHandler configureHandler () throws Exception {
65
65
return new AbstractHandler () {
66
- public void handle (String s , Request request , HttpServletRequest req , final HttpServletResponse resp )
67
- throws IOException , ServletException {
66
+ public void handle (String s , Request request , HttpServletRequest req , final HttpServletResponse resp ) throws IOException , ServletException {
68
67
resp .setContentType ("text/plain;charset=utf-8" );
69
68
resp .setStatus (200 );
70
69
final Continuation continuation = ContinuationSupport .getContinuation (req );
@@ -100,62 +99,64 @@ public void run() {
100
99
};
101
100
}
102
101
103
- //TODO Netty only.
102
+ // TODO Netty only.
104
103
105
- @ Test (groups = {"standalone" , "default_provider" })
104
+ @ Test (groups = { "standalone" , "default_provider" })
106
105
public void testStream () throws IOException {
107
- AsyncHttpClient ahc = getAsyncHttpClient (null );
108
- final AtomicBoolean err = new AtomicBoolean (false );
109
- final LinkedBlockingQueue <String > queue = new LinkedBlockingQueue <String >();
110
- final AtomicBoolean status = new AtomicBoolean (false );
111
- final AtomicInteger headers = new AtomicInteger (0 );
112
- final CountDownLatch latch = new CountDownLatch (1 );
113
- ahc .executeRequest (ahc .prepareGet (getTargetUrl ()).build (), new AsyncHandler <Object >() {
114
- public void onThrowable (Throwable t ) {
115
- fail ("Got throwable." , t );
116
- err .set (true );
117
- }
106
+ AsyncHttpClient client = getAsyncHttpClient (null );
107
+ try {
108
+ final AtomicBoolean err = new AtomicBoolean (false );
109
+ final LinkedBlockingQueue <String > queue = new LinkedBlockingQueue <String >();
110
+ final AtomicBoolean status = new AtomicBoolean (false );
111
+ final AtomicInteger headers = new AtomicInteger (0 );
112
+ final CountDownLatch latch = new CountDownLatch (1 );
113
+ client .executeRequest (client .prepareGet (getTargetUrl ()).build (), new AsyncHandler <Object >() {
114
+ public void onThrowable (Throwable t ) {
115
+ fail ("Got throwable." , t );
116
+ err .set (true );
117
+ }
118
118
119
- public STATE onBodyPartReceived (HttpResponseBodyPart e ) throws Exception {
120
- String s = new String (e .getBodyPartBytes ());
121
- log .info ("got part: {}" , s );
122
- if (s .isEmpty ()) {
123
- //noinspection ThrowableInstanceNeverThrown
124
- log .warn ("Sampling stacktrace." ,
125
- new Throwable ("trace that, we should not get called for empty body." ));
119
+ public STATE onBodyPartReceived (HttpResponseBodyPart e ) throws Exception {
120
+ String s = new String (e .getBodyPartBytes ());
121
+ log .info ("got part: {}" , s );
122
+ if (s .isEmpty ()) {
123
+ // noinspection ThrowableInstanceNeverThrown
124
+ log .warn ("Sampling stacktrace." , new Throwable ("trace that, we should not get called for empty body." ));
125
+ }
126
+ queue .put (s );
127
+ return STATE .CONTINUE ;
126
128
}
127
- queue .put (s );
128
- return STATE .CONTINUE ;
129
- }
130
129
131
- public STATE onStatusReceived (HttpResponseStatus e ) throws Exception {
132
- status .set (true );
133
- return STATE .CONTINUE ;
134
- }
130
+ public STATE onStatusReceived (HttpResponseStatus e ) throws Exception {
131
+ status .set (true );
132
+ return STATE .CONTINUE ;
133
+ }
135
134
136
- public STATE onHeadersReceived (HttpResponseHeaders e ) throws Exception {
137
- if (headers .incrementAndGet () == 2 ) {
138
- throw new Exception ("Analyze this." );
135
+ public STATE onHeadersReceived (HttpResponseHeaders e ) throws Exception {
136
+ if (headers .incrementAndGet () == 2 ) {
137
+ throw new Exception ("Analyze this." );
138
+ }
139
+ return STATE .CONTINUE ;
139
140
}
140
- return STATE .CONTINUE ;
141
- }
142
141
143
- public Object onCompleted () throws Exception {
144
- latch .countDown ();
145
- return null ;
142
+ public Object onCompleted () throws Exception {
143
+ latch .countDown ();
144
+ return null ;
145
+ }
146
+ });
147
+ try {
148
+ assertTrue (latch .await (1 , TimeUnit .SECONDS ), "Latch failed." );
149
+ } catch (InterruptedException e ) {
150
+ fail ("Interrupted." , e );
146
151
}
147
- });
148
- try {
149
- assertTrue (latch .await (1 , TimeUnit .SECONDS ), "Latch failed." );
150
- } catch (InterruptedException e ) {
151
- fail ("Interrupted." , e );
152
+ assertFalse (err .get ());
153
+ assertEquals (queue .size (), 2 );
154
+ assertTrue (queue .contains ("part1" ));
155
+ assertTrue (queue .contains ("part2" ));
156
+ assertTrue (status .get ());
157
+ assertEquals (headers .get (), 1 );
158
+ } finally {
159
+ client .close ();
152
160
}
153
- assertFalse (err .get ());
154
- assertEquals (queue .size (), 2 );
155
- assertTrue (queue .contains ("part1" ));
156
- assertTrue (queue .contains ("part2" ));
157
- assertTrue (status .get ());
158
- assertEquals (headers .get (), 1 );
159
- ahc .close ();
160
161
}
161
162
}
0 commit comments