Skip to content

Commit

Permalink
Merge pull request #722 rollout cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea authored Dec 11, 2024
2 parents c8001de + 03bea99 commit 3fbd789
Show file tree
Hide file tree
Showing 75 changed files with 1,602 additions and 346 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply plugin: 'kotlin-android'
apply from: 'spec.gradle'

ext {
splitVersion = '5.0.1'
splitVersion = '5.1.0-alpha.2'
}

android {
Expand Down
9 changes: 5 additions & 4 deletions src/androidTest/java/fake/SynchronizerSpyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import io.split.android.client.api.Key;
import io.split.android.client.dtos.Event;
import io.split.android.client.impressions.Impression;
import io.split.android.client.service.synchronizer.Synchronizer;
Expand Down Expand Up @@ -122,12 +123,12 @@ public void unregisterAttributesSynchronizer(String userKey) {
}

@Override
public void registerMySegmentsSynchronizer(String userKey, MySegmentsSynchronizer mySegmentsSynchronizer) {
((MySegmentsSynchronizerRegistry) mSynchronizer).registerMySegmentsSynchronizer(userKey, mySegmentsSynchronizer);
public void registerMySegmentsSynchronizer(Key key, MySegmentsSynchronizer mySegmentsSynchronizer) {
((MySegmentsSynchronizerRegistry) mSynchronizer).registerMySegmentsSynchronizer(key, mySegmentsSynchronizer);
}

@Override
public void unregisterMySegmentsSynchronizer(String userKey) {
((MySegmentsSynchronizerRegistry) mSynchronizer).unregisterMySegmentsSynchronizer(userKey);
public void unregisterMySegmentsSynchronizer(Key key) {
((MySegmentsSynchronizerRegistry) mSynchronizer).unregisterMySegmentsSynchronizer(key);
}
}
61 changes: 34 additions & 27 deletions src/androidTest/java/helper/IntegrationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import fake.HttpClientMock;
import fake.HttpResponseMock;
Expand Down Expand Up @@ -390,40 +391,45 @@ public static <T> Set<T> asSet(T... elements) {
return result;
}

/**
* A simple interface to allow us to define the response for a given path
*/
public interface ResponseClosure {
HttpResponseMock onResponse(URI uri,
HttpMethod httpMethod,
String body);
public static long getTimestampDaysAgo(int days) {
return System.currentTimeMillis() - TimeUnit.DAYS.toMillis(days);
}

static String getSinceFromUri(URI uri) {
try {
return parse(uri.getQuery()).get("since");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

public static String getSinceFromUri(URI uri) {
try {
return parse(uri.getQuery()).get("since");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}

static Map<String, String> parse(String query) throws UnsupportedEncodingException {
Map<String, String> queryPairs = new HashMap<>();
String[] pairs = query.split("&");
static Map<String, String> parse(String query) throws UnsupportedEncodingException {
Map<String, String> queryPairs = new HashMap<>();
String[] pairs = query.split("&");

for (String pair : pairs) {
int idx = pair.indexOf("=");
try {
String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
for (String pair : pairs) {
int idx = pair.indexOf("=");
try {
String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");

queryPairs.put(key, value);
} catch (Exception e) {
e.printStackTrace();
}
queryPairs.put(key, value);
} catch (Exception e) {
e.printStackTrace();
}

return queryPairs;
}

return queryPairs;
}

/**
* A simple interface to allow us to define the response for a given path
*/
public interface ResponseClosure {
HttpResponseMock onResponse(URI uri,
HttpMethod httpMethod,
String body);
}

/**
Expand All @@ -435,5 +441,6 @@ public interface StreamingResponseClosure {

public static class ServicePath {
public static final String MEMBERSHIPS = "memberships";
public static final String SPLIT_CHANGES = "splitChanges";
}
}
10 changes: 9 additions & 1 deletion src/androidTest/java/helper/TestableSplitConfigBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.Constructor;

import io.split.android.client.RolloutCacheConfiguration;
import io.split.android.client.ServiceEndpoints;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.SyncConfig;
Expand Down Expand Up @@ -64,6 +65,7 @@ public class TestableSplitConfigBuilder {
private String mPrefix = "";
private CertificatePinningConfiguration mCertificatePinningConfiguration;
private long mImpressionsDedupeTimeInterval = ServiceConstants.DEFAULT_IMPRESSIONS_DEDUPE_TIME_INTERVAL;
private RolloutCacheConfiguration mRolloutCacheConfiguration = RolloutCacheConfiguration.builder().build();

public TestableSplitConfigBuilder() {
mServiceEndpoints = ServiceEndpoints.builder().build();
Expand Down Expand Up @@ -274,6 +276,11 @@ public TestableSplitConfigBuilder impressionsDedupeTimeInterval(long impressions
return this;
}

public TestableSplitConfigBuilder rolloutCacheConfiguration(RolloutCacheConfiguration rolloutCacheConfiguration) {
this.mRolloutCacheConfiguration = rolloutCacheConfiguration;
return this;
}

public SplitClientConfig build() {
Constructor constructor = SplitClientConfig.class.getDeclaredConstructors()[0];
constructor.setAccessible(true);
Expand Down Expand Up @@ -329,7 +336,8 @@ public SplitClientConfig build() {
mPrefix,
mObserverCacheExpirationPeriod,
mCertificatePinningConfiguration,
mImpressionsDedupeTimeInterval);
mImpressionsDedupeTimeInterval,
mRolloutCacheConfiguration);

Logger.instance().setLevel(mLogLevel);
return config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static helper.IntegrationHelper.ResponseClosure.getSinceFromUri;

import static helper.IntegrationHelper.getSinceFromUri;

import android.content.Context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public void usingNullPrefixResultsInIgnoredPrefix() {

private static String[] getDbList(Context context) {
// remove -journal dbs since we're not interested in them
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return Arrays.stream(context.databaseList()).filter(db -> !db.endsWith("-journal")).toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static helper.IntegrationHelper.ResponseClosure.getSinceFromUri;
import static helper.IntegrationHelper.getSinceFromUri;

import android.content.Context;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tests.integration;

import static helper.IntegrationHelper.ResponseClosure.getSinceFromUri;
import static helper.IntegrationHelper.getSinceFromUri;

import android.content.Context;

Expand Down Expand Up @@ -95,7 +95,6 @@ public void firstRequestChangeNumber() throws Exception {
String apiKey = apiKeyAndDb.first;
SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, INITIAL_CHANGE_NUMBER));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP, System.currentTimeMillis()));

Expand Down
5 changes: 1 addition & 4 deletions src/androidTest/java/tests/integration/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public void testAll() throws Exception {

SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 2));
SplitClient client;
SplitManager manager;
Expand Down Expand Up @@ -249,7 +248,6 @@ public void testNoReadyFromCache() throws Exception {

SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, -1));
SplitClient client;
SplitManager manager;
Expand Down Expand Up @@ -288,7 +286,7 @@ public void testNoReadyFromCache() throws Exception {
latch.await(40, TimeUnit.SECONDS);

Assert.assertTrue(client.isReady());
Assert.assertFalse(readyFromCacheTask.isOnPostExecutionCalled);
Assert.assertTrue(readyFromCacheTask.isOnPostExecutionCalled);
Assert.assertTrue(readyTask.isOnPostExecutionCalled);
Assert.assertFalse(readyTimeOutTask.isOnPostExecutionCalled);

Expand All @@ -304,7 +302,6 @@ public void testGetTreatmentFromCache() throws Exception {
mContext = InstrumentationRegistry.getInstrumentation().getContext();
mRoomDb = DatabaseHelper.getTestDatabase(mContext);
mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 10));
mRoomDb.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, 1));

SplitChange change = Json.fromJson(mJsonChanges.get(0), SplitChange.class);
List<SplitEntity> entities = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public void test() throws Exception {

SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, -1));

ImpressionListenerHelper impListener = new ImpressionListenerHelper();
Expand Down Expand Up @@ -196,7 +195,7 @@ public void test() throws Exception {
Assert.fail("Impressions request timeout");
}

Assert.assertFalse(readyFromCacheTask.isOnPostExecutionCalled);
Assert.assertTrue(readyFromCacheTask.isOnPostExecutionCalled);
Assert.assertEquals("on_s1", treatments.get(0));
Assert.assertEquals("on_s1", treatments.get(1));
Assert.assertEquals("on_s1", treatments.get(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public void testAll() throws Exception {
SplitRoomDatabase.class)
.build();
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.CHANGE_NUMBER_INFO, 2));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_FILTER_QUERY_STRING, expectedQs));
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.SPLITS_UPDATE_TIMESTAMP, System.currentTimeMillis()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static java.lang.Thread.sleep;

import static helper.IntegrationHelper.ResponseClosure.getSinceFromUri;
import static helper.IntegrationHelper.getSinceFromUri;

import android.content.Context;

Expand Down
3 changes: 0 additions & 3 deletions src/androidTest/java/tests/integration/TrackTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import io.split.android.client.dtos.Event;
import io.split.android.client.events.SplitEvent;
import io.split.android.client.exceptions.SplitInstantiationException;
import io.split.android.client.storage.db.GeneralInfoEntity;
import io.split.android.client.storage.db.SplitRoomDatabase;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
Expand Down Expand Up @@ -120,7 +119,6 @@ public void test() throws Exception {
String apiKey = IntegrationHelper.dummyApiKey();
SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));

ImpressionListenerHelper impListener = new ImpressionListenerHelper();

Expand Down Expand Up @@ -220,7 +218,6 @@ public void largeNumberInPropertiesTest() throws InterruptedException, SplitInst
String apiKey = IntegrationHelper.dummyApiKey();
SplitRoomDatabase splitRoomDatabase = DatabaseHelper.getTestDatabase(mContext);
splitRoomDatabase.clearAllTables();
splitRoomDatabase.generalInfoDao().update(new GeneralInfoEntity(GeneralInfoEntity.DATBASE_MIGRATION_STATUS, GeneralInfoEntity.DATBASE_MIGRATION_STATUS_DONE));

final String url = mWebServer.url("/").url().toString();

Expand Down
Loading

0 comments on commit 3fbd789

Please sign in to comment.