diff --git a/build.gradle b/build.gradle index 9e32031ee..9e880ea12 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ apply plugin: 'kotlin-android' apply from: 'spec.gradle' ext { - splitVersion = '5.1.0-alpha.1' + splitVersion = '5.1.0-alpha.2' } android { diff --git a/src/androidTest/java/tests/integration/IntegrationTest.java b/src/androidTest/java/tests/integration/IntegrationTest.java index 57327373b..07f8d8c37 100644 --- a/src/androidTest/java/tests/integration/IntegrationTest.java +++ b/src/androidTest/java/tests/integration/IntegrationTest.java @@ -286,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); diff --git a/src/androidTest/java/tests/integration/MySegmentsServerErrorTest.java b/src/androidTest/java/tests/integration/MySegmentsServerErrorTest.java index fe10d2bb4..ab1dc376b 100644 --- a/src/androidTest/java/tests/integration/MySegmentsServerErrorTest.java +++ b/src/androidTest/java/tests/integration/MySegmentsServerErrorTest.java @@ -195,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)); diff --git a/src/androidTest/java/tests/integration/rollout/RolloutCacheManagerIntegrationTest.java b/src/androidTest/java/tests/integration/rollout/RolloutCacheManagerIntegrationTest.java index e922053ec..2efb3d933 100644 --- a/src/androidTest/java/tests/integration/rollout/RolloutCacheManagerIntegrationTest.java +++ b/src/androidTest/java/tests/integration/rollout/RolloutCacheManagerIntegrationTest.java @@ -65,7 +65,7 @@ public void setUp() { @Test public void expirationPeriodIsUsed() throws InterruptedException { - test(getTimestampDaysAgo(1), RolloutCacheConfiguration.builder().expiration(1)); + test(getTimestampDaysAgo(1), RolloutCacheConfiguration.builder().expirationDays(1)); } @Test @@ -75,7 +75,7 @@ public void clearOnInitClearsCacheOnStartup() throws InterruptedException { @Test public void repeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElapsed() throws InterruptedException { - // Preload DB with update timestamp of 1 day ago + // Preload DB with update timestamp of now long oldTimestamp = System.currentTimeMillis(); preloadDb(oldTimestamp, 0L, 8000L); @@ -102,6 +102,7 @@ public void repeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElaps factory.client().on(SplitEvent.SDK_READY, TestingHelper.testTask(readyLatch)); boolean readyAwait = readyLatch.await(10, TimeUnit.SECONDS); + // Destroy factory factory.destroy(); mRequestCountdownLatch = new CountDownLatch(1); @@ -141,6 +142,23 @@ public void repeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElaps .getByName("rolloutCacheLastClearTimestamp").getLongValue()); } + @Test + public void sdkReadyFromCacheIsEmittedOnFreshInit() throws InterruptedException { + SplitFactory splitFactory = getSplitFactory(RolloutCacheConfiguration.builder().build()); + + CountDownLatch latch1 = new CountDownLatch(1); + CountDownLatch latch2 = new CountDownLatch(1); + splitFactory.client().on(SplitEvent.SDK_READY_FROM_CACHE, TestingHelper.testTask(latch1)); + splitFactory.client("two").on(SplitEvent.SDK_READY_FROM_CACHE, TestingHelper.testTask(latch2)); + mRequestCountdownLatch.countDown(); + + boolean await1 = latch1.await(10, TimeUnit.SECONDS); + boolean await2 = latch2.await(10, TimeUnit.SECONDS); + + assertTrue(await1); + assertTrue(await2); + } + private void test(long timestampDaysAgo, RolloutCacheConfiguration.Builder configBuilder) throws InterruptedException { // Preload DB with update timestamp of 1 day ago long oldTimestamp = timestampDaysAgo; diff --git a/src/androidTest/java/tests/workmanager/WorkManagerWrapperTest.java b/src/androidTest/java/tests/workmanager/WorkManagerWrapperTest.java index bc9867946..bf593bbf9 100644 --- a/src/androidTest/java/tests/workmanager/WorkManagerWrapperTest.java +++ b/src/androidTest/java/tests/workmanager/WorkManagerWrapperTest.java @@ -124,7 +124,6 @@ public void scheduleWorkSchedulesSplitsJob() { mWrapper.scheduleWork(); Data inputData = new Data.Builder() - .putLong("splitCacheExpiration", 864000) .putString("endpoint", "https://test.split.io/api") .putBoolean("shouldRecordTelemetry", true) .putStringArray("configuredFilterValues", new String[]{"set_1", "set_2"}) @@ -252,7 +251,6 @@ public void schedulingWithoutCertificatePinning() { mWrapper.scheduleWork(); Data inputData = new Data.Builder() - .putLong("splitCacheExpiration", 864000) .putString("endpoint", "https://test.split.io/api") .putBoolean("shouldRecordTelemetry", true) .putStringArray("configuredFilterValues", new String[]{"set_1", "set_2"}) diff --git a/src/main/java/io/split/android/client/RolloutCacheConfiguration.java b/src/main/java/io/split/android/client/RolloutCacheConfiguration.java index fde7d6acf..413034e26 100644 --- a/src/main/java/io/split/android/client/RolloutCacheConfiguration.java +++ b/src/main/java/io/split/android/client/RolloutCacheConfiguration.java @@ -5,16 +5,16 @@ public class RolloutCacheConfiguration { - private final int mExpiration; + private final int mExpirationDays; private final boolean mClearOnInit; private RolloutCacheConfiguration(int expiration, boolean clearOnInit) { - mExpiration = expiration; + mExpirationDays = expiration; mClearOnInit = clearOnInit; } - public int getExpiration() { - return mExpiration; + public int getExpirationDays() { + return mExpirationDays; } public boolean isClearOnInit() { @@ -38,15 +38,15 @@ private Builder() { /** * Set the expiration time for the rollout definitions cache, in days. Default is 10 days. - * @param expiration in days + * @param expirationDays in days * @return This builder */ - public Builder expiration(int expiration) { - if (expiration < MIN_EXPIRATION_DAYS) { + public Builder expirationDays(int expirationDays) { + if (expirationDays < MIN_EXPIRATION_DAYS) { Logger.w("Cache expiration must be at least 1 day. Using default value."); mExpiration = ServiceConstants.DEFAULT_ROLLOUT_CACHE_EXPIRATION; } else { - mExpiration = expiration; + mExpiration = expirationDays; } return this; diff --git a/src/main/java/io/split/android/client/SplitClientConfig.java b/src/main/java/io/split/android/client/SplitClientConfig.java index 94631c1d0..00af53115 100644 --- a/src/main/java/io/split/android/client/SplitClientConfig.java +++ b/src/main/java/io/split/android/client/SplitClientConfig.java @@ -254,7 +254,7 @@ public String trafficType() { @Deprecated public long cacheExpirationInSeconds() { - return TimeUnit.DAYS.toSeconds(rolloutCacheConfiguration().getExpiration()); + return TimeUnit.DAYS.toSeconds(rolloutCacheConfiguration().getExpirationDays()); } public long eventFlushInterval() { diff --git a/src/main/java/io/split/android/client/events/SplitEventsManager.java b/src/main/java/io/split/android/client/events/SplitEventsManager.java index f6150157e..c443aa8e2 100644 --- a/src/main/java/io/split/android/client/events/SplitEventsManager.java +++ b/src/main/java/io/split/android/client/events/SplitEventsManager.java @@ -191,6 +191,9 @@ private void triggerSdkReadyIfNeeded() { if ((wasTriggered(SplitInternalEvent.MY_SEGMENTS_UPDATED) || wasTriggered(SplitInternalEvent.MY_SEGMENTS_FETCHED) || wasTriggered(SplitInternalEvent.MY_LARGE_SEGMENTS_UPDATED)) && (wasTriggered(SplitInternalEvent.SPLITS_UPDATED) || wasTriggered(SplitInternalEvent.SPLITS_FETCHED)) && !isTriggered(SplitEvent.SDK_READY)) { + if (!isTriggered(SplitEvent.SDK_READY_FROM_CACHE)) { + trigger(SplitEvent.SDK_READY_FROM_CACHE); + } trigger(SplitEvent.SDK_READY); } } diff --git a/src/main/java/io/split/android/client/service/ServiceConstants.java b/src/main/java/io/split/android/client/service/ServiceConstants.java index 07cf4d1b8..ecfa4ad05 100644 --- a/src/main/java/io/split/android/client/service/ServiceConstants.java +++ b/src/main/java/io/split/android/client/service/ServiceConstants.java @@ -28,7 +28,6 @@ public class ServiceConstants { public final static String WORKER_PARAM_ENDPOINT = "endpoint"; public final static String WORKER_PARAM_IMPRESSIONS_PER_PUSH = "impressionsPerPush"; public final static String WORKER_PARAM_EVENTS_PER_PUSH = "eventsPerPush"; - public final static String WORKER_PARAM_SPLIT_CACHE_EXPIRATION = "splitCacheExpiration"; public static final String WORKER_PARAM_UNIQUE_KEYS_PER_PUSH = "unique_keys_per_push"; public static final String WORKER_PARAM_UNIQUE_KEYS_ESTIMATED_SIZE_IN_BYTES = "unique_keys_estimated_size_in_bytes"; public static final String WORKER_PARAM_ENCRYPTION_ENABLED = "encryptionEnabled"; diff --git a/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java b/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java index 75ecc45fc..934f9ac75 100644 --- a/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java +++ b/src/main/java/io/split/android/client/service/executor/SplitTaskFactoryImpl.java @@ -128,8 +128,8 @@ public ImpressionsRecorderTask createImpressionsRecorderTask() { @Override public SplitsSyncTask createSplitsSyncTask(boolean checkCacheExpiration) { - return SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorageContainer.getSplitsStorage(), checkCacheExpiration, - mSplitClientConfig.cacheExpirationInSeconds(), mSplitsFilterQueryStringFromConfig, mEventsManager, mSplitsStorageContainer.getTelemetryStorage()); + return SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorageContainer.getSplitsStorage(), + mSplitsFilterQueryStringFromConfig, mEventsManager, mSplitsStorageContainer.getTelemetryStorage()); } @Override diff --git a/src/main/java/io/split/android/client/service/splits/SplitsSyncHelper.java b/src/main/java/io/split/android/client/service/splits/SplitsSyncHelper.java index f65adb969..17e136fb7 100644 --- a/src/main/java/io/split/android/client/service/splits/SplitsSyncHelper.java +++ b/src/main/java/io/split/android/client/service/splits/SplitsSyncHelper.java @@ -185,17 +185,6 @@ private void updateStorage(boolean clearBeforeUpdate, SplitChange splitChange) { mSplitsStorage.update(mSplitChangeProcessor.process(splitChange)); } - public boolean cacheHasExpired(long storedChangeNumber, long updateTimestamp, long cacheExpirationInSeconds) { - long elapsed = now() - TimeUnit.MILLISECONDS.toSeconds(updateTimestamp); - return storedChangeNumber > -1 - && updateTimestamp > 0 - && (elapsed > cacheExpirationInSeconds); - } - - private long now() { - return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); - } - private void logError(String message) { Logger.e("Error while executing splits sync/update task: " + message); } diff --git a/src/main/java/io/split/android/client/service/splits/SplitsSyncTask.java b/src/main/java/io/split/android/client/service/splits/SplitsSyncTask.java index 1fe6d6cec..8a7a7bc6f 100644 --- a/src/main/java/io/split/android/client/service/splits/SplitsSyncTask.java +++ b/src/main/java/io/split/android/client/service/splits/SplitsSyncTask.java @@ -21,8 +21,6 @@ public class SplitsSyncTask implements SplitTask { private final String mSplitsFilterQueryStringFromConfig; private final SplitsStorage mSplitsStorage; - private final boolean mCheckCacheExpiration; - private final long mCacheExpirationInSeconds; private final SplitsSyncHelper mSplitsSyncHelper; @Nullable private final ISplitEventsManager mEventsManager; // Should only be null on background sync @@ -32,27 +30,21 @@ public class SplitsSyncTask implements SplitTask { public static SplitsSyncTask build(@NonNull SplitsSyncHelper splitsSyncHelper, @NonNull SplitsStorage splitsStorage, - boolean checkCacheExpiration, - long cacheExpirationInSeconds, String splitsFilterQueryString, @NonNull ISplitEventsManager eventsManager, @NonNull TelemetryRuntimeProducer telemetryRuntimeProducer) { - return new SplitsSyncTask(splitsSyncHelper, splitsStorage, checkCacheExpiration, cacheExpirationInSeconds, splitsFilterQueryString, telemetryRuntimeProducer, eventsManager, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); + return new SplitsSyncTask(splitsSyncHelper, splitsStorage, splitsFilterQueryString, telemetryRuntimeProducer, eventsManager, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); } public static SplitTask buildForBackground(@NonNull SplitsSyncHelper splitsSyncHelper, @NonNull SplitsStorage splitsStorage, - boolean checkCacheExpiration, - long cacheExpirationInSeconds, - String splitsFilterQueryString, + String splitsFilterQueryString, @NonNull TelemetryRuntimeProducer telemetryRuntimeProducer) { - return new SplitsSyncTask(splitsSyncHelper, splitsStorage, checkCacheExpiration, cacheExpirationInSeconds, splitsFilterQueryString, telemetryRuntimeProducer, null, 1); + return new SplitsSyncTask(splitsSyncHelper, splitsStorage, splitsFilterQueryString, telemetryRuntimeProducer, null, 1); } private SplitsSyncTask(@NonNull SplitsSyncHelper splitsSyncHelper, @NonNull SplitsStorage splitsStorage, - boolean checkCacheExpiration, - long cacheExpirationInSeconds, String splitsFilterQueryString, @NonNull TelemetryRuntimeProducer telemetryRuntimeProducer, @Nullable ISplitEventsManager eventsManager, @@ -60,8 +52,6 @@ private SplitsSyncTask(@NonNull SplitsSyncHelper splitsSyncHelper, mSplitsStorage = checkNotNull(splitsStorage); mSplitsSyncHelper = checkNotNull(splitsSyncHelper); - mCacheExpirationInSeconds = cacheExpirationInSeconds; - mCheckCacheExpiration = checkCacheExpiration; mSplitsFilterQueryStringFromConfig = splitsFilterQueryString; mEventsManager = eventsManager; mChangeChecker = new SplitsChangeChecker(); @@ -73,10 +63,6 @@ private SplitsSyncTask(@NonNull SplitsSyncHelper splitsSyncHelper, @NonNull public SplitTaskExecutionInfo execute() { long storedChangeNumber = mSplitsStorage.getTill(); - long updateTimestamp = mSplitsStorage.getUpdateTimestamp(); - - boolean shouldClearExpiredCache = mCheckCacheExpiration && - mSplitsSyncHelper.cacheHasExpired(storedChangeNumber, updateTimestamp, mCacheExpirationInSeconds); boolean splitsFilterHasChanged = splitsFilterHasChanged(mSplitsStorage.getSplitsFilterQueryString()); @@ -87,8 +73,8 @@ public SplitTaskExecutionInfo execute() { long startTime = System.currentTimeMillis(); SplitTaskExecutionInfo result = mSplitsSyncHelper.sync(storedChangeNumber, - splitsFilterHasChanged || shouldClearExpiredCache, - splitsFilterHasChanged || shouldClearExpiredCache, mOnDemandFetchBackoffMaxRetries); + splitsFilterHasChanged, + splitsFilterHasChanged, mOnDemandFetchBackoffMaxRetries); mTelemetryRuntimeProducer.recordSyncLatency(OperationType.SPLITS, System.currentTimeMillis() - startTime); if (result.getStatus() == SplitTaskExecutionStatus.SUCCESS) { diff --git a/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java b/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java index c195c2f5b..2c9b9db04 100644 --- a/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java +++ b/src/main/java/io/split/android/client/service/synchronizer/RolloutCacheManagerImpl.java @@ -101,7 +101,7 @@ private boolean validateExpiration() { // calculate elapsed time since last update long daysSinceLastUpdate = TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - lastUpdateTimestamp); - if (lastUpdateTimestamp > 0 && daysSinceLastUpdate >= mConfig.getExpiration()) { + if (lastUpdateTimestamp > 0 && daysSinceLastUpdate >= mConfig.getExpirationDays()) { Logger.v("Clearing rollout definitions cache due to expiration"); return true; } else if (mConfig.isClearOnInit()) { diff --git a/src/main/java/io/split/android/client/service/synchronizer/WorkManagerWrapper.java b/src/main/java/io/split/android/client/service/synchronizer/WorkManagerWrapper.java index c342f1cbc..c65042116 100644 --- a/src/main/java/io/split/android/client/service/synchronizer/WorkManagerWrapper.java +++ b/src/main/java/io/split/android/client/service/synchronizer/WorkManagerWrapper.java @@ -199,7 +199,6 @@ private SplitTaskType taskTypeFromTags(Set tags) { private Data buildSplitSyncInputData() { Data.Builder dataBuilder = new Data.Builder(); - dataBuilder.putLong(ServiceConstants.WORKER_PARAM_SPLIT_CACHE_EXPIRATION, mSplitClientConfig.cacheExpirationInSeconds()); dataBuilder.putString(ServiceConstants.WORKER_PARAM_ENDPOINT, mSplitClientConfig.endpoint()); dataBuilder.putBoolean(ServiceConstants.SHOULD_RECORD_TELEMETRY, mSplitClientConfig.shouldRecordTelemetry()); dataBuilder.putString(ServiceConstants.WORKER_PARAM_CONFIGURED_FILTER_TYPE, (mFilter != null) ? mFilter.getType().queryStringField() : null); diff --git a/src/main/java/io/split/android/client/service/workmanager/SplitWorker.java b/src/main/java/io/split/android/client/service/workmanager/SplitWorker.java index 1b51bdfa9..b24b231ce 100644 --- a/src/main/java/io/split/android/client/service/workmanager/SplitWorker.java +++ b/src/main/java/io/split/android/client/service/workmanager/SplitWorker.java @@ -23,7 +23,6 @@ public abstract class SplitWorker extends Worker { private final SplitRoomDatabase mDatabase; private final HttpClient mHttpClient; private final String mEndpoint; - private final long mCacheExpirationInSeconds; protected SplitTask mSplitTask; @@ -36,8 +35,6 @@ public SplitWorker(@NonNull Context context, String apiKey = inputData.getString(ServiceConstants.WORKER_PARAM_API_KEY); mEndpoint = inputData.getString(ServiceConstants.WORKER_PARAM_ENDPOINT); mDatabase = SplitRoomDatabase.getDatabase(context, databaseName); - mCacheExpirationInSeconds = inputData.getLong(ServiceConstants.WORKER_PARAM_SPLIT_CACHE_EXPIRATION, - ServiceConstants.DEFAULT_SPLITS_CACHE_EXPIRATION_IN_SECONDS); mHttpClient = buildHttpClient(apiKey, buildCertPinningConfig(inputData.getString(ServiceConstants.WORKER_PARAM_CERTIFICATE_PINS))); } @@ -64,10 +61,6 @@ public String getEndPoint() { return mEndpoint; } - public long getCacheExpirationInSeconds() { - return mCacheExpirationInSeconds; - } - private static HttpClient buildHttpClient(String apiKey, @Nullable CertificatePinningConfiguration certificatePinningConfiguration) { HttpClientImpl.Builder builder = new HttpClientImpl.Builder(); diff --git a/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorker.java b/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorker.java index 6724aafe0..d5f3f6d27 100644 --- a/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorker.java +++ b/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorker.java @@ -22,7 +22,6 @@ public SplitsSyncWorker(@NonNull Context context, new FetcherProvider(getHttpClient(), getEndPoint()), new SplitChangeProcessorProvider().provideSplitChangeProcessor(params.configuredFilterType(), params.configuredFilterValues()), new SyncHelperProvider(), - getCacheExpirationInSeconds(), params.flagsSpec()); mSplitTask = builder.getTask(); diff --git a/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilder.java b/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilder.java index d1fa25c7a..9d0a8a026 100644 --- a/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilder.java +++ b/src/main/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilder.java @@ -15,7 +15,6 @@ */ class SplitsSyncWorkerTaskBuilder { - private final long mCacheExpirationInSeconds; private final StorageProvider mStorageProvider; private final FetcherProvider mFetcherProvider; private final SplitChangeProcessor mSplitChangeProcessor; @@ -26,13 +25,11 @@ class SplitsSyncWorkerTaskBuilder { FetcherProvider fetcherProvider, SplitChangeProcessor splitChangeProcessor, SyncHelperProvider splitsSyncHelperProvider, - long cacheExpirationInSeconds, String flagsSpec) { mStorageProvider = storageProvider; mFetcherProvider = fetcherProvider; mSplitsSyncHelperProvider = splitsSyncHelperProvider; mSplitChangeProcessor = splitChangeProcessor; - mCacheExpirationInSeconds = cacheExpirationInSeconds; mFlagsSpec = flagsSpec; } @@ -51,8 +48,6 @@ SplitTask getTask() { return SplitsSyncTask.buildForBackground(splitsSyncHelper, splitsStorage, - false, - mCacheExpirationInSeconds, splitsFilterQueryString, telemetryStorage); } catch (URISyntaxException e) { diff --git a/src/test/java/io/split/android/client/RolloutCacheConfigurationTest.java b/src/test/java/io/split/android/client/RolloutCacheConfigurationTest.java index 8dd533451..ba342d1c8 100644 --- a/src/test/java/io/split/android/client/RolloutCacheConfigurationTest.java +++ b/src/test/java/io/split/android/client/RolloutCacheConfigurationTest.java @@ -11,16 +11,16 @@ public class RolloutCacheConfigurationTest { @Test public void defaultValues() { RolloutCacheConfiguration config = RolloutCacheConfiguration.builder().build(); - assertEquals(10, config.getExpiration()); + assertEquals(10, config.getExpirationDays()); assertFalse(config.isClearOnInit()); } @Test public void expirationIsCorrectlySet() { RolloutCacheConfiguration.Builder builder = RolloutCacheConfiguration.builder(); - builder.expiration(1); + builder.expirationDays(1); RolloutCacheConfiguration config = builder.build(); - assertEquals(1, config.getExpiration()); + assertEquals(1, config.getExpirationDays()); } @Test @@ -34,8 +34,8 @@ public void clearOnInitIsCorrectlySet() { @Test public void negativeExpirationIsSetToDefault() { RolloutCacheConfiguration.Builder builder = RolloutCacheConfiguration.builder(); - builder.expiration(-1); + builder.expirationDays(-1); RolloutCacheConfiguration config = builder.build(); - assertEquals(10, config.getExpiration()); + assertEquals(10, config.getExpirationDays()); } } diff --git a/src/test/java/io/split/android/client/SplitClientConfigTest.java b/src/test/java/io/split/android/client/SplitClientConfigTest.java index 690455513..1e69b9c12 100644 --- a/src/test/java/io/split/android/client/SplitClientConfigTest.java +++ b/src/test/java/io/split/android/client/SplitClientConfigTest.java @@ -229,17 +229,17 @@ public void observerCacheExpirationPeriodMatchesDedupeTimeIntervalWhenDedupeTime public void rolloutCacheConfigurationDefaults() { RolloutCacheConfiguration config = SplitClientConfig.builder().build().rolloutCacheConfiguration(); - assertEquals(10, config.getExpiration()); + assertEquals(10, config.getExpirationDays()); assertFalse(config.isClearOnInit()); } @Test public void rolloutCacheConfigurationExpirationIsCorrectlySet() { RolloutCacheConfiguration config = SplitClientConfig.builder() - .rolloutCacheConfiguration(RolloutCacheConfiguration.builder().expiration(1).clearOnInit(true).build()) + .rolloutCacheConfiguration(RolloutCacheConfiguration.builder().expirationDays(1).clearOnInit(true).build()) .build().rolloutCacheConfiguration(); - assertEquals(1, config.getExpiration()); + assertEquals(1, config.getExpirationDays()); assertTrue(config.isClearOnInit()); } @@ -251,7 +251,7 @@ public void nullRolloutCacheConfigurationSetsDefault() { .rolloutCacheConfiguration(null) .build().rolloutCacheConfiguration(); - assertEquals(10, config.getExpiration()); + assertEquals(10, config.getExpirationDays()); assertFalse(config.isClearOnInit()); assertEquals(1, logMessages.size()); } diff --git a/src/test/java/io/split/android/client/events/EventsManagerTest.java b/src/test/java/io/split/android/client/events/EventsManagerTest.java index c9cca29c6..1f896825d 100644 --- a/src/test/java/io/split/android/client/events/EventsManagerTest.java +++ b/src/test/java/io/split/android/client/events/EventsManagerTest.java @@ -51,6 +51,7 @@ public void eventOnReady() { execute(shouldStop, intervalExecutionTime, maxExecutionTime, eventManager, SplitEvent.SDK_READY); assertTrue(eventManager.eventAlreadyTriggered(SplitEvent.SDK_READY)); + assertTrue(eventManager.eventAlreadyTriggered(SplitEvent.SDK_READY_FROM_CACHE)); assertFalse(eventManager.eventAlreadyTriggered(SplitEvent.SDK_READY_TIMED_OUT)); } diff --git a/src/test/java/io/split/android/client/service/SplitSyncTaskTest.java b/src/test/java/io/split/android/client/service/SplitSyncTaskTest.java index 9c354f744..9bbe08901 100644 --- a/src/test/java/io/split/android/client/service/SplitSyncTaskTest.java +++ b/src/test/java/io/split/android/client/service/SplitSyncTaskTest.java @@ -67,7 +67,7 @@ public void correctExecution() throws HttpFetcherException { // And updateTimestamp is 0 // Retry is off, so splitSyncHelper.sync should be called mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer); + mQueryString, mEventsManager, mTelemetryRuntimeProducer); when(mSplitsStorage.getTill()).thenReturn(-1L); when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L); when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); @@ -77,38 +77,6 @@ public void correctExecution() throws HttpFetcherException { verify(mSplitsSyncHelper, times(1)).sync(-1, false, false, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); } - @Test - public void cleanOldCacheDisabled() throws HttpFetcherException { - // Cache should not be cleared when cache expired - mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - false, 100L, mQueryString, mEventsManager, mTelemetryRuntimeProducer); - when(mSplitsStorage.getTill()).thenReturn(300L); - when(mSplitsStorage.getUpdateTimestamp()).thenReturn(100L); - when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); - // This value function checks that cache is expired, here we simulate cache expired - when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(true); - - mTask.execute(); - - verify(mSplitsStorage, never()).clear(); - } - - @Test - public void cleanOldCacheEnabled() throws HttpFetcherException { - - // Cache should be cleared when cache expired - mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - true, 100L, mQueryString, mEventsManager, mTelemetryRuntimeProducer); - when(mSplitsStorage.getTill()).thenReturn(100L); - when(mSplitsStorage.getUpdateTimestamp()).thenReturn(100L); // Dummy value clearing depends on cacheHasExpired function value - when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); - when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(true); - - mTask.execute(); - - verify(mSplitsSyncHelper, times(1)).sync(100L, true, true, ServiceConstants.ON_DEMAND_FETCH_BACKOFF_MAX_RETRIES); - } - @Test public void cleanSplitsWhenQueryStringHasChanged() throws HttpFetcherException { // Splits have to be cleared when query string on db is != than current one on current sdk client instance @@ -119,11 +87,10 @@ public void cleanSplitsWhenQueryStringHasChanged() throws HttpFetcherException { Map params = new HashMap<>(); params.put("since", 100L); mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - true, 100L, otherQs, mEventsManager, mTelemetryRuntimeProducer); + otherQs, mEventsManager, mTelemetryRuntimeProducer); when(mSplitsStorage.getTill()).thenReturn(100L); when(mSplitsStorage.getUpdateTimestamp()).thenReturn(1111L); when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); - when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(false); mTask.execute(); @@ -137,11 +104,10 @@ public void noClearSplitsWhenQueryStringHasNotChanged() throws HttpFetcherExcept // Setting up cache not expired mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - true,100L, mQueryString, mEventsManager, mTelemetryRuntimeProducer); + mQueryString, mEventsManager, mTelemetryRuntimeProducer); when(mSplitsStorage.getTill()).thenReturn(100L); when(mSplitsStorage.getUpdateTimestamp()).thenReturn(1111L); when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); - when(mSplitsSyncHelper.cacheHasExpired(anyLong(), anyLong(), anyLong())).thenReturn(false); mTask.execute(); @@ -156,7 +122,7 @@ public void splitUpdatedNotified() throws HttpFetcherException { // And updateTimestamp is 0 // Retry is off, so splitSyncHelper.sync should be called mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer); + mQueryString, mEventsManager, mTelemetryRuntimeProducer); when(mSplitsStorage.getTill()).thenReturn(-1L).thenReturn(100L); when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L); when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); @@ -174,7 +140,7 @@ public void splitFetchdNotified() throws HttpFetcherException { // And updateTimestamp is 0 // Retry is off, so splitSyncHelper.sync should be called mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer); + mQueryString, mEventsManager, mTelemetryRuntimeProducer); when(mSplitsStorage.getTill()).thenReturn(100L); when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L); when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); @@ -188,7 +154,7 @@ public void splitFetchdNotified() throws HttpFetcherException { @Test public void syncIsTrackedInTelemetry() { mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer); + mQueryString, mEventsManager, mTelemetryRuntimeProducer); when(mSplitsStorage.getTill()).thenReturn(100L); when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L); when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); @@ -202,7 +168,7 @@ public void syncIsTrackedInTelemetry() { @Test public void recordSuccessInTelemetry() { mTask = SplitsSyncTask.build(mSplitsSyncHelper, mSplitsStorage, - false, 1000, mQueryString, mEventsManager, mTelemetryRuntimeProducer); + mQueryString, mEventsManager, mTelemetryRuntimeProducer); when(mSplitsStorage.getTill()).thenReturn(-1L); when(mSplitsStorage.getUpdateTimestamp()).thenReturn(0L); when(mSplitsStorage.getSplitsFilterQueryString()).thenReturn(mQueryString); diff --git a/src/test/java/io/split/android/client/service/SplitsSyncHelperTest.java b/src/test/java/io/split/android/client/service/SplitsSyncHelperTest.java index 1b2d9e104..2ff74c234 100644 --- a/src/test/java/io/split/android/client/service/SplitsSyncHelperTest.java +++ b/src/test/java/io/split/android/client/service/SplitsSyncHelperTest.java @@ -14,7 +14,6 @@ import static org.mockito.Mockito.when; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentMatcher; @@ -174,46 +173,6 @@ public void shouldClearStorageAfterFetch() throws HttpFetcherException { assertEquals(SplitTaskExecutionStatus.SUCCESS, result.getStatus()); } - @Test - public void cacheExpired() throws HttpFetcherException { - - // change number > -1 should clear cache - // when cache expired - - long cacheExpInSeconds = 10000; - long updateTimestamp = System.currentTimeMillis() / 1000 - cacheExpInSeconds - 1; - boolean expired = mSplitsSyncHelper.cacheHasExpired(100, updateTimestamp, cacheExpInSeconds); - - Assert.assertTrue(expired); - } - - @Test - public void cacheNotExpired() throws HttpFetcherException { - - // change number > -1 should clear cache - // only when cache expired - - long cacheExpInSeconds = 10000; - long updateTimestamp = System.currentTimeMillis() - cacheExpInSeconds * 1000 + 1000; - boolean expired = mSplitsSyncHelper.cacheHasExpired(100, updateTimestamp, cacheExpInSeconds); - - Assert.assertFalse(expired); - } - - @Test - public void cacheExpiredButChangeNumber() throws HttpFetcherException { - - // change number = -1 means no previous cache available - // so, should no clear cache - // even if it's expired - - long cacheExpInSeconds = 10000; - long updateTimestamp = System.currentTimeMillis() / 1000 - cacheExpInSeconds - 1000; - boolean expired = mSplitsSyncHelper.cacheHasExpired(-1, updateTimestamp, cacheExpInSeconds); - - Assert.assertFalse(expired); - } - @Test public void errorIsRecordedInTelemetry() throws HttpFetcherException { when(mSplitsFetcher.execute(mDefaultParams, null)) diff --git a/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt b/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt index aa710c378..b89da1649 100644 --- a/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt +++ b/src/test/java/io/split/android/client/service/synchronizer/RolloutCacheManagerTest.kt @@ -177,7 +177,7 @@ class RolloutCacheManagerTest { } private fun getCacheManager(expiration: Int, clearOnInit: Boolean): RolloutCacheManager { - return RolloutCacheManagerImpl(mGeneralInfoStorage, RolloutCacheConfiguration.builder().expiration(expiration).clearOnInit(clearOnInit).build(), mCleanUpDatabaseTask, mEncryptionMigrationTask, mSplitsCache, mSegmentsCache) + return RolloutCacheManagerImpl(mGeneralInfoStorage, RolloutCacheConfiguration.builder().expirationDays(expiration).clearOnInit(clearOnInit).build(), mCleanUpDatabaseTask, mEncryptionMigrationTask, mSplitsCache, mSegmentsCache) } private fun createMockedTimestamp(period: Long): Long { diff --git a/src/test/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilderTest.java b/src/test/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilderTest.java index 47f83e41f..1b950b365 100644 --- a/src/test/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilderTest.java +++ b/src/test/java/io/split/android/client/service/workmanager/splits/SplitsSyncWorkerTaskBuilderTest.java @@ -56,7 +56,6 @@ public void getTaskUsesStorageProviderForSplitsStorage() { mFetcherProvider, mSplitChangeProcessor, mSplitsSyncHelperProvider, - 0, null); builder.getTask(); @@ -71,7 +70,6 @@ public void getTaskUsesFetcherProviderForFetcher() throws URISyntaxException { mFetcherProvider, mSplitChangeProcessor, mSplitsSyncHelperProvider, - 0, null); builder.getTask(); @@ -86,7 +84,6 @@ public void getTaskUsesStorageProviderForTelemetryStorage() { mFetcherProvider, mSplitChangeProcessor, mSplitsSyncHelperProvider, - 0, null); builder.getTask(); @@ -104,7 +101,6 @@ public void getTaskUsesSplitsSyncHelperProviderForSplitsSyncHelper() throws URIS mFetcherProvider, mSplitChangeProcessor, mSplitsSyncHelperProvider, - 0, "1.5"); builder.getTask(); @@ -127,7 +123,6 @@ public void getTaskReturnsNullWhenURISyntaxExceptionIsThrown() throws URISyntaxE mFetcherProvider, mSplitChangeProcessor, mSplitsSyncHelperProvider, - 0, null); SplitTask task = builder.getTask(); @@ -145,12 +140,11 @@ public void getTaskUsesSplitSyncTaskStaticMethod() { mFetcherProvider, mSplitChangeProcessor, mSplitsSyncHelperProvider, - 250, "2.5"); builder.getTask(); - mockedStatic.verify(() -> SplitsSyncTask.buildForBackground(splitsSyncHelper, mSplitsStorage, false, 250, "filterQueryString", mTelemetryStorage)); + mockedStatic.verify(() -> SplitsSyncTask.buildForBackground(splitsSyncHelper, mSplitsStorage, "filterQueryString", mTelemetryStorage)); } } }