28
28
import io .reactivex .netty .contexts .RxContexts ;
29
29
import io .reactivex .netty .contexts .TestContext ;
30
30
import io .reactivex .netty .contexts .TestContextSerializer ;
31
+ import io .reactivex .netty .protocol .http .client .FlatResponseOperator ;
31
32
import io .reactivex .netty .protocol .http .client .HttpClient ;
32
33
import io .reactivex .netty .protocol .http .client .HttpClientRequest ;
33
34
import io .reactivex .netty .protocol .http .client .HttpClientResponse ;
35
+ import io .reactivex .netty .protocol .http .client .ResponseHolder ;
34
36
import io .reactivex .netty .protocol .http .server .HttpServer ;
35
37
import io .reactivex .netty .protocol .http .server .HttpServerBuilder ;
36
38
import io .reactivex .netty .protocol .http .server .HttpServerRequest ;
41
43
import org .junit .Before ;
42
44
import org .junit .Test ;
43
45
import rx .Observable ;
44
- import rx .functions .Action0 ;
45
- import rx .functions .Action1 ;
46
46
import rx .functions .Func1 ;
47
47
48
- import java .util .ArrayList ;
49
- import java .util .List ;
50
48
import java .util .concurrent .Callable ;
51
- import java .util .concurrent .CountDownLatch ;
49
+ import java .util .concurrent .ExecutionException ;
52
50
import java .util .concurrent .ExecutorService ;
53
51
import java .util .concurrent .Executors ;
54
52
import java .util .concurrent .Future ;
55
53
import java .util .concurrent .TimeUnit ;
54
+ import java .util .concurrent .TimeoutException ;
56
55
57
56
import static io .reactivex .netty .contexts .ThreadLocalRequestCorrelator .getCurrentContextContainer ;
58
57
import static io .reactivex .netty .contexts .ThreadLocalRequestCorrelator .getCurrentRequestId ;
@@ -103,7 +102,7 @@ public String getContextValue(String key) {
103
102
return Observable .error (e );
104
103
}
105
104
}
106
- }).enableWireLogging (LogLevel .DEBUG ).build ();
105
+ }).enableWireLogging (LogLevel .ERROR ).build ();
107
106
mockServer .start ();
108
107
}
109
108
@@ -121,10 +120,10 @@ public void testEndToEnd() throws Exception {
121
120
public Observable <HttpClientResponse <ByteBuf >> call (HttpClient <ByteBuf , ByteBuf > client ) {
122
121
return client .submit (HttpClientRequest .createGet ("/" ));
123
122
}
124
- }).enableWireLogging (LogLevel .ERROR ).build ().start ();
123
+ }).enableWireLogging (LogLevel .DEBUG ).build ().start ();
125
124
126
125
HttpClient <ByteBuf , ByteBuf > testClient = RxNetty .<ByteBuf , ByteBuf >newHttpClientBuilder ("localhost" , server .getServerPort ())
127
- .enableWireLogging (LogLevel .DEBUG ).build ();
126
+ .enableWireLogging (LogLevel .ERROR ).build ();
128
127
129
128
String reqId = "testE2E" ;
130
129
sendTestRequest (testClient , reqId );
@@ -184,7 +183,8 @@ public void testWithPooledConnections() throws Exception {
184
183
RxContexts .<ByteBuf , ByteBuf >newHttpClientBuilder ("localhost" , mockServer .getServerPort (),
185
184
REQUEST_ID_HEADER_NAME ,
186
185
RxContexts .DEFAULT_CORRELATOR )
187
- .withMaxConnections (1 ).withIdleConnectionsTimeoutMillis (100000 ).build ();
186
+ .withMaxConnections (1 ).enableWireLogging (LogLevel .ERROR )
187
+ .withIdleConnectionsTimeoutMillis (100000 ).build ();
188
188
ContextsContainer container = new ContextsContainerImpl (new MapBackedKeySupplier ());
189
189
container .addContext (CTX_1_NAME , CTX_1_VAL );
190
190
container .addContext (CTX_2_NAME , CTX_2_VAL , new TestContextSerializer ());
@@ -203,7 +203,8 @@ public void testNoStateLeakOnThreadReuse() throws Exception {
203
203
RxContexts .<ByteBuf , ByteBuf >newHttpClientBuilder ("localhost" , mockServer .getServerPort (),
204
204
REQUEST_ID_HEADER_NAME ,
205
205
RxContexts .DEFAULT_CORRELATOR )
206
- .withMaxConnections (1 ).withIdleConnectionsTimeoutMillis (100000 ).build ();
206
+ .withMaxConnections (1 ).enableWireLogging (LogLevel .ERROR )
207
+ .withIdleConnectionsTimeoutMillis (100000 ).build ();
207
208
208
209
ContextsContainer container = new ContextsContainerImpl (new MapBackedKeySupplier ());
209
210
container .addContext (CTX_1_NAME , CTX_1_VAL );
@@ -259,7 +260,7 @@ public Observable<Void> call(HttpClientResponse<ByteBuf> response) {
259
260
260
261
private static void invokeMockServer (HttpClient <ByteBuf , ByteBuf > testClient , final String requestId ,
261
262
boolean finishServerProcessing )
262
- throws MockBackendRequestFailedException , InterruptedException {
263
+ throws MockBackendRequestFailedException , InterruptedException , TimeoutException , ExecutionException {
263
264
try {
264
265
sendTestRequest (testClient , requestId );
265
266
} finally {
@@ -277,40 +278,24 @@ private static void invokeMockServer(HttpClient<ByteBuf, ByteBuf> testClient, fi
277
278
}
278
279
279
280
private static void sendTestRequest (HttpClient <ByteBuf , ByteBuf > testClient , final String requestId )
280
- throws MockBackendRequestFailedException , InterruptedException {
281
+ throws MockBackendRequestFailedException , InterruptedException , TimeoutException , ExecutionException {
281
282
System .err .println ("Sending test request to mock server, with request id: " + requestId );
282
283
RxContexts .DEFAULT_CORRELATOR .dumpThreadState (System .err );
283
- final CountDownLatch finishLatch = new CountDownLatch (1 );
284
- final List <HttpClientResponse <ByteBuf >> responseHolder = new ArrayList <HttpClientResponse <ByteBuf >>();
285
- testClient .submit (HttpClientRequest .createGet ("" ).withHeader (REQUEST_ID_HEADER_NAME , requestId ))
286
- .finallyDo (new Action0 () {
287
- @ Override
288
- public void call () {
289
- finishLatch .countDown ();
290
- }
291
- })
292
- .subscribe (new Action1 <HttpClientResponse <ByteBuf >>() {
293
- @ Override
294
- public void call (HttpClientResponse <ByteBuf > response ) {
295
- responseHolder .add (response );
296
- }
297
- });
298
-
299
- finishLatch .await (1 , TimeUnit .MINUTES );
300
- if (responseHolder .isEmpty ()) {
301
- throw new AssertionError ("Response not received." );
302
- }
303
284
304
- System .err .println ("Received response from mock server, with request id: " + requestId
305
- + ", status: " + responseHolder .get (0 ).getStatus ());
285
+ ResponseHolder <ByteBuf > responseHolder =
286
+ testClient .submit (HttpClientRequest .createGet ("" ).withHeader (REQUEST_ID_HEADER_NAME , requestId ))
287
+ .lift (FlatResponseOperator .<ByteBuf >flatResponse ())
288
+ .toBlocking ().toFuture ().get (1 , TimeUnit .MINUTES );
306
289
307
- HttpClientResponse <ByteBuf > response = responseHolder .get (0 );
290
+ System .err .println ("Received response from mock server, with request id: " + requestId
291
+ + ", status: " + responseHolder .getResponse ().getStatus ());
308
292
309
- if (response .getStatus ().code () != HttpResponseStatus .OK .code ()) {
310
- throw new MockBackendRequestFailedException ("Test request failed. Status: " + response .getStatus ().code ());
293
+ if (responseHolder .getResponse ().getStatus ().code () != HttpResponseStatus .OK .code ()) {
294
+ throw new MockBackendRequestFailedException ("Test request failed. Status: "
295
+ + responseHolder .getResponse ().getStatus ().code ());
311
296
}
312
297
313
- String requestIdGot = response .getHeaders ().get (REQUEST_ID_HEADER_NAME );
298
+ String requestIdGot = responseHolder . getResponse () .getHeaders ().get (REQUEST_ID_HEADER_NAME );
314
299
315
300
if (!requestId .equals (requestId )) {
316
301
throw new MockBackendRequestFailedException ("Request Id not sent from mock server. Expected: "
0 commit comments