From f4368a15b84c1a3850cfc922add7a85f5e74a9e5 Mon Sep 17 00:00:00 2001 From: Vankka Date: Fri, 20 Dec 2024 23:04:36 +0200 Subject: [PATCH] Make MockDiscordSRV initialization failures log better --- .../discordsrv/common/FullBootExtension.java | 6 ++--- .../com/discordsrv/common/MockDiscordSRV.java | 22 ++++++++++++++++++- .../config/ConfigsLoadOnEnableTest.java | 7 +++--- .../common/event/bus/EventBusTest.java | 2 +- .../parser/RequirementTypeParserTest.java | 2 +- .../MinecraftToDiscordChatMessageTest.java | 8 +++---- .../placeholder/PlaceholderServiceTest.java | 2 +- .../provider/PlayerProviderBootTest.java | 2 +- 8 files changed, 36 insertions(+), 15 deletions(-) diff --git a/common/src/test/java/com/discordsrv/common/FullBootExtension.java b/common/src/test/java/com/discordsrv/common/FullBootExtension.java index 5658bb72..049f31c3 100644 --- a/common/src/test/java/com/discordsrv/common/FullBootExtension.java +++ b/common/src/test/java/com/discordsrv/common/FullBootExtension.java @@ -46,8 +46,8 @@ public class FullBootExtension implements BeforeAllCallback, ExtensionContext.St try { System.out.println("Enabling..."); - MockDiscordSRV.INSTANCE.enable(); - MockDiscordSRV.INSTANCE.waitForStatus(DiscordSRVApi.Status.CONNECTED); + MockDiscordSRV.getInstance().enable(); + MockDiscordSRV.getInstance().waitForStatus(DiscordSRVApi.Status.CONNECTED); System.out.println("Enabled successfully"); } catch (Throwable e) { Assertions.fail(e); @@ -57,7 +57,7 @@ public class FullBootExtension implements BeforeAllCallback, ExtensionContext.St @Override public void close() { System.out.println("Disabling..."); - MockDiscordSRV.INSTANCE.disable(); + MockDiscordSRV.getInstance().disable(); System.out.println("Disabled successfully"); } } diff --git a/common/src/test/java/com/discordsrv/common/MockDiscordSRV.java b/common/src/test/java/com/discordsrv/common/MockDiscordSRV.java index cf7c194f..52d86054 100644 --- a/common/src/test/java/com/discordsrv/common/MockDiscordSRV.java +++ b/common/src/test/java/com/discordsrv/common/MockDiscordSRV.java @@ -57,10 +57,30 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import static org.junit.jupiter.api.Assertions.fail; + @SuppressWarnings("ConstantConditions") public class MockDiscordSRV extends AbstractDiscordSRV { - 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 connectionConfigLoaded = false; diff --git a/common/src/test/java/com/discordsrv/common/config/ConfigsLoadOnEnableTest.java b/common/src/test/java/com/discordsrv/common/config/ConfigsLoadOnEnableTest.java index 61becbae..f872d4db 100644 --- a/common/src/test/java/com/discordsrv/common/config/ConfigsLoadOnEnableTest.java +++ b/common/src/test/java/com/discordsrv/common/config/ConfigsLoadOnEnableTest.java @@ -29,8 +29,9 @@ public class ConfigsLoadOnEnableTest { @Test public void configsLoaded() { - Assertions.assertTrue(MockDiscordSRV.INSTANCE.configLoaded, "Config loaded"); - Assertions.assertTrue(MockDiscordSRV.INSTANCE.connectionConfigLoaded, "Connection config loaded"); - Assertions.assertTrue(MockDiscordSRV.INSTANCE.messagesConfigLoaded, "Messages config loaded"); + MockDiscordSRV discordSRV = MockDiscordSRV.getInstance(); + Assertions.assertTrue(discordSRV.configLoaded, "Config loaded"); + Assertions.assertTrue(discordSRV.connectionConfigLoaded, "Connection config loaded"); + Assertions.assertTrue(discordSRV.messagesConfigLoaded, "Messages config loaded"); } } diff --git a/common/src/test/java/com/discordsrv/common/event/bus/EventBusTest.java b/common/src/test/java/com/discordsrv/common/event/bus/EventBusTest.java index 553f3437..dabcea42 100644 --- a/common/src/test/java/com/discordsrv/common/event/bus/EventBusTest.java +++ b/common/src/test/java/com/discordsrv/common/event/bus/EventBusTest.java @@ -31,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class EventBusTest { private static final Listener listener = new Listener(); - private static final EventBus eventBus = MockDiscordSRV.INSTANCE.eventBus(); + private static final EventBus eventBus = MockDiscordSRV.getInstance().eventBus(); @BeforeAll public static void subscribe() { diff --git a/common/src/test/java/com/discordsrv/common/linking/requirement/parser/RequirementTypeParserTest.java b/common/src/test/java/com/discordsrv/common/linking/requirement/parser/RequirementTypeParserTest.java index d0a78ed4..16aadfdd 100644 --- a/common/src/test/java/com/discordsrv/common/linking/requirement/parser/RequirementTypeParserTest.java +++ b/common/src/test/java/com/discordsrv/common/linking/requirement/parser/RequirementTypeParserTest.java @@ -41,7 +41,7 @@ import static org.junit.jupiter.api.Assertions.*; public class RequirementTypeParserTest { private final RequirementParser requirementParser = RequirementParser.getInstance(); - private final RequiredLinkingModule module = new RequiredLinkingModule(MockDiscordSRV.INSTANCE) { + private final RequiredLinkingModule module = new RequiredLinkingModule(MockDiscordSRV.getInstance()) { @Override public RequiredLinkingConfig config() { return null; diff --git a/common/src/test/java/com/discordsrv/common/messageforwarding/game/MinecraftToDiscordChatMessageTest.java b/common/src/test/java/com/discordsrv/common/messageforwarding/game/MinecraftToDiscordChatMessageTest.java index 99a45efe..08419058 100644 --- a/common/src/test/java/com/discordsrv/common/messageforwarding/game/MinecraftToDiscordChatMessageTest.java +++ b/common/src/test/java/com/discordsrv/common/messageforwarding/game/MinecraftToDiscordChatMessageTest.java @@ -57,7 +57,7 @@ public class MinecraftToDiscordChatMessageTest { @Test public void runTest() throws InterruptedException { - DiscordSRV discordSRV = MockDiscordSRV.INSTANCE; + DiscordSRV discordSRV = MockDiscordSRV.getInstance(); EventBus bus = discordSRV.eventBus(); String testMessage = UUID.randomUUID().toString(); @@ -69,7 +69,7 @@ public class MinecraftToDiscordChatMessageTest { try { TestHelper.set(future::completeExceptionally); - MockDiscordSRV.INSTANCE.eventBus().publish( + discordSRV.eventBus().publish( new GameChatMessageReceiveEvent( null, new IPlayer() { @@ -86,7 +86,7 @@ public class MinecraftToDiscordChatMessageTest { @Override public DiscordSRV discordSRV() { - return MockDiscordSRV.INSTANCE; + return discordSRV; } @Override @@ -129,7 +129,7 @@ public class MinecraftToDiscordChatMessageTest { public void runCommand(String command) {} }, ComponentUtil.toAPI(Component.text(testMessage)), - new GlobalChannel(MockDiscordSRV.INSTANCE), + new GlobalChannel(discordSRV), false )); diff --git a/common/src/test/java/com/discordsrv/common/placeholder/PlaceholderServiceTest.java b/common/src/test/java/com/discordsrv/common/placeholder/PlaceholderServiceTest.java index 5a584f3a..c9a6b359 100644 --- a/common/src/test/java/com/discordsrv/common/placeholder/PlaceholderServiceTest.java +++ b/common/src/test/java/com/discordsrv/common/placeholder/PlaceholderServiceTest.java @@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class PlaceholderServiceTest { - private final PlaceholderService service = MockDiscordSRV.INSTANCE.placeholderService(); + private final PlaceholderService service = MockDiscordSRV.getInstance().placeholderService(); @Test public void staticFieldTest() { diff --git a/common/src/test/java/com/discordsrv/common/player/provider/PlayerProviderBootTest.java b/common/src/test/java/com/discordsrv/common/player/provider/PlayerProviderBootTest.java index 41a946e5..32e15568 100644 --- a/common/src/test/java/com/discordsrv/common/player/provider/PlayerProviderBootTest.java +++ b/common/src/test/java/com/discordsrv/common/player/provider/PlayerProviderBootTest.java @@ -29,6 +29,6 @@ public class PlayerProviderBootTest { @Test public void subscribed() { - Assertions.assertTrue(MockDiscordSRV.INSTANCE.playerProviderSubscribed, "Player provider subscribed"); + Assertions.assertTrue(MockDiscordSRV.getInstance().playerProviderSubscribed, "Player provider subscribed"); } }