Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 3, 2024
1 parent f4ea326 commit a9d47fe
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import helper.IntegrationHelper;
import helper.SplitEventTaskHelper;
import helper.TestableSplitConfigBuilder;
import io.split.android.client.ServiceEndpoints;
import io.split.android.client.SplitClient;
import io.split.android.client.SplitClientConfig;
import io.split.android.client.SplitFactory;
Expand All @@ -32,15 +33,21 @@
import io.split.android.client.storage.db.SplitRoomDatabase;
import io.split.android.client.storage.db.attributes.AttributesEntity;
import io.split.android.client.utils.Json;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

public class AttributesIntegrationTest {

private Context mContext;
private SplitRoomDatabase mRoomDb;
private SplitFactory mSplitFactory;
private MockWebServer mWebServer;

@Before
public void setup() {
setupServer();
mContext = InstrumentationRegistry.getInstrumentation().getContext();
mRoomDb = DatabaseHelper.getTestDatabase(mContext);
mRoomDb.clearAllTables();
Expand All @@ -51,7 +58,7 @@ public void testPersistentAttributes() throws InterruptedException {
insertSplitsFromFileIntoDB();
CountDownLatch readyLatch = new CountDownLatch(1);
SplitClient client = getSplitClient(readyLatch, true, null);
readyLatch.await(5, TimeUnit.SECONDS);
readyLatch.await(10, TimeUnit.SECONDS);

// 1. Evaluate without attrs
Assert.assertEquals("on", client.getTreatment("workm"));
Expand Down Expand Up @@ -224,8 +231,12 @@ private void setAttributesInClientAndEvaluate(SplitClient client) {

private SplitClient getSplitClient(CountDownLatch readyLatch, boolean persistenceEnabled, String matchingKey) {
if (mSplitFactory == null) {
final String url = mWebServer.url("/").url().toString();
ServiceEndpoints endpoints = ServiceEndpoints.builder()
.apiEndpoint(url).eventsEndpoint(url).build();
SplitClientConfig config = new TestableSplitConfigBuilder()
.enableDebug()
.serviceEndpoints(endpoints)
.featuresRefreshRate(9999)
.segmentsRefreshRate(9999)
.impressionsRefreshRate(9999)
Expand Down Expand Up @@ -272,4 +283,24 @@ private List<Split> getSplitListFromJson() {

return changes.splits;
}

private void setupServer() {
mWebServer = new MockWebServer();

final Dispatcher dispatcher = new Dispatcher() {

@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().contains("/" + IntegrationHelper.ServicePath.MEMBERSHIPS)) {
return new MockResponse().setResponseCode(200).setBody(IntegrationHelper.dummyAllSegments());
} else if (request.getPath().contains("/splitChanges")) {
return new MockResponse().setResponseCode(200)
.setBody(IntegrationHelper.emptySplitChanges(-1, 10000));
} else {
return new MockResponse().setResponseCode(404);
}
}
};
mWebServer.setDispatcher(dispatcher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void validateCache(SplitTaskExecutionListener listener) {
execute();
Logger.v("Rollout cache manager: Migrating encryption");
mEncryptionMigrationTask.execute();
Logger.v("Rollout cache manager: validation finished");
Logger.v("Rollout cache manager: Validation finished");
listener.taskExecuted(SplitTaskExecutionInfo.success(SplitTaskType.GENERIC_TASK));
} catch (Exception ex) {
Logger.e("Error occurred validating cache: " + ex.getMessage());
Expand All @@ -97,15 +97,18 @@ public SplitTaskExecutionInfo execute() {
}

private boolean validateExpiration() {
// calculate elapsed time since last update
long lastUpdateTimestamp = mGeneralInfoStorage.getSplitsUpdateTimestamp();
// calculate elapsed time since last update
long daysSinceLastUpdate = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - lastUpdateTimestamp);

if (daysSinceLastUpdate > mConfig.getExpiration()) {
if (lastUpdateTimestamp > 0 && daysSinceLastUpdate > mConfig.getExpiration()) {
Logger.v("Clearing rollout definitions cache due to expiration");
return true;
} else if (mConfig.clearOnInit()) {
long lastCacheClearTimestamp = mGeneralInfoStorage.getRolloutCacheLastClearTimestamp();
if (lastCacheClearTimestamp < 1) {
return true;
}
long daysSinceCacheClear = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - lastCacheClearTimestamp);

// don't clear too soon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,30 @@ class RolloutCacheManagerTest {
verify(mEncryptionMigrationTask).execute()
}

@Test
fun `default value for update timestamp does not clear cache`() {
`when`(mGeneralInfoStorage.splitsUpdateTimestamp).thenReturn(0L)
`when`(mGeneralInfoStorage.rolloutCacheLastClearTimestamp).thenReturn(0L)
mRolloutCacheManager = getCacheManager(10, false)

mRolloutCacheManager.validateCache(mock(SplitTaskExecutionListener::class.java))

verify(mSplitsCache, times(0)).clear()
verify(mSegmentsCache, times(0)).clear()
}

@Test
fun `default value for last clear timestamp clears cache when clearOnInit is true`() {
`when`(mGeneralInfoStorage.splitsUpdateTimestamp).thenReturn(createMockedTimestamp(System.currentTimeMillis()))
`when`(mGeneralInfoStorage.rolloutCacheLastClearTimestamp).thenReturn(0L)
mRolloutCacheManager = getCacheManager(10, true)

mRolloutCacheManager.validateCache(mock(SplitTaskExecutionListener::class.java))

verify(mSplitsCache).clear()
verify(mSegmentsCache).clear()
}

private fun getCacheManager(expiration: Int, clearOnInit: Boolean): RolloutCacheManager {
return RolloutCacheManagerImpl(mGeneralInfoStorage, RolloutCacheConfiguration.builder().expiration(expiration).clearOnInit(clearOnInit).build(), mCleanUpDatabaseTask, mEncryptionMigrationTask, mSplitsCache, mSegmentsCache)
}
Expand Down

0 comments on commit a9d47fe

Please sign in to comment.