From 1f5fe346fe55c6b53c2bd44df9f553a10dd7e72a Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:21:52 +0000 Subject: [PATCH] fix: Remove java.time.Duration usage from FDv2 goodbye parsing Co-Authored-By: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> --- .../sdk/internal/fdv2/payloads/Goodbye.java | 39 ++++++++++--------- .../fdv2/sources/FDv2ProtocolHandler.java | 18 ++++----- .../fdv2/payloads/FDv2PayloadsTest.java | 21 +++++----- .../fdv2/sources/FDv2ProtocolHandlerTest.java | 9 ++--- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/payloads/Goodbye.java b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/payloads/Goodbye.java index 9a3e0f35..28995cc7 100644 --- a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/payloads/Goodbye.java +++ b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/payloads/Goodbye.java @@ -2,6 +2,7 @@ import com.google.gson.TypeAdapter; import com.google.gson.annotations.JsonAdapter; +import com.google.gson.annotations.SerializedName; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; @@ -9,30 +10,30 @@ import java.io.IOException; import java.math.BigDecimal; -import java.time.Duration; /** * Represents the goodbye event, which indicates that the server is about to disconnect. */ public final class Goodbye { private final String reason; + @SerializedName("protocolFallbackTTL") @JsonAdapter(FallbackTTLAdapter.class) - private final Duration protocolFallbackTTL; + private final Long protocolFallbackTTLMillis; - private static final class FallbackTTLAdapter extends TypeAdapter { + private static final class FallbackTTLAdapter extends TypeAdapter { @Override - public void write(JsonWriter writer, Duration value) throws IOException { - writer.value(BigDecimal.valueOf(value.toMillis(), 3)); + public void write(JsonWriter writer, Long value) throws IOException { + writer.value(BigDecimal.valueOf(value, 3)); } @Override - public Duration read(JsonReader reader) throws IOException { + public Long read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NUMBER) { - return Duration.ofMillis((long) (reader.nextDouble() * 1000)); + return (long) (reader.nextDouble() * 1000); } // The spec treats a malformed TTL the same as 0: both use the default TTL. reader.skipValue(); - return Duration.ZERO; + return 0L; } } @@ -49,11 +50,11 @@ public Goodbye(String reason) { * Constructs a new Goodbye. * * @param reason reason for the disconnection - * @param protocolFallbackTTL fallback TTL, or null if absent; ZERO requests the default TTL + * @param protocolFallbackTTLMillis fallback TTL in milliseconds, or null if absent; 0 requests the default TTL */ - public Goodbye(String reason, Duration protocolFallbackTTL) { + public Goodbye(String reason, Long protocolFallbackTTLMillis) { this.reason = reason; - this.protocolFallbackTTL = protocolFallbackTTL; + this.protocolFallbackTTLMillis = protocolFallbackTTLMillis; } /** @@ -66,12 +67,12 @@ public String getReason() { } /** - * Returns the fallback TTL, or null if absent. ZERO requests the default TTL. + * Returns the fallback TTL in milliseconds, or null if absent. 0 requests the default TTL. * - * @return the fallback TTL + * @return the fallback TTL in milliseconds */ - public Duration getProtocolFallbackTTL() { - return protocolFallbackTTL; + public Long getProtocolFallbackTTLMillis() { + return protocolFallbackTTLMillis; } /** @@ -83,7 +84,7 @@ public Duration getProtocolFallbackTTL() { */ public static Goodbye parse(JsonReader reader) throws SerializationException { String reason = null; - Duration protocolFallbackTTL = null; + Long protocolFallbackTTLMillis = null; try { if (reader.peek() != JsonToken.BEGIN_OBJECT) { @@ -99,10 +100,10 @@ public static Goodbye parse(JsonReader reader) throws SerializationException { break; case "protocolFallbackTTL": if (reader.peek() == JsonToken.NULL) { - protocolFallbackTTL = null; + protocolFallbackTTLMillis = null; reader.skipValue(); } else { - protocolFallbackTTL = new FallbackTTLAdapter().read(reader); + protocolFallbackTTLMillis = new FallbackTTLAdapter().read(reader); } break; default: @@ -112,7 +113,7 @@ public static Goodbye parse(JsonReader reader) throws SerializationException { } reader.endObject(); - return new Goodbye(reason, protocolFallbackTTL); + return new Goodbye(reason, protocolFallbackTTLMillis); } catch (IOException | RuntimeException e) { throw new SerializationException(e); } diff --git a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandler.java b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandler.java index db87c546..b410ce04 100644 --- a/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandler.java +++ b/lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandler.java @@ -10,7 +10,6 @@ import com.launchdarkly.sdk.internal.fdv2.payloads.ServerIntent; import com.launchdarkly.sdk.json.SerializationException; -import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -137,15 +136,15 @@ public String getReason() { public static final class FDv2ActionGoodbye implements IFDv2ProtocolAction { private final String reason; - private final Duration protocolFallbackTTL; + private final Long protocolFallbackTTLMillis; public FDv2ActionGoodbye(String reason) { this(reason, null); } - public FDv2ActionGoodbye(String reason, Duration protocolFallbackTTL) { + public FDv2ActionGoodbye(String reason, Long protocolFallbackTTLMillis) { this.reason = reason; - this.protocolFallbackTTL = protocolFallbackTTL; + this.protocolFallbackTTLMillis = protocolFallbackTTLMillis; } @Override @@ -158,12 +157,12 @@ public String getReason() { } /** - * Returns the fallback TTL, or null if absent. ZERO requests the default TTL. + * Returns the fallback TTL in milliseconds, or null if absent. 0 requests the default TTL. * - * @return the fallback TTL + * @return the fallback TTL in milliseconds */ - public Duration getProtocolFallbackTTL() { - return protocolFallbackTTL; + public Long getProtocolFallbackTTLMillis() { + return protocolFallbackTTLMillis; } } @@ -280,7 +279,7 @@ private IFDv2ProtocolAction error(Error error) { } private IFDv2ProtocolAction goodbye(Goodbye intent) { - return new FDv2ActionGoodbye(intent.getReason(), intent.getProtocolFallbackTTL()); + return new FDv2ActionGoodbye(intent.getReason(), intent.getProtocolFallbackTTLMillis()); } /** @@ -356,4 +355,3 @@ public void reset() { state = FDv2ProtocolState.INACTIVE; } } - diff --git a/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/payloads/FDv2PayloadsTest.java b/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/payloads/FDv2PayloadsTest.java index a3296a94..51cbd061 100644 --- a/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/payloads/FDv2PayloadsTest.java +++ b/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/payloads/FDv2PayloadsTest.java @@ -16,7 +16,6 @@ import org.junit.Test; import java.io.StringReader; -import java.time.Duration; import java.util.List; import static com.launchdarkly.sdk.internal.GsonHelpers.gsonInstance; @@ -31,24 +30,28 @@ public class FDv2PayloadsTest extends BaseInternalTest { @Test public void goodbye_ParsesProtocolFallbackTTL() throws Exception { String[] values = {"60", "1.25", "0", "-1", "3601", "\"invalid\"", "true", "{}", "[]", "null"}; - Duration[] expected = {Duration.ofSeconds(60), Duration.ofMillis(1250), Duration.ZERO, - Duration.ofSeconds(-1), Duration.ofSeconds(3601), Duration.ZERO, Duration.ZERO, - Duration.ZERO, Duration.ZERO, null}; + Long[] expected = {60000L, 1250L, 0L, -1000L, 3601000L, 0L, 0L, 0L, 0L, null}; for (int i = 0; i < values.length; i++) { String json = "{\"reason\":\"fallback\",\"protocolFallbackTTL\":" + values[i] + "}"; Goodbye goodbye = Goodbye.parse(new JsonReader(new StringReader(json))); - assertEquals(values[i], expected[i], goodbye.getProtocolFallbackTTL()); + assertEquals(values[i], expected[i], goodbye.getProtocolFallbackTTLMillis()); assertEquals("fallback", goodbye.getReason()); Goodbye reserialized = Goodbye.parse(new JsonReader(new StringReader(gsonInstance().toJson(goodbye)))); - assertEquals(expected[i], reserialized.getProtocolFallbackTTL()); + assertEquals(expected[i], reserialized.getProtocolFallbackTTLMillis()); } } @Test public void goodbye_WithoutProtocolFallbackTTL() throws Exception { - assertNull(Goodbye.parse(new JsonReader(new StringReader("{}"))).getProtocolFallbackTTL()); - assertNull(new Goodbye("reason").getProtocolFallbackTTL()); - assertEquals(Duration.ofSeconds(5), new Goodbye("reason", Duration.ofSeconds(5)).getProtocolFallbackTTL()); + assertNull(Goodbye.parse(new JsonReader(new StringReader("{}"))).getProtocolFallbackTTLMillis()); + assertNull(new Goodbye("reason").getProtocolFallbackTTLMillis()); + assertEquals(Long.valueOf(5000), new Goodbye("reason", 5000L).getProtocolFallbackTTLMillis()); + } + + @Test + public void goodbye_SerializesProtocolFallbackTTLInSeconds() { + JsonElement json = gsonInstance().toJsonTree(new Goodbye("reason", 1250L)); + assertEquals(1.25, json.getAsJsonObject().get("protocolFallbackTTL").getAsDouble(), 0.0); } @Test diff --git a/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandlerTest.java b/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandlerTest.java index badee28b..56e41e94 100644 --- a/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandlerTest.java +++ b/lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandlerTest.java @@ -16,7 +16,6 @@ import org.junit.Test; -import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -343,14 +342,14 @@ public void goodbye_ReturnsGoodbyeActionWithReason() { assertTrue(action instanceof FDv2ProtocolHandler.FDv2ActionGoodbye); FDv2ProtocolHandler.FDv2ActionGoodbye goodbyeAction = (FDv2ProtocolHandler.FDv2ActionGoodbye) action; assertEquals("Server is shutting down", goodbyeAction.getReason()); - assertNull(goodbyeAction.getProtocolFallbackTTL()); - assertNull(new FDv2ProtocolHandler.FDv2ActionGoodbye("reason").getProtocolFallbackTTL()); + assertNull(goodbyeAction.getProtocolFallbackTTLMillis()); + assertNull(new FDv2ProtocolHandler.FDv2ActionGoodbye("reason").getProtocolFallbackTTLMillis()); } @Test public void goodbye_PassesThroughProtocolFallbackTTL() { String[] values = {"60", "1.25", "\"invalid\"", "null"}; - Duration[] expected = {Duration.ofSeconds(60), Duration.ofMillis(1250), Duration.ZERO, null}; + Long[] expected = {60000L, 1250L, 0L, null}; for (int i = 0; i < values.length; i++) { JsonElement data = gsonInstance().fromJson( "{\"reason\":\"fallback\",\"protocolFallbackTTL\":" + values[i] + "}", JsonElement.class); @@ -359,7 +358,7 @@ public void goodbye_PassesThroughProtocolFallbackTTL() { assertTrue(action instanceof FDv2ProtocolHandler.FDv2ActionGoodbye); FDv2ProtocolHandler.FDv2ActionGoodbye goodbyeAction = (FDv2ProtocolHandler.FDv2ActionGoodbye) action; assertEquals("fallback", goodbyeAction.getReason()); - assertEquals(values[i], expected[i], goodbyeAction.getProtocolFallbackTTL()); + assertEquals(values[i], expected[i], goodbyeAction.getProtocolFallbackTTLMillis()); } }