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
Expand Up @@ -2,37 +2,38 @@

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;
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;
@SerializedName("protocolFallbackTTL")
@JsonAdapter(FallbackTTLAdapter.class)
private final Duration protocolFallbackTTL;
private final Long protocolFallbackTTLMillis;

private static final class FallbackTTLAdapter extends TypeAdapter<Duration> {
private static final class FallbackTTLAdapter extends TypeAdapter<Long> {
@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;
}
}

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

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

Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -356,4 +355,3 @@ public void reset() {
state = FDv2ProtocolState.INACTIVE;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import org.junit.Test;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -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);
Expand All @@ -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());
}
}

Expand Down