Make MockDiscordSRV initialization failures log better

This commit is contained in:
Vankka 2024-12-20 23:04:36 +02:00
parent bb3c4987bc
commit f4368a15b8
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
8 changed files with 36 additions and 15 deletions

View File

@ -46,8 +46,8 @@ public class FullBootExtension implements BeforeAllCallback, ExtensionContext.St
try { try {
System.out.println("Enabling..."); System.out.println("Enabling...");
MockDiscordSRV.INSTANCE.enable(); MockDiscordSRV.getInstance().enable();
MockDiscordSRV.INSTANCE.waitForStatus(DiscordSRVApi.Status.CONNECTED); MockDiscordSRV.getInstance().waitForStatus(DiscordSRVApi.Status.CONNECTED);
System.out.println("Enabled successfully"); System.out.println("Enabled successfully");
} catch (Throwable e) { } catch (Throwable e) {
Assertions.fail(e); Assertions.fail(e);
@ -57,7 +57,7 @@ public class FullBootExtension implements BeforeAllCallback, ExtensionContext.St
@Override @Override
public void close() { public void close() {
System.out.println("Disabling..."); System.out.println("Disabling...");
MockDiscordSRV.INSTANCE.disable(); MockDiscordSRV.getInstance().disable();
System.out.println("Disabled successfully"); System.out.println("Disabled successfully");
} }
} }

View File

@ -57,10 +57,30 @@ import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.fail;
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
public class MockDiscordSRV extends AbstractDiscordSRV<IBootstrap, MainConfig, ConnectionConfig, MessagesConfig> { public class MockDiscordSRV extends AbstractDiscordSRV<IBootstrap, MainConfig, ConnectionConfig, MessagesConfig> {
public static final MockDiscordSRV INSTANCE = new MockDiscordSRV(); private static MockDiscordSRV INSTANCE = null;
private static Throwable FAILED_TO_GET_INSTANCE = null;
public static MockDiscordSRV getInstance() {
if (FAILED_TO_GET_INSTANCE != null) {
fail("Failed to make MockDiscordSRV instance", FAILED_TO_GET_INSTANCE);
assert false;
}
if (INSTANCE != null) {
return INSTANCE;
}
try {
return INSTANCE = new MockDiscordSRV();
} catch (Throwable t) {
FAILED_TO_GET_INSTANCE = t;
return getInstance();
}
}
public boolean configLoaded = false; public boolean configLoaded = false;
public boolean connectionConfigLoaded = false; public boolean connectionConfigLoaded = false;

View File

@ -29,8 +29,9 @@ public class ConfigsLoadOnEnableTest {
@Test @Test
public void configsLoaded() { public void configsLoaded() {
Assertions.assertTrue(MockDiscordSRV.INSTANCE.configLoaded, "Config loaded"); MockDiscordSRV discordSRV = MockDiscordSRV.getInstance();
Assertions.assertTrue(MockDiscordSRV.INSTANCE.connectionConfigLoaded, "Connection config loaded"); Assertions.assertTrue(discordSRV.configLoaded, "Config loaded");
Assertions.assertTrue(MockDiscordSRV.INSTANCE.messagesConfigLoaded, "Messages config loaded"); Assertions.assertTrue(discordSRV.connectionConfigLoaded, "Connection config loaded");
Assertions.assertTrue(discordSRV.messagesConfigLoaded, "Messages config loaded");
} }
} }

View File

@ -31,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class EventBusTest { public class EventBusTest {
private static final Listener listener = new Listener(); private static final Listener listener = new Listener();
private static final EventBus eventBus = MockDiscordSRV.INSTANCE.eventBus(); private static final EventBus eventBus = MockDiscordSRV.getInstance().eventBus();
@BeforeAll @BeforeAll
public static void subscribe() { public static void subscribe() {

View File

@ -41,7 +41,7 @@ import static org.junit.jupiter.api.Assertions.*;
public class RequirementTypeParserTest { public class RequirementTypeParserTest {
private final RequirementParser requirementParser = RequirementParser.getInstance(); private final RequirementParser requirementParser = RequirementParser.getInstance();
private final RequiredLinkingModule<?> module = new RequiredLinkingModule<DiscordSRV>(MockDiscordSRV.INSTANCE) { private final RequiredLinkingModule<?> module = new RequiredLinkingModule<DiscordSRV>(MockDiscordSRV.getInstance()) {
@Override @Override
public RequiredLinkingConfig config() { public RequiredLinkingConfig config() {
return null; return null;

View File

@ -57,7 +57,7 @@ public class MinecraftToDiscordChatMessageTest {
@Test @Test
public void runTest() throws InterruptedException { public void runTest() throws InterruptedException {
DiscordSRV discordSRV = MockDiscordSRV.INSTANCE; DiscordSRV discordSRV = MockDiscordSRV.getInstance();
EventBus bus = discordSRV.eventBus(); EventBus bus = discordSRV.eventBus();
String testMessage = UUID.randomUUID().toString(); String testMessage = UUID.randomUUID().toString();
@ -69,7 +69,7 @@ public class MinecraftToDiscordChatMessageTest {
try { try {
TestHelper.set(future::completeExceptionally); TestHelper.set(future::completeExceptionally);
MockDiscordSRV.INSTANCE.eventBus().publish( discordSRV.eventBus().publish(
new GameChatMessageReceiveEvent( new GameChatMessageReceiveEvent(
null, null,
new IPlayer() { new IPlayer() {
@ -86,7 +86,7 @@ public class MinecraftToDiscordChatMessageTest {
@Override @Override
public DiscordSRV discordSRV() { public DiscordSRV discordSRV() {
return MockDiscordSRV.INSTANCE; return discordSRV;
} }
@Override @Override
@ -129,7 +129,7 @@ public class MinecraftToDiscordChatMessageTest {
public void runCommand(String command) {} public void runCommand(String command) {}
}, },
ComponentUtil.toAPI(Component.text(testMessage)), ComponentUtil.toAPI(Component.text(testMessage)),
new GlobalChannel(MockDiscordSRV.INSTANCE), new GlobalChannel(discordSRV),
false false
)); ));

View File

@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class PlaceholderServiceTest { public class PlaceholderServiceTest {
private final PlaceholderService service = MockDiscordSRV.INSTANCE.placeholderService(); private final PlaceholderService service = MockDiscordSRV.getInstance().placeholderService();
@Test @Test
public void staticFieldTest() { public void staticFieldTest() {

View File

@ -29,6 +29,6 @@ public class PlayerProviderBootTest {
@Test @Test
public void subscribed() { public void subscribed() {
Assertions.assertTrue(MockDiscordSRV.INSTANCE.playerProviderSubscribed, "Player provider subscribed"); Assertions.assertTrue(MockDiscordSRV.getInstance().playerProviderSubscribed, "Player provider subscribed");
} }
} }