25
25
26
26
public class ListenableFutureTest extends AbstractBasicTest {
27
27
28
- @ Test ( groups = "standalone" )
28
+ @ Test
29
29
public void testListenableFuture () throws Exception {
30
30
final AtomicInteger statusCode = new AtomicInteger (500 );
31
31
try (AsyncHttpClient ahc = asyncHttpClient ()) {
32
32
final CountDownLatch latch = new CountDownLatch (1 );
33
33
final ListenableFuture <Response > future = ahc .prepareGet (getTargetUrl ()).execute ();
34
- future .addListener (new Runnable () {
35
-
36
- public void run () {
37
- try {
38
- statusCode .set (future .get ().getStatusCode ());
39
- latch .countDown ();
40
- } catch (InterruptedException e ) {
41
- e .printStackTrace ();
42
- } catch (ExecutionException e ) {
43
- e .printStackTrace ();
44
- }
34
+ future .addListener (() -> {
35
+ try {
36
+ statusCode .set (future .get ().getStatusCode ());
37
+ latch .countDown ();
38
+ } catch (InterruptedException e ) {
39
+ e .printStackTrace ();
40
+ } catch (ExecutionException e ) {
41
+ e .printStackTrace ();
45
42
}
46
43
}, Executors .newFixedThreadPool (1 ));
47
44
@@ -50,32 +47,32 @@ public void run() {
50
47
}
51
48
}
52
49
53
- @ Test ( groups = "standalone" )
50
+ @ Test
54
51
public void testListenableFutureAfterCompletion () throws Exception {
55
52
56
- AtomicInteger counter = new AtomicInteger (1 );
53
+ final CountDownLatch latch = new CountDownLatch (1 );
57
54
58
55
try (AsyncHttpClient ahc = asyncHttpClient ()) {
59
56
final ListenableFuture <Response > future = ahc .prepareGet (getTargetUrl ()).execute ();
60
57
future .get ();
61
- future .addListener (() -> counter . decrementAndGet (), Runnable ::run );
58
+ future .addListener (() -> latch . countDown (), Runnable ::run );
62
59
}
63
- assertEquals (counter .get (), 0 );
60
+
61
+ latch .await (10 , TimeUnit .SECONDS );
64
62
}
65
63
66
- @ Test ( groups = "standalone" )
64
+ @ Test
67
65
public void testListenableFutureBeforeAndAfterCompletion () throws Exception {
68
66
69
- AtomicInteger counter = new AtomicInteger (2 );
67
+ final CountDownLatch latch = new CountDownLatch (2 );
70
68
71
69
try (AsyncHttpClient ahc = asyncHttpClient ()) {
72
70
final ListenableFuture <Response > future = ahc .prepareGet (getTargetUrl ()).execute ();
73
-
74
- future .addListener (() -> counter .decrementAndGet (), Runnable ::run );
75
-
71
+ future .addListener (() -> latch .countDown (), Runnable ::run );
76
72
future .get ();
77
- future .addListener (() -> counter . decrementAndGet (), Runnable ::run );
73
+ future .addListener (() -> latch . countDown (), Runnable ::run );
78
74
}
79
- assertEquals (counter .get (), 0 );
75
+
76
+ latch .await (10 , TimeUnit .SECONDS );
80
77
}
81
78
}
0 commit comments