Skip to content

Commit

Permalink
Make the same change to chat.postMessage with #1274 (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch authored Jan 25, 2024
1 parent 7c838c1 commit 5f4139e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.slack.api.methods.impl;

import com.slack.api.methods.Methods;
import com.slack.api.methods.MethodsConfig;
import com.slack.api.methods.MethodsCustomRateLimitResolver;
import com.slack.api.methods.MethodsRateLimits;
Expand Down Expand Up @@ -66,6 +67,14 @@ public int getAllowedRequestsForChatPostMessagePerMinute(String teamId, String c

@Override
public WaitTime acquireWaitTimeForChatPostMessage(String teamId, String channel) {
// See MethodsClientImpl#buildMethodNameAndSuffix() for the consistency of this logic
String methodName = Methods.CHAT_POST_MESSAGE + "_" + channel;
Optional<Long> rateLimitedEpochMillis = waitTimeCalculator
.getRateLimitedMethodRetryEpochMillis(executorName, teamId, methodName);
if (rateLimitedEpochMillis.isPresent()) {
long millisToWait = rateLimitedEpochMillis.get() - System.currentTimeMillis();
return new WaitTime(millisToWait, RequestPace.RateLimited);
}
return waitTimeCalculator.calculateWaitTimeForChatPostMessage(
teamId,
channel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.slack.api.Slack;
import com.slack.api.SlackConfig;
import com.slack.api.methods.Methods;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.SlackApiException;
import com.slack.api.rate_limits.metrics.MetricsDatastore;
Expand All @@ -22,17 +23,20 @@
public class RateLimitedTest {

MockSlackApiServer server = new MockSlackApiServer();
SlackConfig config = new SlackConfig();
Slack slack = Slack.getInstance(config);

String executorName = RateLimitedTest.class.getCanonicalName();
String executorName;
SlackConfig config;
Slack slack;

@Before
public void setup() throws Exception {
server.start();
executorName = RateLimitedTest.class.getCanonicalName() + "_" + System.currentTimeMillis();
config = new SlackConfig();
config.getMethodsConfig().setExecutorName(executorName);
config.synchronizeMetricsDatabases();
config.setMethodsEndpointUrlPrefix(server.getMethodsEndpointPrefix());

slack = Slack.getInstance(config);
}

@After
Expand All @@ -54,13 +58,13 @@ public void sync() throws Exception {
Integer numOfRequests = datastore.getNumberOfLastMinuteRequests(
executorName,
"T1234567",
"users.list");
Methods.USERS_LIST);
assertThat(numOfRequests, is(1));

Long millisToResume = datastore.getRateLimitedMethodRetryEpochMillis(
executorName,
"T1234567",
"users.list");
Methods.USERS_LIST);
assertThat(millisToResume, is(greaterThan(0L)));
}
}
Expand All @@ -76,15 +80,63 @@ public void async() throws Exception {
Integer numOfRequests = datastore.getNumberOfLastMinuteRequests(
executorName,
"T1234567",
"users.list");
Methods.USERS_LIST);
assertThat(numOfRequests, is(1));

Long millisToResume = datastore.getRateLimitedMethodRetryEpochMillis(
executorName,
"T1234567",
Methods.USERS_LIST);
assertThat(millisToResume, is(greaterThan(0L)));
}
}

@Test
public void chatPostMessageSync() throws Exception {
MethodsClient client = slack.methods(RateLimitedToken);
try {
client.chatPostMessage(r -> r.text("Hey!").channel("C12345"));
} catch (SlackApiException e) {
assertThat(e.getResponse().code(), is(429));
assertThat(e.getResponse().header("Retry-After"), is("3"));
MetricsDatastore datastore = config.getMethodsConfig().getMetricsDatastore();
log.debug("stats: {}", datastore.getAllStats());

Integer numOfRequests = datastore.getNumberOfLastMinuteRequests(
executorName,
"T1234567",
Methods.CHAT_POST_MESSAGE + "_C12345");
assertThat(numOfRequests, is(1));

Long millisToResume = datastore.getRateLimitedMethodRetryEpochMillis(
executorName,
"T1234567",
"users.list");
Methods.CHAT_POST_MESSAGE + "_C12345");
assertThat(millisToResume, is(greaterThan(0L)));
}
}

@Test
public void chatPostMessageAsync() throws Exception {
try {
slack.methodsAsync(RateLimitedToken)
.chatPostMessage(r -> r.text("Hey!").channel("C12345"))
.get(2, TimeUnit.SECONDS);
} catch (Exception ee) {
MetricsDatastore datastore = config.getMethodsConfig().getMetricsDatastore();
log.debug("stats: {}", datastore.getAllStats());

Integer numOfRequests = datastore.getNumberOfLastMinuteRequests(
executorName,
"T1234567",
Methods.CHAT_POST_MESSAGE + "_C12345");
assertThat(numOfRequests, is(1));

Long millisToResume = datastore.getRateLimitedMethodRetryEpochMillis(
executorName,
"T1234567",
Methods.CHAT_POST_MESSAGE + "_C12345");
assertThat(millisToResume, is(greaterThan(0L)));
}
}
}

0 comments on commit 5f4139e

Please sign in to comment.