-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #1273 AsyncMethodsRateLimiter does not handle ratelimitted errors…
… properly (#1274)
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
slack-api-client/src/test/java/test_locally/api/methods/RateLimitedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package test_locally.api.methods; | ||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.SlackConfig; | ||
import com.slack.api.methods.MethodsClient; | ||
import com.slack.api.methods.SlackApiException; | ||
import com.slack.api.rate_limits.metrics.MetricsDatastore; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import util.MockSlackApiServer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static util.MockSlackApi.RateLimitedToken; | ||
|
||
@Slf4j | ||
public class RateLimitedTest { | ||
|
||
MockSlackApiServer server = new MockSlackApiServer(); | ||
SlackConfig config = new SlackConfig(); | ||
Slack slack = Slack.getInstance(config); | ||
|
||
String executorName = RateLimitedTest.class.getCanonicalName(); | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
server.start(); | ||
config.getMethodsConfig().setExecutorName(executorName); | ||
config.synchronizeMetricsDatabases(); | ||
config.setMethodsEndpointUrlPrefix(server.getMethodsEndpointPrefix()); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
server.stop(); | ||
} | ||
|
||
@Test | ||
public void sync() throws Exception { | ||
MethodsClient client = slack.methods(RateLimitedToken); | ||
try { | ||
client.usersList(r -> r); | ||
} 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", | ||
"users.list"); | ||
assertThat(numOfRequests, is(1)); | ||
|
||
Long millisToResume = datastore.getRateLimitedMethodRetryEpochMillis( | ||
executorName, | ||
"T1234567", | ||
"users.list"); | ||
assertThat(millisToResume, is(greaterThan(0L))); | ||
} | ||
} | ||
|
||
@Test | ||
public void async() throws Exception { | ||
try { | ||
slack.methodsAsync(RateLimitedToken).usersList(r -> r).get(2, TimeUnit.SECONDS); | ||
} catch (Exception ee) { | ||
MetricsDatastore datastore = config.getMethodsConfig().getMetricsDatastore(); | ||
log.debug("stats: {}", datastore.getAllStats()); | ||
|
||
Integer numOfRequests = datastore.getNumberOfLastMinuteRequests( | ||
executorName, | ||
"T1234567", | ||
"users.list"); | ||
assertThat(numOfRequests, is(1)); | ||
|
||
Long millisToResume = datastore.getRateLimitedMethodRetryEpochMillis( | ||
executorName, | ||
"T1234567", | ||
"users.list"); | ||
assertThat(millisToResume, is(greaterThan(0L))); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters