Skip to content

Commit

Permalink
Fix TooManyRequests in setMyName test
Browse files Browse the repository at this point in the history
  • Loading branch information
pengrad committed Jan 22, 2024
1 parent badcd17 commit fae2162
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public SetMyName() {
/**
*
* @param name New bot name; 0-64 characters. Pass an empty string to remove the dedicated name for the given language.
* @return
*/
public SetMyName name(String name) {
add("name", name);
Expand All @@ -21,7 +20,6 @@ public SetMyName name(String name) {
/**
*
* @param languageCode A two-letter ISO 639-1 language code. If empty, the name will be shown to all users for whose language there is no dedicated name.
* @return
*/
public SetMyName languageCode(String languageCode) {
add("language_code", languageCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class RetryInterceptor implements Interceptor {

private final int defaultSleepMillis;
private final Gson gson = new Gson();
private boolean enabled = true;

public RetryInterceptor() {
this(1000);
Expand All @@ -25,8 +26,13 @@ public RetryInterceptor(int defaultSleepMillis) {
this.defaultSleepMillis = defaultSleepMillis;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

@Override
public Response intercept(Chain chain) throws IOException {
if (!enabled) return chain.proceed(chain.request());
Request request = chain.request();
Exception exception = null;
int retries = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ public static TelegramBot createTestBot() {
.connectTimeout(75, TimeUnit.SECONDS)
.writeTimeout(75, TimeUnit.SECONDS)
.readTimeout(75, TimeUnit.SECONDS)
.addInterceptor(new RetryInterceptor(1000));
.addInterceptor(retry);
if (localBuild) {
okHttpBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
}
return new TelegramBot.Builder(token).okHttpClient(okHttpBuilder.build()).build();
}

static RetryInterceptor retry = new RetryInterceptor(1000);
static TelegramBot bot = createTestBot();
static Long chatId;
static Long groupId;
Expand Down Expand Up @@ -2352,8 +2353,13 @@ public void setMyShortDescription() {

@Test
public void setMyName() {
retry.setEnabled(false);
BaseResponse response = bot.execute(new SetMyName().name("name").languageCode("en"));
assertTrue(response.isOk());
if (!response.isOk()) {
assertEquals(429, response.errorCode());
assertTrue(response.description().startsWith("Too Many Requests: retry after"));
}
retry.setEnabled(true);

GetMyNameResponse nameResponse = bot.execute(new GetMyName().languageCode("en"));
assertTrue(nameResponse.isOk());
Expand Down

0 comments on commit fae2162

Please sign in to comment.