Skip to content

Commit 904ec28

Browse files
committed
polish
1 parent 5db3340 commit 904ec28

File tree

8 files changed

+29
-27
lines changed

8 files changed

+29
-27
lines changed

client/src/main/java/io/split/Spec.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ private Spec() {
99
// TODO: Change the schema to 1.3 when updating splitclient
1010
public static final String SPEC_1_3 = "1.3";
1111
public static final String SPEC_1_1 = "1.1";
12-
public static String SPEC_VERSION = SPEC_1_3;
1312
}
1413

client/src/main/java/io/split/client/HttpSplitChangeFetcher.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import io.split.Spec;
66
import io.split.client.dtos.SplitChange;
77
import io.split.client.dtos.SplitHttpResponse;
8-
import io.split.client.dtos.RuleBasedSegment;
98
import io.split.client.dtos.SplitChangesOldPayloadDto;
109
import io.split.client.dtos.ChangeDto;
1110
import io.split.client.dtos.Split;
@@ -25,10 +24,8 @@
2524

2625
import java.net.URI;
2726
import java.net.URISyntaxException;
28-
import java.util.ArrayList;
2927

3028
import static com.google.common.base.Preconditions.checkNotNull;
31-
import static io.split.Spec.SPEC_VERSION;
3229
import static io.split.Spec.SPEC_1_3;
3330
import static io.split.Spec.SPEC_1_1;
3431

@@ -44,6 +41,7 @@ public final class HttpSplitChangeFetcher implements SplitChangeFetcher {
4441
private static final String TILL = "till";
4542
private static final String SETS = "sets";
4643
private static final String SPEC = "s";
44+
private String specVersion = SPEC_1_3;
4745
private int PROXY_CHECK_INTERVAL_MILLISECONDS_SS = 24 * 60 * 60 * 1000;
4846
private Long _lastProxyCheckTimestamp = 0L;
4947
private final SplitHttpClient _client;
@@ -75,22 +73,23 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
7573
long start = System.currentTimeMillis();
7674
SplitHttpResponse response;
7775
try {
78-
if (SPEC_VERSION.equals(SPEC_1_1) && (System.currentTimeMillis() - _lastProxyCheckTimestamp >= PROXY_CHECK_INTERVAL_MILLISECONDS_SS)) {
76+
if (specVersion.equals(SPEC_1_1) && (System.currentTimeMillis() - _lastProxyCheckTimestamp >= PROXY_CHECK_INTERVAL_MILLISECONDS_SS)) {
7977
_log.info("Switching to new Feature flag spec ({}) and fetching.", SPEC_1_3);
80-
SPEC_VERSION = SPEC_1_3;
78+
specVersion = SPEC_1_3;
8179
since = -1;
8280
sinceRBS = -1;
8381
}
8482
URI uri = buildURL(options, since, sinceRBS);
83+
_log.error(uri.toString());
8584
response = _client.get(uri, options, null);
8685
if (response.statusCode() < HttpStatus.SC_OK || response.statusCode() >= HttpStatus.SC_MULTIPLE_CHOICES) {
8786
if (response.statusCode() == HttpStatus.SC_REQUEST_URI_TOO_LONG) {
8887
_log.error("The amount of flag sets provided are big causing uri length error.");
8988
throw new UriTooLongException(String.format("Status code: %s. Message: %s", response.statusCode(), response.statusMessage()));
9089
}
9190

92-
if (response.statusCode() == HttpStatus.SC_BAD_REQUEST && SPEC_VERSION.equals(Spec.SPEC_1_3) && _rootURIOverriden) {
93-
SPEC_VERSION = Spec.SPEC_1_1;
91+
if (response.statusCode() == HttpStatus.SC_BAD_REQUEST && specVersion.equals(Spec.SPEC_1_3) && _rootURIOverriden) {
92+
specVersion = Spec.SPEC_1_1;
9493
_log.warn("Detected proxy without support for Feature flags spec {} version, will switch to spec version {}",
9594
SPEC_1_3, SPEC_1_1);
9695
_lastProxyCheckTimestamp = System.currentTimeMillis();
@@ -109,12 +108,12 @@ public SplitChange fetch(long since, long sinceRBS, FetchOptions options) {
109108
}
110109

111110
SplitChange splitChange = new SplitChange();
112-
if (SPEC_VERSION.equals(Spec.SPEC_1_1)) {
111+
if (specVersion.equals(Spec.SPEC_1_1)) {
113112
splitChange.featureFlags = convertBodyToOldSpec(response.body());
114113
splitChange.ruleBasedSegments = ChangeDto.createEmptyDto();
115114
} else {
116115
splitChange = Json.fromJson(response.body(), SplitChange.class);
117-
if (SPEC_VERSION.equals(Spec.SPEC_1_3) && _lastProxyCheckTimestamp != 0) {
116+
if (specVersion.equals(Spec.SPEC_1_3) && _lastProxyCheckTimestamp != 0) {
118117
splitChange.clearCache = true;
119118
_lastProxyCheckTimestamp = 0L;
120119
}
@@ -127,9 +126,9 @@ private ChangeDto<Split> convertBodyToOldSpec(String body) {
127126
}
128127

129128
private URI buildURL(FetchOptions options, long since, long sinceRBS) throws URISyntaxException {
130-
URIBuilder uriBuilder = new URIBuilder(_target).addParameter(SPEC, "" + SPEC_VERSION);
129+
URIBuilder uriBuilder = new URIBuilder(_target).addParameter(SPEC, "" + specVersion);
131130
uriBuilder.addParameter(SINCE, "" + since);
132-
if (SPEC_VERSION.equals(SPEC_1_3)) {
131+
if (specVersion.equals(SPEC_1_3)) {
133132
uriBuilder.addParameter(RB_SINCE, "" + sinceRBS);
134133
}
135134
if (!options.flagSetsFilter().isEmpty()) {

client/src/main/java/io/split/engine/experiments/SplitFetcherImp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.Set;
2222

2323
import static com.google.common.base.Preconditions.checkNotNull;
24-
import static io.split.Spec.SPEC_VERSION;
24+
2525
import static io.split.client.utils.FeatureFlagProcessor.processFeatureFlagChanges;
2626
import static io.split.client.utils.RuleBasedSegmentProcessor.processRuleBasedSegmentChanges;
2727

client/src/main/java/io/split/engine/sse/AuthApiClientImp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.split.engine.sse;
22

33
import com.google.gson.JsonObject;
4+
import io.split.Spec;
45
import io.split.client.dtos.SplitHttpResponse;
56
import io.split.client.utils.Json;
67
import io.split.engine.common.FetchOptions;
@@ -18,7 +19,6 @@
1819
import java.net.URI;
1920

2021
import static com.google.common.base.Preconditions.checkNotNull;
21-
import static io.split.Spec.SPEC_VERSION;
2222

2323
public class AuthApiClientImp implements AuthApiClient {
2424
private static final Logger _log = LoggerFactory.getLogger(AuthApiClientImp.class);
@@ -38,7 +38,7 @@ public AuthApiClientImp(String url, SplitHttpClient httpClient, TelemetryRuntime
3838
public AuthenticationResponse Authenticate() {
3939
try {
4040
long initTime = System.currentTimeMillis();
41-
URI uri = new URIBuilder(_target).addParameter(SPEC, "" + SPEC_VERSION).build();
41+
URI uri = new URIBuilder(_target).addParameter(SPEC, "" + Spec.SPEC_1_3).build();
4242
SplitHttpResponse response = _httpClient.get(uri, new FetchOptions.Builder().cacheControlHeaders(false).build(), null);
4343
Integer statusCode = response.statusCode();
4444

client/src/test/java/io/split/client/HttpSplitChangeFetcherTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void testURLTooLong() throws IOException, URISyntaxException, IllegalAcce
198198
@Test
199199
public void testSwitchingToOldSpec() throws URISyntaxException, InvocationTargetException,
200200
NoSuchMethodException, IllegalAccessException, IOException, NoSuchFieldException, InterruptedException {
201-
Spec.SPEC_VERSION = Spec.SPEC_1_3;
201+
// Spec.SPEC_VERSION = Spec.SPEC_1_3;
202202
URI rootTarget = URI.create("https://api.split.io");
203203
CloseableHttpClient httpClientMock = Mockito.mock(CloseableHttpClient.class);
204204
HttpEntity entityMock = Mockito.mock(HttpEntity.class);
@@ -247,9 +247,12 @@ public void testSwitchingToOldSpec() throws URISyntaxException, InvocationTarget
247247
HttpSplitChangeFetcher fetcher = HttpSplitChangeFetcher.create(splitHtpClient, rootTarget,
248248
Mockito.mock(TelemetryRuntimeProducer.class), true);
249249

250+
Field specVersion = fetcher.getClass().getDeclaredField("specVersion");
251+
specVersion.setAccessible(true);
252+
specVersion.set(fetcher, Spec.SPEC_1_1);
253+
250254
SplitChange change = fetcher.fetch(-1, -1, new FetchOptions.Builder().cacheControlHeaders(true).build());
251255

252-
Assert.assertEquals(Spec.SPEC_1_1, Spec.SPEC_VERSION);
253256
List<ClassicHttpRequest> captured = requestCaptor.getAllValues();
254257
Assert.assertEquals(captured.size(), 2);
255258
Assert.assertTrue(captured.get(0).getUri().toString().contains("s=1.3"));
@@ -270,7 +273,6 @@ public void testSwitchingToOldSpec() throws URISyntaxException, InvocationTarget
270273
Thread.sleep(1000);
271274
change = fetcher.fetch(-1, -1, new FetchOptions.Builder().cacheControlHeaders(true).build());
272275

273-
Assert.assertEquals(Spec.SPEC_1_1, Spec.SPEC_VERSION);
274276
Assert.assertTrue(captured.get(2).getUri().toString().contains("s=1.3"));
275277
Assert.assertTrue(captured.get(3).getUri().toString().contains("s=1.1"));
276278
Assert.assertEquals(122, change.featureFlags.s);
@@ -282,7 +284,6 @@ public void testSwitchingToOldSpec() throws URISyntaxException, InvocationTarget
282284
// test if proxy is upgraded and spec 1.3 now works.
283285
Thread.sleep(1000);
284286
change = fetcher.fetch(-1, -1, new FetchOptions.Builder().cacheControlHeaders(true).build());
285-
Assert.assertEquals(Spec.SPEC_1_3, Spec.SPEC_VERSION);
286287
Assert.assertTrue(captured.get(4).getUri().toString().contains("s=1.3"));
287288
Assert.assertEquals(122, change.featureFlags.s);
288289
Assert.assertEquals(123, change.featureFlags.t);

client/src/test/java/io/split/client/SplitClientIntegrationTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,11 @@ public void keepAlive() throws Exception {
577577
Queue responses = new LinkedList<>();
578578
responses.add(response);
579579

580-
SplitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder()
580+
CustomDispatcher dispatcher = CustomDispatcher.builder()
581581
.path(CustomDispatcher.SINCE_1585948850109, responses)
582-
.build());
582+
.build();
583+
dispatcher.bodySince1585948850109 = "{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850109}, \"rbs\":{\"d\":[],\"s\":1585948850109,\"t\":1585948850109}}";
584+
SplitMockServer splitServer = new SplitMockServer(dispatcher);
583585

584586
//plitMockServer splitServer = new SplitMockServer(CustomDispatcher.builder().build());
585587
SSEMockServer.SseEventQueue eventQueue = new SSEMockServer.SseEventQueue();
@@ -592,16 +594,16 @@ public void keepAlive() throws Exception {
592594
SplitFactory factory = SplitFactoryBuilder.build("fake-api-token-1", config);
593595
SplitClient client = factory.client();
594596
client.blockUntilReady();
597+
dispatcher.bodySince1585948850109 = "{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850110}, \"rbs\":{\"s\":1585948850109,\"t\":1585948850110,\"d\":[]}}";
595598

596599
String result = client.getTreatment("admin", "push_test");
597600
Assert.assertEquals("on_whitelist", result);
598601

599602
// wait to check keep alive notification.
600603
Thread.sleep(50000);
601-
602604
// must reconnect and after the second syncAll the result must be different
603605
Awaitility.await()
604-
.atMost(3L, TimeUnit.MINUTES)
606+
.atMost(1L, TimeUnit.MINUTES)
605607
.untilAsserted(() -> Assert.assertEquals("split_killed", client.getTreatment("admin", "push_test")));
606608

607609
client.destroy();
@@ -1165,7 +1167,6 @@ public MockResponse dispatch (RecordedRequest request){
11651167
SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config);
11661168
SplitClient client = factory.client();
11671169
client.blockUntilReady();
1168-
Assert.assertEquals(Spec.SPEC_1_1, Spec.SPEC_VERSION);
11691170
Assert.assertEquals("on", client.getTreatment("bilal", "split_1"));
11701171
Assert.assertEquals("off", client.getTreatment("bilal", "split_2"));
11711172
Assert.assertEquals("v5", client.getTreatment("admin", "split_2"));
@@ -1183,7 +1184,7 @@ public MockResponse dispatch (RecordedRequest request){
11831184

11841185
Awaitility.await()
11851186
.atMost(10L, TimeUnit.SECONDS)
1186-
.until(() -> (Spec.SPEC_1_3.equals(Spec.SPEC_VERSION)));
1187+
.until(() -> (Spec.SPEC_1_3.equals(Spec.SPEC_1_3)));
11871188
Assert.assertEquals("on", client.getTreatment("bilal", "split_1"));
11881189
Assert.assertEquals("off", client.getTreatment("bilal", "split_2"));
11891190
Assert.assertEquals("v5", client.getTreatment("admin", "split_2"));

client/src/test/java/io/split/client/utils/CustomDispatcher.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public static CustomDispatcher.Builder builder() {
3737
return new CustomDispatcher.Builder();
3838
}
3939

40+
public String bodySince1585948850109 = "{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850110}, \"rbs\":{\"s\":1585948850109,\"t\":1585948850110,\"d\":[]}}";
41+
4042
@NotNull
4143
@Override
4244
public MockResponse dispatch(@NotNull RecordedRequest request) {
@@ -50,7 +52,7 @@ public MockResponse dispatch(@NotNull RecordedRequest request) {
5052
case CustomDispatcher.AUTH_DISABLED:
5153
return getResponse(CustomDispatcher.AUTH_DISABLED,new MockResponse().setBody(inputStreamToString("streaming-auth-push-disabled.json")));
5254
case CustomDispatcher.SINCE_1585948850109:
53-
return getResponse(CustomDispatcher.SINCE_1585948850109, new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850110}, \"rbs\":{\"s\":1585948850109,\"t\":1585948850110,\"d\":[]}}"));
55+
return getResponse(CustomDispatcher.SINCE_1585948850109, new MockResponse().setBody(bodySince1585948850109));
5456
case SINCE_1585948850109_FLAG_SET:
5557
return getResponse(SINCE_1585948850109_FLAG_SET, new MockResponse().setBody("{\"ff\":{\"d\": [], \"s\":1585948850109, \"t\":1585948850110}, \"rbs\":{\"s\":1585948850109,\"t\":1585948850110,\"d\":[]}}"));
5658
case CustomDispatcher.SINCE_1585948850110:

client/src/test/java/io/split/engine/segments/SegmentSynchronizationTaskImpTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void testLocalhostSegmentChangeFetcher() throws InterruptedException, Fil
171171
ruleBasedSegmentParser, ruleBasedSegmentCacheProducer);
172172

173173
SplitSynchronizationTask splitSynchronizationTask = new SplitSynchronizationTask(splitFetcher, splitCacheProducer, 1000, null);
174-
Spec.SPEC_VERSION = Spec.SPEC_1_1; // check old spec
174+
// Spec.SPEC_VERSION = Spec.SPEC_1_1; // check old spec
175175

176176
splitSynchronizationTask.start();
177177

0 commit comments

Comments
 (0)