20
20
import com .ning .http .client .Response ;
21
21
import org .testng .annotations .Test ;
22
22
23
+ import java .util .concurrent .CountDownLatch ;
24
+ import java .util .concurrent .ExecutionException ;
23
25
import java .util .concurrent .Executors ;
24
- import java .util .concurrent .atomic .AtomicBoolean ;
26
+ import java .util .concurrent .Future ;
27
+ import java .util .concurrent .TimeUnit ;
28
+ import java .util .concurrent .atomic .AtomicInteger ;
25
29
26
30
import static org .testng .Assert .assertEquals ;
27
- import static org .testng .Assert .assertNotNull ;
28
- import static org .testng .Assert .assertTrue ;
29
31
30
32
/**
31
33
* Tests case where response doesn't have body.
@@ -36,19 +38,26 @@ public abstract class ListenableFutureTest extends AbstractBasicTest {
36
38
37
39
@ Test (groups = {"standalone" , "default_provider" })
38
40
public void testListenableFuture () throws Throwable {
39
- final AtomicBoolean executed = new AtomicBoolean ( false );
41
+ final AtomicInteger statusCode = new AtomicInteger ( 500 );
40
42
AsyncHttpClient ahc = getAsyncHttpClient (null );
41
- Response response = ((ListenableFuture <Response >)ahc .prepareGet (getTargetUrl ()).execute ()).addListener (new Runnable (){
42
-
43
+ final CountDownLatch latch = new CountDownLatch (1 );
44
+ final Future <Response > future = ((ListenableFuture <Response >)ahc .prepareGet (getTargetUrl ()).execute ());
45
+ ((ListenableFuture )future ).addListener (new Runnable (){
43
46
44
47
public void run () {
45
- executed .set (true );
48
+ try {
49
+ statusCode .set (future .get ().getStatusCode ());
50
+ latch .countDown ();
51
+ } catch (InterruptedException e ) {
52
+ e .printStackTrace ();
53
+ } catch (ExecutionException e ) {
54
+ e .printStackTrace ();
55
+ }
46
56
}
47
- }, Executors .newFixedThreadPool (1 )). get () ;
57
+ }, Executors .newFixedThreadPool (1 ));
48
58
49
- assertNotNull (response );
50
- assertEquals (response .getStatusCode (), 200 );
51
- assertTrue (executed .get ());
59
+ latch .await (10 , TimeUnit .SECONDS );
60
+ assertEquals (statusCode .get (), 200 );
52
61
ahc .close ();
53
62
}
54
63
}
0 commit comments