From 23181ebd4289722c0b927576a13bd7e08e6440d8 Mon Sep 17 00:00:00 2001 From: Eric T Date: Wed, 30 Jul 2025 22:14:30 +0100 Subject: [PATCH 1/2] fix singleton initialization and add test --- .../nostr/api/NostrSpringWebSocketClient.java | 20 +++++++++-- .../unit/NostrSpringWebSocketClientTest.java | 36 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 nostr-java-api/src/test/java/nostr/api/unit/NostrSpringWebSocketClientTest.java diff --git a/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java b/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java index a5b7c94b5..f12733d0d 100644 --- a/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java +++ b/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java @@ -25,18 +25,32 @@ public class NostrSpringWebSocketClient implements NostrIF { @Getter private Identity sender; - private static NostrSpringWebSocketClient INSTANCE; + private static volatile NostrSpringWebSocketClient INSTANCE; public NostrSpringWebSocketClient(String relayName, String relayUri) { setRelays(Map.of(relayName, relayUri)); } public static NostrIF getInstance() { - return (INSTANCE == null) ? new NostrSpringWebSocketClient() : INSTANCE; + if (INSTANCE == null) { + synchronized (NostrSpringWebSocketClient.class) { + if (INSTANCE == null) { + INSTANCE = new NostrSpringWebSocketClient(); + } + } + } + return INSTANCE; } public static NostrIF getInstance(@NonNull Identity sender) { - return (INSTANCE == null) ? new NostrSpringWebSocketClient(sender) : INSTANCE; + if (INSTANCE == null) { + synchronized (NostrSpringWebSocketClient.class) { + if (INSTANCE == null) { + INSTANCE = new NostrSpringWebSocketClient(sender); + } + } + } + return INSTANCE; } public NostrSpringWebSocketClient(@NonNull Identity sender) { diff --git a/nostr-java-api/src/test/java/nostr/api/unit/NostrSpringWebSocketClientTest.java b/nostr-java-api/src/test/java/nostr/api/unit/NostrSpringWebSocketClientTest.java new file mode 100644 index 000000000..a590c2192 --- /dev/null +++ b/nostr-java-api/src/test/java/nostr/api/unit/NostrSpringWebSocketClientTest.java @@ -0,0 +1,36 @@ +package nostr.api.unit; + +import nostr.api.NostrIF; +import nostr.api.NostrSpringWebSocketClient; +import nostr.id.Identity; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; + +import static org.junit.jupiter.api.Assertions.assertSame; + +public class NostrSpringWebSocketClientTest { + + @BeforeEach + void resetSingleton() throws Exception { + Field instance = NostrSpringWebSocketClient.class.getDeclaredField("INSTANCE"); + instance.setAccessible(true); + instance.set(null, null); + } + + @Test + void getInstanceShouldReturnSameInstance() { + NostrIF first = NostrSpringWebSocketClient.getInstance(); + NostrIF second = NostrSpringWebSocketClient.getInstance(); + assertSame(first, second, "Multiple calls should return the same instance"); + } + + @Test + void getInstanceWithIdentityShouldReturnSameInstance() { + Identity identity = Identity.generateRandomIdentity(); + NostrIF first = NostrSpringWebSocketClient.getInstance(identity); + NostrIF second = NostrSpringWebSocketClient.getInstance(); + assertSame(first, second, "Calls with and without identity should return the same instance"); + } +} From caabc1a963e944ac25eb97903e4d835bd6184623 Mon Sep 17 00:00:00 2001 From: Eric T Date: Wed, 30 Jul 2025 22:27:24 +0100 Subject: [PATCH 2/2] Update nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../src/main/java/nostr/api/NostrSpringWebSocketClient.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java b/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java index f12733d0d..f38e0c34f 100644 --- a/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java +++ b/nostr-java-api/src/main/java/nostr/api/NostrSpringWebSocketClient.java @@ -47,6 +47,8 @@ public static NostrIF getInstance(@NonNull Identity sender) { synchronized (NostrSpringWebSocketClient.class) { if (INSTANCE == null) { INSTANCE = new NostrSpringWebSocketClient(sender); + } else if (INSTANCE.getSender() == null) { + INSTANCE.sender = sender; // Initialize sender if not already set } } }