Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,59 @@
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<Duration> {
@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));
}
// The spec treats a malformed TTL the same as 0: both use the default TTL.
reader.skipValue();
return Duration.ZERO;
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
}
}

/**
* Constructs a new 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;
}

/**
Expand All @@ -29,6 +64,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.
Expand All @@ -39,6 +83,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) {
Expand All @@ -52,17 +97,24 @@ public static Goodbye parse(JsonReader reader) throws SerializationException {
case "reason":
reason = reader.nextString();
break;
case "protocolFallbackTTL":
if (reader.peek() == JsonToken.NULL) {
Comment thread
beekld marked this conversation as resolved.
Resolved
protocolFallbackTTL = null;
reader.skipValue();
} else {
protocolFallbackTTL = new FallbackTTLAdapter().read(reader);
}
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();

return new Goodbye(reason);
return new Goodbye(reason, protocolFallbackTTL);
} catch (IOException | RuntimeException e) {
throw new SerializationException(e);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -341,4 +357,3 @@ public void reset() {
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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" +
Expand Down Expand Up @@ -874,4 +898,3 @@ public void deserializeEventsArray_CanDeserializeValidEventsArray() throws Excep
assertEquals(100, transferred.getVersion());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.junit.Test;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1063,4 +1082,3 @@ public void reset_WithMixedOperations_ClearsAllChanges() {
assertEquals("f-new", changesetAction.getChangeset().getChanges().get(0).getKey());
}
}

Loading