11package controllers ;
22
3+ import static java .time .temporal .ChronoUnit .SECONDS ;
4+ import static org .junit .Assert .assertEquals ;
5+ import static play .mvc .Http .Status .SERVICE_UNAVAILABLE ;
6+
7+ import java .time .Duration ;
8+ import java .util .OptionalInt ;
9+ import java .util .concurrent .CompletionStage ;
10+ import java .util .concurrent .CountDownLatch ;
311import java .util .concurrent .ExecutionException ;
12+ import java .util .concurrent .TimeUnit ;
13+ import java .util .concurrent .TimeoutException ;
14+ import java .util .concurrent .atomic .AtomicInteger ;
15+ import java .util .stream .IntStream ;
416import org .junit .Before ;
517import org .junit .Test ;
618import org .slf4j .Logger ;
1527import play .mvc .Results ;
1628import play .test .WithServer ;
1729
18- import java .time .Duration ;
19- import java .util .OptionalInt ;
20- import java .util .concurrent .CompletionStage ;
21- import java .util .concurrent .TimeUnit ;
22- import java .util .concurrent .TimeoutException ;
23- import java .util .concurrent .atomic .AtomicInteger ;
24- import java .util .stream .IntStream ;
25-
26- import static java .time .temporal .ChronoUnit .SECONDS ;
27- import static org .junit .Assert .assertEquals ;
28- import static play .mvc .Http .Status .SERVICE_UNAVAILABLE ;
29-
30- import play .mvc .Http .MultipartFormData .*;
31- import akka .stream .javadsl .*;
32- import akka .util .ByteString ;
33-
3430public class HomeControllerTest extends WithServer {
3531
3632 private final Logger log = LoggerFactory .getLogger (HomeControllerTest .class );
@@ -49,15 +45,15 @@ public void setup() {
4945 port = optHttpsPort .getAsInt ();
5046 url = "https://localhost:" + port ;
5147 } else {
52- port = testServer .getRunningHttpPort ().getAsInt ();
48+ port = testServer .getRunningHttpPort ()
49+ .getAsInt ();
5350 url = "http://localhost:" + port ;
5451 }
5552 }
5653
5754 @ Test
5855 public void givenASingleGetRequestWhenResponseThenBlockWithCompletableAndLog ()
5956 throws InterruptedException , ExecutionException {
60- AtomicInteger completedReqs = new AtomicInteger (0 );
6157 WSClient ws = play .test .WSTestClient .newClient (port );
6258 WSResponse wsResponse = ws .url (url )
6359 .setRequestFilter (new AhcCurlRequestLogger ())
@@ -75,8 +71,10 @@ public void givenASingleGetRequestWhenResponseThenBlockWithCompletableAndLog()
7571 }
7672
7773 @ Test
78- public void givenASingleGetRequestWhenResponseThenLog () throws InterruptedException {
74+ public void givenASingleGetRequestWhenResponseThenLog () throws Exception {
7975 AtomicInteger completedReqs = new AtomicInteger (0 );
76+ CountDownLatch latch = new CountDownLatch (1 );
77+
8078 WSClient ws = play .test .WSTestClient .newClient (port );
8179 ws .url (url )
8280 .setRequestFilter (new AhcCurlRequestLogger ())
@@ -88,20 +86,22 @@ public void givenASingleGetRequestWhenResponseThenLog() throws InterruptedExcept
8886 .getId () + " Request complete: Response code = "
8987 + r .getStatus ()
9088 + " | Response: " + r .getBody () + " | Current Time:" + System .currentTimeMillis ());
89+ latch .countDown ();
9190 completedReqs .incrementAndGet ();
9291 });
9392
9493 log .debug (
9594 "Waiting for requests to be completed. Current Time: " + System .currentTimeMillis ());
96- while (completedReqs .get () != 1 ) {
97- Thread .sleep (100 );
98- }
99- log .debug ("All requests have been completed. Exiting test." );
95+ latch .await ();
96+ assertEquals (1 , completedReqs .get ());
97+ log .debug ("All requests have1 been completed. Exiting test." );
10098 }
10199
102100 @ Test
103- public void givenASinglePostRequestWhenResponseThenLog () throws InterruptedException {
101+ public void givenASinglePostRequestWhenResponseThenLog () throws Exception {
104102 AtomicInteger completedReqs = new AtomicInteger (0 );
103+ CountDownLatch latch = new CountDownLatch (1 );
104+
105105 WSClient ws = play .test .WSTestClient .newClient (port );
106106 ws .url (url )
107107 .setContentType ("application/x-www-form-urlencoded" )
@@ -111,20 +111,22 @@ public void givenASinglePostRequestWhenResponseThenLog() throws InterruptedExcep
111111 .getId () + " Request complete: Response code = "
112112 + r .getStatus ()
113113 + " | Response: " + r .getBody () + " | Current Time:" + System .currentTimeMillis ());
114+ latch .countDown ();
114115 completedReqs .incrementAndGet ();
115116 });
116117
117118 log .debug (
118119 "Waiting for requests to be completed. Current Time: " + System .currentTimeMillis ());
119- while (completedReqs .get () != 1 ) {
120- Thread .sleep (100 );
121- }
120+ latch .await ();
121+ assertEquals (1 , completedReqs .get ());
122122 log .debug ("All requests have been completed. Exiting test." );
123123 }
124124
125125 @ Test
126- public void givenMultipleRequestsWhenResponseThenLog () throws InterruptedException {
126+ public void givenMultipleRequestsWhenResponseThenLog () throws Exception {
127127 AtomicInteger completedReqs = new AtomicInteger (0 );
128+ CountDownLatch latch = new CountDownLatch (100 );
129+
128130 WSClient ws = play .test .WSTestClient .newClient (port );
129131 IntStream .range (0 , 100 )
130132 .parallel ()
@@ -139,46 +141,58 @@ public void givenMultipleRequestsWhenResponseThenLog() throws InterruptedExcepti
139141 "Thread#" + num + " Request complete: Response code = " + r .getStatus ()
140142 + " | Response: " + r .getBody () + " | Current Time:"
141143 + System .currentTimeMillis ());
144+ latch .countDown ();
142145 completedReqs .incrementAndGet ();
143146 })
144147 );
145148
146149 log .debug (
147150 "Waiting for requests to be completed. Current Time: " + System .currentTimeMillis ());
148- while (completedReqs .get () != 100 ) {
149- Thread .sleep (100 );
150- }
151+ latch .await ();
152+ assertEquals (100 , completedReqs .get ());
151153 log .debug ("All requests have been completed. Exiting test." );
152154 }
153155
154156 @ Test
155- public void givenLongResponseWhenTimeoutThenHandle () throws InterruptedException {
157+ public void givenLongResponseWhenTimeoutThenHandle () throws Exception {
158+ AtomicInteger completedReqs = new AtomicInteger (0 );
159+ CountDownLatch latch = new CountDownLatch (1 );
160+
156161 WSClient ws = play .test .WSTestClient .newClient (port );
157162 Futures futures = app .injector ()
158163 .instanceOf (Futures .class );
159- CompletionStage <Result > f = futures .timeout (ws .url (url )
160- .setRequestTimeout (Duration .of (1 , SECONDS ))
161- .get ()
162- .thenApply (result -> {
163- try {
164- Thread .sleep (2000L );
165- return Results .ok ();
166- } catch (InterruptedException e ) {
167- return Results .status (
168- SERVICE_UNAVAILABLE );
169- }
170- }), 1L , TimeUnit .SECONDS );
164+ CompletionStage <Result > f = futures .timeout (
165+ ws .url (url )
166+ .setRequestTimeout (Duration .of (1 , SECONDS ))
167+ .get ()
168+ .thenApply (result -> {
169+ try {
170+ Thread .sleep (2000L );
171+ return Results .ok ();
172+ } catch (InterruptedException e ) {
173+ return Results .status (
174+ SERVICE_UNAVAILABLE );
175+ }
176+ }), 1L , TimeUnit .SECONDS
177+ );
171178 CompletionStage <Object > res = f .handleAsync ((result , e ) -> {
172179 if (e != null ) {
173180 log .error ("Exception thrown" , e );
181+ completedReqs .incrementAndGet ();
182+ latch .countDown ();
174183 return e .getCause ();
175184 } else {
176185 return result ;
177186 }
178187 });
179- res .thenAccept (result ->
180- assertEquals (TimeoutException .class , result )
181- );
182- Thread .sleep (2500 );
188+ res .thenAccept (result -> {
189+ assertEquals (TimeoutException .class , result );
190+ });
191+
192+ log .debug (
193+ "Waiting for requests to be completed. Current Time: " + System .currentTimeMillis ());
194+ latch .await ();
195+ assertEquals (1 , completedReqs .get ());
196+ log .debug ("All requests have been completed. Exiting test." );
183197 }
184198}
0 commit comments