Skip to content

Commit 171be16

Browse files
fix: Remove java.time.Duration usage from FDv2 goodbye parsing (#273)
**Requirements** - [x] I have added test coverage for new or changed functionality - [x] I have followed the repository's [pull request submission guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests) - [ ] I have validated my changes against all supported platform versions **Related issues** Follow-up to #265. Android consumes this library (android-client-sdk#435); `java.time.Duration` is only available natively on API 26+, and Android supports API 21 without core-library desugaring. **Describe the solution you've provided** `Goodbye` and `FDv2ProtocolHandler` in `lib/shared/internal` now store `protocolFallbackTTL` as a nullable `Long` of milliseconds instead of a `Duration`. The wire format (seconds) is unchanged. Internal API changes: - `Goodbye.getProtocolFallbackTTL()` (returned `Duration`) is replaced by `getProtocolFallbackTTLMillis()` (returns `Long`). - The two-argument constructors take a `Long` of milliseconds; the one-argument constructors are unchanged. Neither the Java server SDK nor the Android SDK calls the old getter. The compiled production classes in the module have no `java.time` references. **Describe alternatives you've considered** Keeping the `Duration` getter alongside a millis accessor. That still leaves `Duration` in the parse path, which is what runs on Android. **Additional context** Ran `FDv2PayloadsTest` and `FDv2ProtocolHandlerTest` plus the rest of the module's JUnit tests directly: 324 passing. Local Gradle/Checkstyle was blocked by Maven Central rate limiting, so CI is the check for those. Link to Devin session: https://app.devin.ai/sessions/2540f086f1ea4dfba1470051465fb0fd Open in Devin Desktop: https://app.devin.ai/desktop/session/2540f086f1ea4dfba1470051465fb0fd?variant=devin Requested by: @kinyoklion <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Overview** > FDv2 **goodbye** handling no longer uses `java.time.Duration` for `protocolFallbackTTL`. **`Goodbye`** and **`FDv2ProtocolHandler.FDv2ActionGoodbye`** now hold a nullable **`Long` in milliseconds**, with wire JSON still expressed in **seconds** via the existing `FallbackTTLAdapter` (malformed values still map to **0**). > > The public internal API rename is **`getProtocolFallbackTTL()` → `getProtocolFallbackTTLMillis()`**, and constructors that took a `Duration` take millis instead. Tests in **`FDv2PayloadsTest`** and **`FDv2ProtocolHandlerTest`** expect millis values and add coverage that Gson serializes TTL back to seconds (e.g. 1250 ms → `1.25`). > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 1f5fe34. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 32c8676 commit 171be16

4 files changed

Lines changed: 44 additions & 43 deletions

File tree

‎lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/payloads/Goodbye.java‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@
22

33
import com.google.gson.TypeAdapter;
44
import com.google.gson.annotations.JsonAdapter;
5+
import com.google.gson.annotations.SerializedName;
56
import com.google.gson.stream.JsonReader;
67
import com.google.gson.stream.JsonToken;
78
import com.google.gson.stream.JsonWriter;
89
import com.launchdarkly.sdk.json.SerializationException;
910

1011
import java.io.IOException;
1112
import java.math.BigDecimal;
12-
import java.time.Duration;
1313

1414
/**
1515
* Represents the goodbye event, which indicates that the server is about to disconnect.
1616
*/
1717
public final class Goodbye {
1818
private final String reason;
19+
@SerializedName("protocolFallbackTTL")
1920
@JsonAdapter(FallbackTTLAdapter.class)
20-
private final Duration protocolFallbackTTL;
21+
private final Long protocolFallbackTTLMillis;
2122

22-
private static final class FallbackTTLAdapter extends TypeAdapter<Duration> {
23+
private static final class FallbackTTLAdapter extends TypeAdapter<Long> {
2324
@Override
24-
public void write(JsonWriter writer, Duration value) throws IOException {
25-
writer.value(BigDecimal.valueOf(value.toMillis(), 3));
25+
public void write(JsonWriter writer, Long value) throws IOException {
26+
writer.value(BigDecimal.valueOf(value, 3));
2627
}
2728

2829
@Override
29-
public Duration read(JsonReader reader) throws IOException {
30+
public Long read(JsonReader reader) throws IOException {
3031
if (reader.peek() == JsonToken.NUMBER) {
31-
return Duration.ofMillis((long) (reader.nextDouble() * 1000));
32+
return (long) (reader.nextDouble() * 1000);
3233
}
3334
// The spec treats a malformed TTL the same as 0: both use the default TTL.
3435
reader.skipValue();
35-
return Duration.ZERO;
36+
return 0L;
3637
}
3738
}
3839

@@ -49,11 +50,11 @@ public Goodbye(String reason) {
4950
* Constructs a new Goodbye.
5051
*
5152
* @param reason reason for the disconnection
52-
* @param protocolFallbackTTL fallback TTL, or null if absent; ZERO requests the default TTL
53+
* @param protocolFallbackTTLMillis fallback TTL in milliseconds, or null if absent; 0 requests the default TTL
5354
*/
54-
public Goodbye(String reason, Duration protocolFallbackTTL) {
55+
public Goodbye(String reason, Long protocolFallbackTTLMillis) {
5556
this.reason = reason;
56-
this.protocolFallbackTTL = protocolFallbackTTL;
57+
this.protocolFallbackTTLMillis = protocolFallbackTTLMillis;
5758
}
5859

5960
/**
@@ -66,12 +67,12 @@ public String getReason() {
6667
}
6768

6869
/**
69-
* Returns the fallback TTL, or null if absent. ZERO requests the default TTL.
70+
* Returns the fallback TTL in milliseconds, or null if absent. 0 requests the default TTL.
7071
*
71-
* @return the fallback TTL
72+
* @return the fallback TTL in milliseconds
7273
*/
73-
public Duration getProtocolFallbackTTL() {
74-
return protocolFallbackTTL;
74+
public Long getProtocolFallbackTTLMillis() {
75+
return protocolFallbackTTLMillis;
7576
}
7677

7778
/**
@@ -83,7 +84,7 @@ public Duration getProtocolFallbackTTL() {
8384
*/
8485
public static Goodbye parse(JsonReader reader) throws SerializationException {
8586
String reason = null;
86-
Duration protocolFallbackTTL = null;
87+
Long protocolFallbackTTLMillis = null;
8788

8889
try {
8990
if (reader.peek() != JsonToken.BEGIN_OBJECT) {
@@ -99,10 +100,10 @@ public static Goodbye parse(JsonReader reader) throws SerializationException {
99100
break;
100101
case "protocolFallbackTTL":
101102
if (reader.peek() == JsonToken.NULL) {
102-
protocolFallbackTTL = null;
103+
protocolFallbackTTLMillis = null;
103104
reader.skipValue();
104105
} else {
105-
protocolFallbackTTL = new FallbackTTLAdapter().read(reader);
106+
protocolFallbackTTLMillis = new FallbackTTLAdapter().read(reader);
106107
}
107108
break;
108109
default:
@@ -112,7 +113,7 @@ public static Goodbye parse(JsonReader reader) throws SerializationException {
112113
}
113114
reader.endObject();
114115

115-
return new Goodbye(reason, protocolFallbackTTL);
116+
return new Goodbye(reason, protocolFallbackTTLMillis);
116117
} catch (IOException | RuntimeException e) {
117118
throw new SerializationException(e);
118119
}

‎lib/shared/internal/src/main/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandler.java‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.launchdarkly.sdk.internal.fdv2.payloads.ServerIntent;
1111
import com.launchdarkly.sdk.json.SerializationException;
1212

13-
import java.time.Duration;
1413
import java.util.ArrayList;
1514
import java.util.Collections;
1615
import java.util.List;
@@ -137,15 +136,15 @@ public String getReason() {
137136

138137
public static final class FDv2ActionGoodbye implements IFDv2ProtocolAction {
139138
private final String reason;
140-
private final Duration protocolFallbackTTL;
139+
private final Long protocolFallbackTTLMillis;
141140

142141
public FDv2ActionGoodbye(String reason) {
143142
this(reason, null);
144143
}
145144

146-
public FDv2ActionGoodbye(String reason, Duration protocolFallbackTTL) {
145+
public FDv2ActionGoodbye(String reason, Long protocolFallbackTTLMillis) {
147146
this.reason = reason;
148-
this.protocolFallbackTTL = protocolFallbackTTL;
147+
this.protocolFallbackTTLMillis = protocolFallbackTTLMillis;
149148
}
150149

151150
@Override
@@ -158,12 +157,12 @@ public String getReason() {
158157
}
159158

160159
/**
161-
* Returns the fallback TTL, or null if absent. ZERO requests the default TTL.
160+
* Returns the fallback TTL in milliseconds, or null if absent. 0 requests the default TTL.
162161
*
163-
* @return the fallback TTL
162+
* @return the fallback TTL in milliseconds
164163
*/
165-
public Duration getProtocolFallbackTTL() {
166-
return protocolFallbackTTL;
164+
public Long getProtocolFallbackTTLMillis() {
165+
return protocolFallbackTTLMillis;
167166
}
168167
}
169168

@@ -280,7 +279,7 @@ private IFDv2ProtocolAction error(Error error) {
280279
}
281280

282281
private IFDv2ProtocolAction goodbye(Goodbye intent) {
283-
return new FDv2ActionGoodbye(intent.getReason(), intent.getProtocolFallbackTTL());
282+
return new FDv2ActionGoodbye(intent.getReason(), intent.getProtocolFallbackTTLMillis());
284283
}
285284

286285
/**
@@ -356,4 +355,3 @@ public void reset() {
356355
state = FDv2ProtocolState.INACTIVE;
357356
}
358357
}
359-

‎lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/payloads/FDv2PayloadsTest.java‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.junit.Test;
1717

1818
import java.io.StringReader;
19-
import java.time.Duration;
2019
import java.util.List;
2120

2221
import static com.launchdarkly.sdk.internal.GsonHelpers.gsonInstance;
@@ -31,24 +30,28 @@ public class FDv2PayloadsTest extends BaseInternalTest {
3130
@Test
3231
public void goodbye_ParsesProtocolFallbackTTL() throws Exception {
3332
String[] values = {"60", "1.25", "0", "-1", "3601", "\"invalid\"", "true", "{}", "[]", "null"};
34-
Duration[] expected = {Duration.ofSeconds(60), Duration.ofMillis(1250), Duration.ZERO,
35-
Duration.ofSeconds(-1), Duration.ofSeconds(3601), Duration.ZERO, Duration.ZERO,
36-
Duration.ZERO, Duration.ZERO, null};
33+
Long[] expected = {60000L, 1250L, 0L, -1000L, 3601000L, 0L, 0L, 0L, 0L, null};
3734
for (int i = 0; i < values.length; i++) {
3835
String json = "{\"reason\":\"fallback\",\"protocolFallbackTTL\":" + values[i] + "}";
3936
Goodbye goodbye = Goodbye.parse(new JsonReader(new StringReader(json)));
40-
assertEquals(values[i], expected[i], goodbye.getProtocolFallbackTTL());
37+
assertEquals(values[i], expected[i], goodbye.getProtocolFallbackTTLMillis());
4138
assertEquals("fallback", goodbye.getReason());
4239
Goodbye reserialized = Goodbye.parse(new JsonReader(new StringReader(gsonInstance().toJson(goodbye))));
43-
assertEquals(expected[i], reserialized.getProtocolFallbackTTL());
40+
assertEquals(expected[i], reserialized.getProtocolFallbackTTLMillis());
4441
}
4542
}
4643

4744
@Test
4845
public void goodbye_WithoutProtocolFallbackTTL() throws Exception {
49-
assertNull(Goodbye.parse(new JsonReader(new StringReader("{}"))).getProtocolFallbackTTL());
50-
assertNull(new Goodbye("reason").getProtocolFallbackTTL());
51-
assertEquals(Duration.ofSeconds(5), new Goodbye("reason", Duration.ofSeconds(5)).getProtocolFallbackTTL());
46+
assertNull(Goodbye.parse(new JsonReader(new StringReader("{}"))).getProtocolFallbackTTLMillis());
47+
assertNull(new Goodbye("reason").getProtocolFallbackTTLMillis());
48+
assertEquals(Long.valueOf(5000), new Goodbye("reason", 5000L).getProtocolFallbackTTLMillis());
49+
}
50+
51+
@Test
52+
public void goodbye_SerializesProtocolFallbackTTLInSeconds() {
53+
JsonElement json = gsonInstance().toJsonTree(new Goodbye("reason", 1250L));
54+
assertEquals(1.25, json.getAsJsonObject().get("protocolFallbackTTL").getAsDouble(), 0.0);
5255
}
5356

5457
@Test

‎lib/shared/internal/src/test/java/com/launchdarkly/sdk/internal/fdv2/sources/FDv2ProtocolHandlerTest.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import org.junit.Test;
1818

19-
import java.time.Duration;
2019
import java.util.ArrayList;
2120
import java.util.Collections;
2221
import java.util.List;
@@ -343,14 +342,14 @@ public void goodbye_ReturnsGoodbyeActionWithReason() {
343342
assertTrue(action instanceof FDv2ProtocolHandler.FDv2ActionGoodbye);
344343
FDv2ProtocolHandler.FDv2ActionGoodbye goodbyeAction = (FDv2ProtocolHandler.FDv2ActionGoodbye) action;
345344
assertEquals("Server is shutting down", goodbyeAction.getReason());
346-
assertNull(goodbyeAction.getProtocolFallbackTTL());
347-
assertNull(new FDv2ProtocolHandler.FDv2ActionGoodbye("reason").getProtocolFallbackTTL());
345+
assertNull(goodbyeAction.getProtocolFallbackTTLMillis());
346+
assertNull(new FDv2ProtocolHandler.FDv2ActionGoodbye("reason").getProtocolFallbackTTLMillis());
348347
}
349348

350349
@Test
351350
public void goodbye_PassesThroughProtocolFallbackTTL() {
352351
String[] values = {"60", "1.25", "\"invalid\"", "null"};
353-
Duration[] expected = {Duration.ofSeconds(60), Duration.ofMillis(1250), Duration.ZERO, null};
352+
Long[] expected = {60000L, 1250L, 0L, null};
354353
for (int i = 0; i < values.length; i++) {
355354
JsonElement data = gsonInstance().fromJson(
356355
"{\"reason\":\"fallback\",\"protocolFallbackTTL\":" + values[i] + "}", JsonElement.class);
@@ -359,7 +358,7 @@ public void goodbye_PassesThroughProtocolFallbackTTL() {
359358
assertTrue(action instanceof FDv2ProtocolHandler.FDv2ActionGoodbye);
360359
FDv2ProtocolHandler.FDv2ActionGoodbye goodbyeAction = (FDv2ProtocolHandler.FDv2ActionGoodbye) action;
361360
assertEquals("fallback", goodbyeAction.getReason());
362-
assertEquals(values[i], expected[i], goodbyeAction.getProtocolFallbackTTL());
361+
assertEquals(values[i], expected[i], goodbyeAction.getProtocolFallbackTTLMillis());
363362
}
364363
}
365364

0 commit comments

Comments
 (0)