Skip to content

Commit

Permalink
added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Al committed Dec 13, 2024
1 parent 2dc15df commit 249ab03
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 5 deletions.
237 changes: 237 additions & 0 deletions client/src/test/java/io/split/client/SplitClientIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
import io.split.storages.pluggable.CustomStorageWrapperImp;
import io.split.storages.pluggable.domain.EventConsumer;
import io.split.storages.pluggable.domain.ImpressionConsumer;

import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.awaitility.Awaitility;
import org.glassfish.grizzly.utils.Pair;
import org.glassfish.jersey.media.sse.OutboundEvent;
Expand All @@ -21,6 +25,10 @@
import java.io.IOException;
import java.net.URISyntaxException;

import java.nio.Buffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -771,6 +779,235 @@ public void getTreatmentFlagSetWithPolling() throws Exception {
splitServer.stop();
}

@Test
public void ImpressionToggleOptimizedModeTest() throws Exception {
String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8);
List<RecordedRequest> allRequests = new ArrayList<>();

Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
allRequests.add(request);
switch (request.getPath()) {
case "/api/splitChanges?s=1.1&since=-1":
return new MockResponse().setResponseCode(200).setBody(splits);
case "/api/splitChanges?s=1.1&since=1602796638344":
return new MockResponse().setResponseCode(200).setBody("{\"splits\": [], \"since\":1602796638344, \"till\":1602796638344}");
case "/api/testImpressions/bulk":
return new MockResponse().setResponseCode(200);
case "/api/testImpressions/count":
return new MockResponse().setResponseCode(200);
case "/v1/keys/ss":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/usage":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/config":
return new MockResponse().setResponseCode(200);
}
return new MockResponse().setResponseCode(404);
}
};

MockWebServer server = new MockWebServer();
server.setDispatcher(dispatcher);

server.start();
String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort());
SplitClientConfig config = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.endpoint(serverURL, serverURL)
.authServiceURL(String.format("%s/api/auth/enabled", serverURL))
.telemetryURL(serverURL + "/v1")
.streamingEnabled(false)
.featuresRefreshRate(5)
.impressionsMode(ImpressionsManager.Mode.OPTIMIZED)
.build();

SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config);
SplitClient client = factory.client();
client.blockUntilReady();

Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle", null));
Assert.assertEquals("off", client.getTreatment("user2", "impression_toggle_on", null));
Assert.assertEquals("off", client.getTreatment("user3", "impression_toggle_off", null));
Thread.sleep(1000);
client.destroy();
boolean check1 = false, check2 = false;
for (int i=0; i < allRequests.size(); i++ ) {
if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) {
check1 = true;
String body = allRequests.get(i).getBody().readUtf8();
Assert.assertTrue(body.contains("without_impression_toggle"));
Assert.assertTrue(body.contains("impression_toggle_on"));
Assert.assertFalse(body.contains("impression_toggle_off"));
}
if (allRequests.get(i).getPath().equals("/v1/keys/ss")) {
check2 = true;
String body = allRequests.get(i).getBody().readUtf8();
Assert.assertFalse(body.contains("without_impression_toggle"));
Assert.assertFalse(body.contains("impression_toggle_on"));
Assert.assertTrue(body.contains("impression_toggle_off"));
}
}
server.shutdown();
Assert.assertTrue(check1);
Assert.assertTrue(check2);
}

@Test
public void ImpressionToggleDebugModeTest() throws Exception {
String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8);
List<RecordedRequest> allRequests = new ArrayList<>();

Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
allRequests.add(request);
switch (request.getPath()) {
case "/api/splitChanges?s=1.1&since=-1":
return new MockResponse().setResponseCode(200).setBody(splits);
case "/api/splitChanges?s=1.1&since=1602796638344":
return new MockResponse().setResponseCode(200).setBody("{\"splits\": [], \"since\":1602796638344, \"till\":1602796638344}");
case "/api/testImpressions/bulk":
return new MockResponse().setResponseCode(200);
case "/api/testImpressions/count":
return new MockResponse().setResponseCode(200);
case "/v1/keys/ss":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/usage":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/config":
return new MockResponse().setResponseCode(200);
}
return new MockResponse().setResponseCode(404);
}
};

MockWebServer server = new MockWebServer();
server.setDispatcher(dispatcher);

server.start();
String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort());
SplitClientConfig config = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.endpoint(serverURL, serverURL)
.telemetryURL(serverURL + "/v1")
.authServiceURL(String.format("%s/api/auth/enabled", serverURL))
.streamingEnabled(false)
.featuresRefreshRate(5)
.impressionsMode(ImpressionsManager.Mode.DEBUG)
.build();

SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config);
SplitClient client = factory.client();
client.blockUntilReady();

Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle", null));
Assert.assertEquals("off", client.getTreatment("user2", "impression_toggle_on", null));
Assert.assertEquals("off", client.getTreatment("user3", "impression_toggle_off", null));
Thread.sleep(1000);
client.destroy();
boolean check1 = false, check2 = false;
for (int i=0; i < allRequests.size(); i++ ) {
if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) {
check1 = true;
String body = allRequests.get(i).getBody().readUtf8();
Assert.assertTrue(body.contains("without_impression_toggle"));
Assert.assertTrue(body.contains("impression_toggle_on"));
Assert.assertFalse(body.contains("impression_toggle_off"));
}
if (allRequests.get(i).getPath().equals("/v1/keys/ss")) {
check2 = true;
String body = allRequests.get(i).getBody().readUtf8();
Assert.assertFalse(body.contains("without_impression_toggle"));
Assert.assertFalse(body.contains("impression_toggle_on"));
Assert.assertTrue(body.contains("impression_toggle_off"));
}
}
server.shutdown();
Assert.assertTrue(check1);
Assert.assertTrue(check2);
}

@Test
public void ImpressionToggleNoneModeTest() throws Exception {
String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8);
List<RecordedRequest> allRequests = new ArrayList<>();

Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) {
allRequests.add(request);
switch (request.getPath()) {
case "/api/splitChanges?s=1.1&since=-1":
return new MockResponse().setResponseCode(200).setBody(splits);
case "/api/splitChanges?s=1.1&since=1602796638344":
return new MockResponse().setResponseCode(200).setBody("{\"splits\": [], \"since\":1602796638344, \"till\":1602796638344}");
case "/api/testImpressions/bulk":
return new MockResponse().setResponseCode(200);
case "/api/testImpressions/count":
return new MockResponse().setResponseCode(200);
case "/v1/keys/ss":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/usage":
return new MockResponse().setResponseCode(200);
case "/v1/metrics/config":
return new MockResponse().setResponseCode(200);
}
return new MockResponse().setResponseCode(404);
}
};

MockWebServer server = new MockWebServer();
server.setDispatcher(dispatcher);

server.start();
String serverURL = String.format("http://%s:%s", server.getHostName(), server.getPort());
SplitClientConfig config = SplitClientConfig.builder()
.setBlockUntilReadyTimeout(10000)
.endpoint(serverURL, serverURL)
.telemetryURL(serverURL + "/v1")
.authServiceURL(String.format("%s/api/auth/enabled", serverURL))
.streamingEnabled(false)
.featuresRefreshRate(5)
.impressionsMode(ImpressionsManager.Mode.NONE)
.build();

SplitFactory factory = SplitFactoryBuilder.build("fake-api-token", config);
SplitClient client = factory.client();
client.blockUntilReady();

Assert.assertEquals("off", client.getTreatment("user1", "without_impression_toggle", null));
Assert.assertEquals("off", client.getTreatment("user2", "impression_toggle_on", null));
Assert.assertEquals("off", client.getTreatment("user3", "impression_toggle_off", null));
Thread.sleep(1000);
client.destroy();
boolean check1 = false, check2 = false, check3 = false;
for (int i=0; i < allRequests.size(); i++ ) {
if (allRequests.get(i).getPath().equals("/api/testImpressions/bulk") ) {
check1 = true;
}
if (allRequests.get(i).getPath().equals("/v1/keys/ss")) {
check2 = true;
String body = allRequests.get(i).getBody().readUtf8();
Assert.assertTrue(body.contains("without_impression_toggle"));
Assert.assertTrue(body.contains("impression_toggle_on"));
Assert.assertTrue(body.contains("impression_toggle_off"));
}
if (allRequests.get(i).getPath().equals("/api/testImpressions/count")) {
check3 = true;
String body = allRequests.get(i).getBody().readUtf8();
Assert.assertTrue(body.contains("without_impression_toggle"));
Assert.assertTrue(body.contains("impression_toggle_on"));
Assert.assertTrue(body.contains("impression_toggle_off"));
}
}
server.shutdown();
Assert.assertFalse(check1);
Assert.assertTrue(check2);
Assert.assertTrue(check3);
}

private SSEMockServer buildSSEMockServer(SSEMockServer.SseEventQueue eventQueue) {
return new SSEMockServer(eventQueue, (token, version, channel) -> {
if (!"1.1".equals(version)) {
Expand Down
32 changes: 32 additions & 0 deletions client/src/test/java/io/split/client/SplitManagerImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import com.google.common.collect.Lists;
import io.split.client.api.SplitView;
import io.split.client.dtos.Split;
import io.split.client.dtos.SplitChange;
import io.split.client.utils.Json;
import io.split.engine.ConditionsTestUtil;
import io.split.engine.SDKReadinessGates;
import io.split.engine.experiments.ParsedCondition;
import io.split.engine.experiments.ParsedSplit;
import io.split.engine.experiments.SplitParser;
import io.split.engine.matchers.AllKeysMatcher;
import io.split.engine.matchers.CombiningMatcher;
import io.split.grammar.Treatments;
Expand All @@ -16,6 +20,10 @@
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -24,6 +32,8 @@
import java.util.Map;
import java.util.concurrent.TimeoutException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -220,4 +230,26 @@ public void splitCallWithEmptySets() {
private ParsedCondition getTestCondition(String treatment) {
return ParsedCondition.createParsedConditionForTests(CombiningMatcher.of(new AllKeysMatcher()), Lists.newArrayList(ConditionsTestUtil.partition(treatment, 10)));
}

@Test
public void ImpressionToggleParseTest() throws IOException {
SplitParser parser = new SplitParser();
String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8);
SplitChange change = Json.fromJson(splits, SplitChange.class);
SplitCacheConsumer splitCacheConsumer = mock(SplitCacheConsumer.class);
for (Split split : change.splits) {
ParsedSplit parsedSplit = parser.parse(split);
when(splitCacheConsumer.get(split.name)).thenReturn(parsedSplit);
}
SplitManagerImpl splitManager = new SplitManagerImpl(splitCacheConsumer,
mock(SplitClientConfig.class),
mock(SDKReadinessGates.class), TELEMETRY_STORAGE);

SplitView splitView = splitManager.split("without_impression_toggle");
assertTrue(splitView.trackImpression);
splitView = splitManager.split("impression_toggle_on");
assertTrue(splitView.trackImpression);
splitView = splitManager.split("impression_toggle_off");
assertFalse(splitView.trackImpression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.junit.Test;
import org.mockito.Mockito;

import javax.validation.constraints.AssertTrue;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -645,19 +646,25 @@ public void ImpressionToggleParseTest() throws IOException {
SplitParser parser = new SplitParser();
String splits = new String(Files.readAllBytes(Paths.get("src/test/resources/splits_imp_toggle.json")), StandardCharsets.UTF_8);
SplitChange change = Json.fromJson(splits, SplitChange.class);
boolean check1 = false, check2 = false, check3 = false;
for (Split split : change.splits) {
// should not cause exception
ParsedSplit parsedSplit = parser.parse(split);
if (split.name.equals("without_impression_toggle")) {
assertTrue(split.trackImpression);
assertTrue(parsedSplit.trackImpression());
check1 = true;
}
if (split.name.equals("impression_toggle_on")) {
assertTrue(split.trackImpression);
assertTrue(parsedSplit.trackImpression());
check2 = true;
}
if (split.name.equals("impression_toggle_off")) {
assertFalse(split.trackImpression);
assertFalse(parsedSplit.trackImpression());
check3 = true;
}
}
assertTrue(check1);
assertTrue(check2);
assertTrue(check3);
}

public void setMatcherTest(Condition c, io.split.engine.matchers.Matcher m) {
Expand Down
2 changes: 1 addition & 1 deletion client/src/test/resources/splits_imp_toggle.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,5 @@
}
],
"since": -1,
"till": 1585948850109
"till": 1602796638344
}

0 comments on commit 249ab03

Please sign in to comment.