Skip to content

Commit

Permalink
New tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Nov 27, 2024
1 parent 489bc31 commit a5f7c5e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/main/java/io/split/android/client/SplitClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ public long impressionsDedupeTimeInterval() {
return mImpressionsDedupeTimeInterval;
}

public boolean clearOnInit() {
return false; // TODO: to be implemented in the future
}

public static final class Builder {

static final int PROXY_PORT_DEFAULT = 80;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class RolloutCacheManagerConfig {
}

public static RolloutCacheManagerConfig from(SplitClientConfig splitClientConfig) {
return new RolloutCacheManagerConfig(splitClientConfig.cacheExpirationInSeconds(), false); // TODO
return new RolloutCacheManagerConfig(splitClientConfig.cacheExpirationInSeconds(), splitClientConfig.clearOnInit());
}

public long getCacheExpirationInDays() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ public void validateCache(SplitTaskExecutionListener listener) {
@Override
public SplitTaskExecutionInfo execute() {
try {
validate();
boolean expired = validateExpiration();
if (expired) {
clear();
}
} catch (Exception e) {
Logger.e("Error occurred validating cache: " + e.getMessage());

Expand All @@ -69,30 +72,33 @@ public SplitTaskExecutionInfo execute() {
return SplitTaskExecutionInfo.success(SplitTaskType.GENERIC_TASK);
}

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

if (daysSinceLastUpdate > mConfig.getCacheExpirationInDays()) {
clear();
Logger.v("Clearing rollout definitions cache due to expiration");
return true;
} else if (mConfig.isClearOnInit()) {
long lastCacheClearTimestamp = mGeneralInfoStorage.getRolloutCacheLastClearTimestamp();
long daysSinceCacheClear = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - lastCacheClearTimestamp);

// don't clear too soon
if (daysSinceCacheClear > MIN_CACHE_CLEAR_DAYS) {
clear();
Logger.v("Forcing rollout definitions cache clear");
return true;
}
}

return false;
}

private void clear() {
if (mStorages.length > 0) {
for (RolloutDefinitionsCache storage : mStorages) {
storage.clear();
}
mGeneralInfoStorage.setRolloutCacheLastClearTimestamp(System.currentTimeMillis());
for (RolloutDefinitionsCache storage : mStorages) {
storage.clear();
}
mGeneralInfoStorage.setRolloutCacheLastClearTimestamp(System.currentTimeMillis());
Logger.v("Rollout definitions cache cleared");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import io.split.android.fake.SplitTaskExecutorStub
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.ArgumentMatchers.argThat
import org.mockito.Mockito.longThat
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
Expand Down Expand Up @@ -90,6 +93,45 @@ class RolloutCacheManagerTest {
verify(mSegmentsCache, times(1)).clear()
}

@Test
fun `exception during clear still calls listener`() {
val mockedTimestamp = createMockedTimestamp(1L)
`when`(mGeneralInfoStorage.splitsUpdateTimestamp).thenReturn(mockedTimestamp)
`when`(mGeneralInfoStorage.rolloutCacheLastClearTimestamp).thenReturn(0L).thenReturn(TimeUnit.HOURS.toMillis(TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis()) - 1))
mRolloutCacheManager = getCacheManager(10L, true)

val listener = mock(SplitTaskExecutionListener::class.java)
`when`(mSplitsCache.clear()).thenThrow(RuntimeException("Exception during clear"))

mRolloutCacheManager.validateCache(listener)

verify(listener).taskExecuted(any())
}

@Test
fun `validateCache updates last clear timestamp when storages are cleared`() {
val mockedTimestamp = createMockedTimestamp(1L)
`when`(mGeneralInfoStorage.splitsUpdateTimestamp).thenReturn(mockedTimestamp)
`when`(mGeneralInfoStorage.rolloutCacheLastClearTimestamp).thenReturn(0L).thenReturn(TimeUnit.HOURS.toMillis(TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis()) - 1))
mRolloutCacheManager = getCacheManager(10L, true)

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

verify(mGeneralInfoStorage).setRolloutCacheLastClearTimestamp(longThat { it > 0 })
}

@Test
fun `validateCache does not update last clear timestamp when storages are not cleared`() {
val mockedTimestamp = createMockedTimestamp(1L)
`when`(mGeneralInfoStorage.splitsUpdateTimestamp).thenReturn(mockedTimestamp)
`when`(mGeneralInfoStorage.rolloutCacheLastClearTimestamp).thenReturn(0L).thenReturn(TimeUnit.HOURS.toMillis(TimeUnit.MILLISECONDS.toHours(System.currentTimeMillis()) - 1))
mRolloutCacheManager = getCacheManager(10L, false)

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

verify(mGeneralInfoStorage, times(0)).setRolloutCacheLastClearTimestamp(anyLong())
}

private fun getCacheManager(expiration: Long, clearOnInit: Boolean): RolloutCacheManager {
return RolloutCacheManagerImpl(mGeneralInfoStorage, RolloutCacheManagerConfig(expiration, clearOnInit), mSplitTaskExecutor, mSplitsCache, mSegmentsCache)
}
Expand Down

0 comments on commit a5f7c5e

Please sign in to comment.