Skip to content

Commit

Permalink
rubenlagus#336 Added shutdown call for execution service to default l…
Browse files Browse the repository at this point in the history
…ong polling bot
  • Loading branch information
javver committed Nov 21, 2017
1 parent 49c0fef commit a1a8c46
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
public abstract class DefaultAbsSender extends AbsSender {
private static final ContentType TEXT_PLAIN_CONTENT_TYPE = ContentType.create("text/plain", StandardCharsets.UTF_8);

private final ExecutorService exe;
protected final ExecutorService exe;
private final ObjectMapper objectMapper = new ObjectMapper();
private final DefaultBotOptions options;
private volatile CloseableHttpClient httpclient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,10 @@ public void clearWebhook() throws TelegramApiRequestException {
throw new TelegramApiRequestException("Error executing setWebook method", e);
}
}

@Override
public void onClosing() {
exe.shutdown();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import org.junit.Test;
import org.mockito.Mockito;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyList;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

public class TelegramLongPollingBotTest {

Expand All @@ -21,4 +27,41 @@ public void testOnUpdateReceived() throws Exception {
Mockito.verify(bot).onUpdateReceived(update1);
Mockito.verify(bot).onUpdateReceived(update2);
}
}

@Test
public void testExecutorShutdown() throws Exception {
TestBot bot = Mockito.spy(new TestBot());
DefaultBotSession session = new DefaultBotSession();
session.setCallback(bot);
session.setOptions(new DefaultBotOptions());
session.start();
session.stop();
Mockito.verify(bot).onClosing();
ExecutorService executor = bot.getExecutor();
assertThat("Executor was not shut down", executor.isShutdown(), is(true));
executor.awaitTermination(1, TimeUnit.SECONDS);
assertThat("Executor could not terminate", executor.isTerminated(), is(true));
}

private static class TestBot extends TelegramLongPollingBot {

@Override
public void onUpdateReceived(Update update) {
}

ExecutorService getExecutor() {
return exe;
}

@Override
public String getBotUsername() {
return "";
}

@Override
public String getBotToken() {
return "";
}
}

}

0 comments on commit a1a8c46

Please sign in to comment.