1313import com .slack .api .model .event .MessageBotEvent ;
1414import com .slack .api .model .event .PongEvent ;
1515import com .slack .api .model .event .UserTypingEvent ;
16- import com .slack .api .rtm .RTMClient ;
17- import com .slack .api .rtm .RTMEventHandler ;
18- import com .slack .api .rtm .RTMEventsDispatcher ;
19- import com .slack .api .rtm .RTMEventsDispatcherFactory ;
20- import com .slack .api .rtm .RTMMessageHandler ;
21- import com .slack .api .rtm .message .Message ;
22- import com .slack .api .rtm .message .PingMessage ;
23- import com .slack .api .rtm .message .PresenceQuery ;
24- import com .slack .api .rtm .message .PresenceSub ;
25- import com .slack .api .rtm .message .Typing ;
16+ import com .slack .api .rtm .*;
17+ import com .slack .api .rtm .message .*;
2618import config .Constants ;
2719import config .SlackTestConfig ;
2820import lombok .extern .slf4j .Slf4j ;
4133import java .util .Objects ;
4234import java .util .concurrent .atomic .AtomicInteger ;
4335
44- import static org .hamcrest .CoreMatchers .equalTo ;
45- import static org .hamcrest .CoreMatchers .is ;
46- import static org .hamcrest .CoreMatchers .notNullValue ;
47- import static org .hamcrest .CoreMatchers .nullValue ;
36+ import static org .hamcrest .CoreMatchers .*;
4837import static org .hamcrest .MatcherAssert .assertThat ;
4938import static org .hamcrest .Matchers .greaterThanOrEqualTo ;
5039
@@ -314,7 +303,7 @@ public void handle(String message) {
314303
315304
316305 @ Test
317- public void givenRTMClient_whenPing_ensureReceivesPong () throws Exception {
306+ public void ping_pong () throws Exception {
318307
319308 // given
320309 SlackConfig config = new SlackConfig ();
@@ -325,36 +314,41 @@ public void givenRTMClient_whenPing_ensureReceivesPong() throws Exception {
325314 final Instant now = Instant .now ();
326315 final long pingId = now .toEpochMilli ();
327316
328- class PongReceived { PongEvent event = null ; }
317+ class PongReceived {
318+ PongEvent event = null ;
319+ }
329320 final PongReceived pongReceived = new PongReceived ();
330321
331322 RTMEventsDispatcher dispatcher = RTMEventsDispatcherFactory .getInstance ();
332323 dispatcher .register (new RTMEventHandler <PongEvent >() {
333- @ Override public void handle (PongEvent event ) {
324+ @ Override
325+ public void handle (PongEvent event ) {
334326 if (Objects .equals (event .getReplyTo (), pingId )) {
335- synchronized (pongReceived ) {
336- pongReceived .event = event ;
337- pongReceived .notifyAll ();
338- }
327+ pongReceived .event = event ;
328+ pongReceived .notifyAll ();
339329 }
340330 }
341331 });
342332
343333 try (RTMClient rtm = slack .rtmStart (classicAppBotToken )) {
344- rtm .connect ();
345334 rtm .addMessageHandler (dispatcher .toMessageHandler ());
335+ rtm .connect ();
336+
337+ Thread .sleep (3000 );
346338
347339 // when
348340 rtm .sendMessage (PingMessage .builder ().id (pingId ).time (now ).build ().toJSONString ());
349341
350342 // ensure
351- synchronized (pongReceived ) {
352- pongReceived .wait (5000L );
343+ long millis = 0L ;
344+ while (pongReceived .event == null && millis < 30_000L ) {
345+ Thread .sleep (1000L );
346+ millis += 1000 ;
353347 }
354- assertThat (pongReceived .event , notNullValue ());
355- assertThat (pongReceived .event .getReplyTo (), equalTo (pingId ));
356- assertThat (pongReceived .event .getTime (), equalTo (now ));
357348 }
349+ assertThat (pongReceived .event , notNullValue ());
350+ assertThat (pongReceived .event .getReplyTo (), equalTo (pingId ));
351+ assertThat (pongReceived .event .getTime (), equalTo (now ));
358352
359353 }
360354}
0 commit comments