Skip to content

Commit f165b62

Browse files
Fix tests background errors (#403)
1 parent 3ed8921 commit f165b62

File tree

4 files changed

+200
-162
lines changed

4 files changed

+200
-162
lines changed

src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@
4141
import java.util.List;
4242
import java.util.Set;
4343
import java.util.concurrent.Executor;
44+
import java.util.concurrent.ExecutorService;
4445
import java.util.concurrent.ThreadFactory;
4546

4647
/**
4748
* Telegram bot to convert medias in the format required to be used as Telegram stickers.
4849
*
4950
* @author Roberto Cella
5051
*/
51-
public record Stickerify(TelegramBot bot, Executor executor) implements UpdatesListener, ExceptionHandler {
52+
public record Stickerify(TelegramBot bot, Executor executor) implements UpdatesListener, ExceptionHandler, AutoCloseable {
5253

5354
private static final Logger LOGGER = LoggerFactory.getLogger(Stickerify.class);
5455
private static final String BOT_TOKEN = System.getenv("STICKERIFY_TOKEN");
@@ -91,6 +92,17 @@ public void onException(TelegramException e) {
9192
LOGGER.atError().log("There was an unexpected failure: {}", e.getMessage());
9293
}
9394

95+
@Override
96+
public void close() {
97+
bot.removeGetUpdatesListener();
98+
99+
if (executor instanceof ExecutorService es) {
100+
es.close();
101+
}
102+
103+
bot.shutdown();
104+
}
105+
94106
private void answer(TelegramRequest request) {
95107
var file = request.getFile();
96108

src/main/java/com/github/stickerifier/stickerify/runner/Main.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@
33
import com.github.stickerifier.stickerify.bot.Stickerify;
44

55
public class Main {
6+
static final Object LOCK = new Object();
7+
68
public static void main(String[] args) {
7-
new Stickerify();
9+
try (var _ = new Stickerify()) {
10+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
11+
synchronized (LOCK) {
12+
LOCK.notifyAll();
13+
}
14+
}));
15+
16+
synchronized (LOCK) {
17+
LOCK.wait();
18+
}
19+
} catch (InterruptedException e) {
20+
Thread.currentThread().interrupt();
21+
}
822
}
923
}

src/test/java/com/github/stickerifier/stickerify/bot/MockResponses.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.github.stickerifier.stickerify.bot;
22

3+
import com.github.stickerifier.stickerify.ResourceHelper;
34
import mockwebserver3.MockResponse;
45
import okio.Buffer;
56
import okio.Okio;
67

7-
import java.io.File;
8-
98
public final class MockResponses {
109

10+
static final MockResponse EMPTY_UPDATES = new MockResponse.Builder().body("""
11+
{
12+
ok: true
13+
}
14+
""").build();
15+
1116
static final MockResponse START_MESSAGE = new MockResponse.Builder().body("""
1217
{
1318
ok: true,
@@ -314,7 +319,7 @@ public final class MockResponses {
314319
}
315320
""").build();
316321

317-
static MockResponse fileInfo(String id) {
322+
static MockResponse fileInfo(String fileName) {
318323
return new MockResponse.Builder().body("""
319324
{
320325
ok: true,
@@ -323,10 +328,11 @@ static MockResponse fileInfo(String id) {
323328
file_path: "%1$s"
324329
}
325330
}
326-
""".formatted(id)).build();
331+
""".formatted(fileName)).build();
327332
}
328333

329-
static MockResponse fileDownload(File file) throws Exception {
334+
static MockResponse fileDownload(String fileName) throws Exception {
335+
var file = ResourceHelper.loadResource(fileName);
330336
try (var buffer = new Buffer(); var source = Okio.source(file)) {
331337
buffer.writeAll(source);
332338
return new MockResponse.Builder().body(buffer).build();

0 commit comments

Comments
 (0)