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 01c5272c..502af433 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 @@ -1,16 +1,39 @@ package com.launchdarkly.sdk.internal.fdv2.payloads; +import com.google.gson.TypeAdapter; +import com.google.gson.annotations.JsonAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; +import com.google.gson.stream.JsonWriter; import com.launchdarkly.sdk.json.SerializationException; 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; + @JsonAdapter(FallbackTTLAdapter.class) + private final Duration protocolFallbackTTL; + + private static final class FallbackTTLAdapter extends TypeAdapter { + @Override + public void write(JsonWriter writer, Duration value) throws IOException { + writer.value(BigDecimal.valueOf(value.toMillis(), 3)); + } + + @Override + public Duration read(JsonReader reader) throws IOException { + if (reader.peek() == JsonToken.NUMBER) { + return Duration.ofMillis((long) (reader.nextDouble() * 1000)); + } + reader.skipValue(); + return Duration.ZERO; + } + } /** * Constructs a new Goodbye. @@ -18,7 +41,18 @@ public final class Goodbye { * @param reason reason for the disconnection */ public Goodbye(String reason) { + this(reason, null); + } + + /** + * Constructs a new Goodbye. + * + * @param reason reason for the disconnection + * @param protocolFallbackTTL fallback TTL, or null if absent; ZERO requests the default TTL + */ + public Goodbye(String reason, Duration protocolFallbackTTL) { this.reason = reason; + this.protocolFallbackTTL = protocolFallbackTTL; } /** @@ -29,6 +63,15 @@ public Goodbye(String reason) { public String getReason() { return reason; } + + /** + * Returns the fallback TTL, or null if absent. ZERO requests the default TTL. + * + * @return the fallback TTL + */ + public Duration getProtocolFallbackTTL() { + return protocolFallbackTTL; + } /** * Parses a Goodbye from a JsonReader. @@ -39,6 +82,7 @@ public String getReason() { */ public static Goodbye parse(JsonReader reader) throws SerializationException { String reason = null; + Duration protocolFallbackTTL = null; try { if (reader.peek() != JsonToken.BEGIN_OBJECT) { @@ -52,6 +96,14 @@ public static Goodbye parse(JsonReader reader) throws SerializationException { case "reason": reason = reader.nextString(); break; + case "protocolFallbackTTL": + if (reader.peek() == JsonToken.NULL) { + protocolFallbackTTL = null; + reader.skipValue(); + } else { + protocolFallbackTTL = new FallbackTTLAdapter().read(reader); + } + break; default: reader.skipValue(); break; @@ -59,10 +111,9 @@ public static Goodbye parse(JsonReader reader) throws SerializationException { } reader.endObject(); - return new Goodbye(reason); + return new Goodbye(reason, protocolFallbackTTL); } 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 a00b48a8..011b7e06 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,6 +10,7 @@ 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; @@ -136,9 +137,15 @@ public String getReason() { public static final class FDv2ActionGoodbye implements IFDv2ProtocolAction { private final String reason; + private final Duration protocolFallbackTTL; public FDv2ActionGoodbye(String reason) { + this(reason, null); + } + + public FDv2ActionGoodbye(String reason, Duration protocolFallbackTTL) { this.reason = reason; + this.protocolFallbackTTL = protocolFallbackTTL; } @Override @@ -149,6 +156,15 @@ public FDv2ProtocolActionType getAction() { public String getReason() { return reason; } + + /** + * Returns the fallback TTL, or null if absent. ZERO requests the default TTL. + * + * @return the fallback TTL + */ + public Duration getProtocolFallbackTTL() { + return protocolFallbackTTL; + } } public static final class FDv2ActionInternalError implements IFDv2ProtocolAction { @@ -264,7 +280,7 @@ private IFDv2ProtocolAction error(Error error) { } private IFDv2ProtocolAction goodbye(Goodbye intent) { - return new FDv2ActionGoodbye(intent.getReason()); + return new FDv2ActionGoodbye(intent.getReason(), intent.getProtocolFallbackTTL()); } /** @@ -341,4 +357,3 @@ public void reset() { } } - 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 ae64534c..a3296a94 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,6 +16,7 @@ import org.junit.Test; import java.io.StringReader; +import java.time.Duration; import java.util.List; import static com.launchdarkly.sdk.internal.GsonHelpers.gsonInstance; @@ -27,6 +28,29 @@ @SuppressWarnings("javadoc") 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}; + 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("fallback", goodbye.getReason()); + Goodbye reserialized = Goodbye.parse(new JsonReader(new StringReader(gsonInstance().toJson(goodbye)))); + assertEquals(expected[i], reserialized.getProtocolFallbackTTL()); + } + } + + @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()); + } + @Test public void serverIntent_CanDeserializeAndReserialize() throws Exception { String json = "{\n" + @@ -874,4 +898,3 @@ public void deserializeEventsArray_CanDeserializeValidEventsArray() throws Excep assertEquals(100, transferred.getVersion()); } } - 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 ae32c71b..28a6d869 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 @@ -14,6 +14,7 @@ import org.junit.Test; +import java.time.Duration; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -340,6 +341,24 @@ 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()); + } + + @Test + public void goodbye_PassesThroughProtocolFallbackTTL() { + String[] values = {"60", "1.25", "\"invalid\"", "null"}; + Duration[] expected = {Duration.ofSeconds(60), Duration.ofMillis(1250), Duration.ZERO, null}; + for (int i = 0; i < values.length; i++) { + JsonElement data = gsonInstance().fromJson( + "{\"reason\":\"fallback\",\"protocolFallbackTTL\":" + values[i] + "}", JsonElement.class); + FDv2ProtocolHandler.IFDv2ProtocolAction action = new FDv2ProtocolHandler().handleEvent( + new FDv2Event(FDv2EventTypes.GOODBYE, data)); + assertTrue(action instanceof FDv2ProtocolHandler.FDv2ActionGoodbye); + FDv2ProtocolHandler.FDv2ActionGoodbye goodbyeAction = (FDv2ProtocolHandler.FDv2ActionGoodbye) action; + assertEquals("fallback", goodbyeAction.getReason()); + assertEquals(values[i], expected[i], goodbyeAction.getProtocolFallbackTTL()); + } } // Requirement 3.3.9: Heartbeat Handling @@ -1063,4 +1082,3 @@ public void reset_WithMixedOperations_ClearsAllChanges() { assertEquals("f-new", changesetAction.getChangeset().getChanges().get(0).getKey()); } } -