Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ping message and pong event to RTM client #459

Merged
merged 5 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.slack.api.rtm.message;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PingMessage implements RTMMessage {

public static final String TYPE_NAME = "ping";

private Long id;
private final String type = TYPE_NAME;
private Instant time;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
import com.slack.api.model.User;
import com.slack.api.model.event.HelloEvent;
import com.slack.api.model.event.MessageBotEvent;
import com.slack.api.model.event.PongEvent;
import com.slack.api.model.event.UserTypingEvent;
import com.slack.api.rtm.*;
import com.slack.api.rtm.RTMClient;
import com.slack.api.rtm.RTMEventHandler;
import com.slack.api.rtm.RTMEventsDispatcher;
import com.slack.api.rtm.RTMEventsDispatcherFactory;
import com.slack.api.rtm.RTMMessageHandler;
import com.slack.api.rtm.message.Message;
import com.slack.api.rtm.message.PingMessage;
import com.slack.api.rtm.message.PresenceQuery;
import com.slack.api.rtm.message.PresenceSub;
import com.slack.api.rtm.message.Typing;
Expand All @@ -30,10 +36,15 @@
import javax.websocket.DeploymentException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

Expand Down Expand Up @@ -301,4 +312,49 @@ public void handle(String message) {
assertThat(counter.get(), is(greaterThanOrEqualTo(5)));
}


@Test
public void givenRTMClient_whenPing_ensureReceivesPong() throws Exception {

// given
SlackConfig config = new SlackConfig();
config.setLibraryMaintainerMode(false);
config.getHttpClientResponseHandlers().add(new JsonDataRecordingListener());
Slack slack = Slack.getInstance(config);

final Instant now = Instant.now();
final long pingId = now.toEpochMilli();

class PongReceived { PongEvent event = null; }
final PongReceived pongReceived = new PongReceived();

RTMEventsDispatcher dispatcher = RTMEventsDispatcherFactory.getInstance();
dispatcher.register(new RTMEventHandler<PongEvent>() {
@Override public void handle(PongEvent event) {
if (Objects.equals(event.getReplyTo(), pingId)) {
synchronized(pongReceived) {
pongReceived.event = event;
pongReceived.notifyAll();
}
}
}
});

try (RTMClient rtm = slack.rtmStart(classicAppBotToken)) {
rtm.connect();
rtm.addMessageHandler(dispatcher.toMessageHandler());

// when
rtm.sendMessage(PingMessage.builder().id(pingId).time(now).build().toJSONString());

// ensure
synchronized(pongReceived) {
pongReceived.wait(5000L);
}
assertThat(pongReceived.event, notNullValue());
assertThat(pongReceived.event.getReplyTo(), equalTo(pingId));
assertThat(pongReceived.event.getTime(), equalTo(now));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.slack.api.model.event;

import lombok.Data;

import java.time.Instant;

/**
* The pong event is sent in response to a 'ping' message previously sent. The id and
* other fields will match that of the ping message.
* <p>
* https://api.slack.com/rtm
*/
@Data
public class PongEvent implements Event
{
public static final String TYPE_NAME = "pong";
private final String type = TYPE_NAME;

private Long replyTo;
private Instant time;
}