#336 Added shutdown call for execution service to default long polling bot
This commit is contained in:
parent
49c0fef3c5
commit
a1a8c460e2
@ -51,7 +51,7 @@ import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
|
|||||||
public abstract class DefaultAbsSender extends AbsSender {
|
public abstract class DefaultAbsSender extends AbsSender {
|
||||||
private static final ContentType TEXT_PLAIN_CONTENT_TYPE = ContentType.create("text/plain", StandardCharsets.UTF_8);
|
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 ObjectMapper objectMapper = new ObjectMapper();
|
||||||
private final DefaultBotOptions options;
|
private final DefaultBotOptions options;
|
||||||
private volatile CloseableHttpClient httpclient;
|
private volatile CloseableHttpClient httpclient;
|
||||||
|
@ -55,4 +55,10 @@ public abstract class TelegramLongPollingBot extends DefaultAbsSender implements
|
|||||||
throw new TelegramApiRequestException("Error executing setWebook method", e);
|
throw new TelegramApiRequestException("Error executing setWebook method", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClosing() {
|
||||||
|
exe.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,11 +3,17 @@ package org.telegram.telegrambots.test;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.telegram.telegrambots.api.objects.Update;
|
import org.telegram.telegrambots.api.objects.Update;
|
||||||
|
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||||
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
||||||
|
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
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.any;
|
||||||
import static org.mockito.Matchers.anyList;
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class TelegramLongPollingBotTest {
|
public class TelegramLongPollingBotTest {
|
||||||
|
|
||||||
@ -21,4 +27,41 @@ public class TelegramLongPollingBotTest {
|
|||||||
Mockito.verify(bot).onUpdateReceived(update1);
|
Mockito.verify(bot).onUpdateReceived(update1);
|
||||||
Mockito.verify(bot).onUpdateReceived(update2);
|
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 "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user