From a1a8c460e2f48d5afca7dba078e893c89d4d14e2 Mon Sep 17 00:00:00 2001 From: Javier Cardona Date: Tue, 21 Nov 2017 13:14:12 -0600 Subject: [PATCH] #336 Added shutdown call for execution service to default long polling bot --- .../telegrambots/bots/DefaultAbsSender.java | 2 +- .../bots/TelegramLongPollingBot.java | 6 +++ .../test/TelegramLongPollingBotTest.java | 47 ++++++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java index b592594b..4b3c9259 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/DefaultAbsSender.java @@ -51,7 +51,7 @@ import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT; 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; diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java index 50da8e71..8a3508de 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/bots/TelegramLongPollingBot.java @@ -55,4 +55,10 @@ public abstract class TelegramLongPollingBot extends DefaultAbsSender implements throw new TelegramApiRequestException("Error executing setWebook method", e); } } + + @Override + public void onClosing() { + exe.shutdown(); + } + } diff --git a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java index 23dbfc6d..992ee446 100644 --- a/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java +++ b/telegrambots/src/test/java/org/telegram/telegrambots/test/TelegramLongPollingBotTest.java @@ -3,11 +3,17 @@ package org.telegram.telegrambots.test; 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 { @@ -21,4 +27,41 @@ public class TelegramLongPollingBotTest { Mockito.verify(bot).onUpdateReceived(update1); Mockito.verify(bot).onUpdateReceived(update2); } -} \ No newline at end of file + + @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 ""; + } + } + +}