Add expectation for amount of messages received in MinecraftToDiscordChatMessageTest

This commit is contained in:
Vankka 2023-10-15 23:44:42 +03:00
parent e0ec1aa31c
commit 3b02c662da
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0

View File

@ -35,7 +35,7 @@ public class MinecraftToDiscordChatMessageTest {
EventBus bus = discordSRV.eventBus();
String testMessage = UUID.randomUUID().toString();
CompletableFuture<Void> future = new CompletableFuture<>();
CompletableFuture<Integer> future = new CompletableFuture<>();
Listener listener = new Listener(testMessage, future);
bus.subscribe(listener);
@ -87,7 +87,13 @@ public class MinecraftToDiscordChatMessageTest {
));
try {
future.get(40, TimeUnit.SECONDS);
Integer amount = future.get(40, TimeUnit.SECONDS);
if (amount == null) {
Assertions.fail("Null amount returned by listener");
return;
}
Assertions.assertEquals(2, amount, "Amount of messages received from listener");
} catch (ExecutionException e) {
Assertions.fail(e.getCause());
} catch (TimeoutException e) {
@ -101,21 +107,23 @@ public class MinecraftToDiscordChatMessageTest {
public static class Listener {
private final String lookFor;
private final CompletableFuture<Void> success;
private final CompletableFuture<Integer> success;
public Listener(String lookFor, CompletableFuture<Void> success) {
public Listener(String lookFor, CompletableFuture<Integer> success) {
this.lookFor = lookFor;
this.success = success;
}
@Subscribe
public void onForwarded(GameChatMessageForwardedEvent event) {
int count = 0;
for (ReceivedDiscordMessage message : event.getDiscordMessage().getMessages()) {
String content = message.getContent();
if (content != null && content.contains(lookFor)) {
success.complete(null);
}
}
count++;
}
}
success.complete(count);
}
}
}