Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 2, 2024
1 parent 4d0b415 commit 428bb5b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/main/java/io/split/android/client/SplitFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import io.split.android.client.shared.SplitClientContainer;
import io.split.android.client.shared.SplitClientContainerImpl;
import io.split.android.client.shared.UserConsent;
import io.split.android.client.storage.cipher.EncryptionMigrationTask;
import io.split.android.client.storage.cipher.SplitCipher;
import io.split.android.client.storage.common.SplitStorageContainer;
import io.split.android.client.storage.db.SplitRoomDatabase;
Expand Down Expand Up @@ -186,7 +185,6 @@ private SplitFactoryImpl(@NonNull String apiToken, @NonNull Key key, @NonNull Sp
config, splitApiFacade, mStorageContainer, splitsFilterQueryStringFromConfig,
getFlagsSpec(testingConfig), mEventsManagerCoordinator, filters, flagSetsFilter, testingConfig);

cleanUpDabase(splitTaskExecutor, splitTaskFactory);
WorkManagerWrapper workManagerWrapper = factoryHelper.buildWorkManagerWrapper(context, config, apiToken, databaseName, filters);
SplitSingleThreadTaskExecutor splitSingleThreadTaskExecutor = new SplitSingleThreadTaskExecutor();
splitSingleThreadTaskExecutor.pause();
Expand Down Expand Up @@ -326,14 +324,19 @@ public void run() {
Runnable initializer = new Runnable() {
@Override
public void run() {
Logger.v("Running initialization thread");
RolloutCacheManagerImpl rolloutCacheManager = new RolloutCacheManagerImpl(config, mStorageContainer, new EncryptionMigrationTask(apiToken, splitDatabase, config.encryptionEnabled(), splitCipher));
RolloutCacheManagerImpl rolloutCacheManager = new RolloutCacheManagerImpl(config,
mStorageContainer,
splitTaskFactory.createCleanUpDatabaseTask(System.currentTimeMillis() / 1000),
splitTaskFactory.createEncryptionMigrationTask(apiToken, splitDatabase, config.encryptionEnabled(), splitCipher));

rolloutCacheManager.validateCache(new SplitTaskExecutionListener() {
@Override
public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) {
mEventsManagerCoordinator.notifyInternalEvent(SplitInternalEvent.ENCRYPTION_MIGRATION_DONE);

splitTaskExecutor.resume();
splitSingleThreadTaskExecutor.resume();

mSyncManager.start();
mLifecycleManager.register(mSyncManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import io.split.android.client.service.splits.SplitsSyncTask;
import io.split.android.client.service.splits.SplitsUpdateTask;
import io.split.android.client.service.telemetry.TelemetryTaskFactory;
import io.split.android.client.storage.cipher.EncryptionMigrationTask;
import io.split.android.client.storage.cipher.SplitCipher;
import io.split.android.client.storage.db.SplitRoomDatabase;

public interface SplitTaskFactory extends TelemetryTaskFactory, ImpressionsTaskFactory {

Expand All @@ -29,4 +32,6 @@ public interface SplitTaskFactory extends TelemetryTaskFactory, ImpressionsTaskF
FilterSplitsInCacheTask createFilterSplitsInCacheTask();

CleanUpDatabaseTask createCleanUpDatabaseTask(long maxTimestamp);

EncryptionMigrationTask createEncryptionMigrationTask(String sdkKey, SplitRoomDatabase splitRoomDatabase, boolean encryptionEnabled, SplitCipher splitCipher);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
import io.split.android.client.service.telemetry.TelemetryStatsRecorderTask;
import io.split.android.client.service.telemetry.TelemetryTaskFactory;
import io.split.android.client.service.telemetry.TelemetryTaskFactoryImpl;
import io.split.android.client.storage.cipher.EncryptionMigrationTask;
import io.split.android.client.storage.cipher.SplitCipher;
import io.split.android.client.storage.common.SplitStorageContainer;
import io.split.android.client.storage.db.SplitRoomDatabase;
import io.split.android.client.telemetry.storage.TelemetryRuntimeProducer;
import io.split.android.client.telemetry.storage.TelemetryStorage;

Expand Down Expand Up @@ -203,6 +206,11 @@ public SplitInPlaceUpdateTask createSplitsUpdateTask(Split featureFlag, long sin
return new SplitInPlaceUpdateTask(mSplitsStorageContainer.getSplitsStorage(), mSplitChangeProcessor, mEventsManager, mTelemetryRuntimeProducer, featureFlag, since);
}

@Override
public EncryptionMigrationTask createEncryptionMigrationTask(String sdkKey, SplitRoomDatabase splitRoomDatabase, boolean encryptionEnabled, SplitCipher splitCipher) {
return new EncryptionMigrationTask(sdkKey, splitRoomDatabase, encryptionEnabled, splitCipher);
}

@NonNull
private TelemetryTaskFactory initializeTelemetryTaskFactory(@NonNull SplitClientConfig splitClientConfig, @Nullable Map<SplitFilter.Type, SplitFilter> filters, TelemetryStorage telemetryStorage) {
final TelemetryTaskFactory mTelemetryTaskFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.concurrent.TimeUnit;

import io.split.android.client.SplitClientConfig;
import io.split.android.client.service.CleanUpDatabaseTask;
import io.split.android.client.service.executor.SplitTask;
import io.split.android.client.service.executor.SplitTaskExecutionInfo;
import io.split.android.client.service.executor.SplitTaskExecutionListener;
Expand All @@ -30,20 +31,30 @@ public class RolloutCacheManagerImpl implements RolloutCacheManager, SplitTask {
@NonNull
private final RolloutDefinitionsCache[] mStorages;
@NonNull
private final CleanUpDatabaseTask mCleanUpDatabaseTask;
@NonNull
private final EncryptionMigrationTask mEncryptionMigrationTask;

public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @NonNull SplitStorageContainer storageContainer, @NonNull EncryptionMigrationTask encryptionMigrationTask) {
public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @NonNull SplitStorageContainer storageContainer,
@NonNull CleanUpDatabaseTask cleanUpDatabaseTask,
@NonNull EncryptionMigrationTask encryptionMigrationTask) {
this(storageContainer.getGeneralInfoStorage(),
RolloutCacheManagerConfig.from(splitClientConfig),
cleanUpDatabaseTask,
encryptionMigrationTask,
storageContainer.getSplitsStorage(),
storageContainer.getMySegmentsStorageContainer(),
storageContainer.getMyLargeSegmentsStorageContainer());
}

@VisibleForTesting
RolloutCacheManagerImpl(@NonNull GeneralInfoStorage generalInfoStorage, @NonNull RolloutCacheManagerConfig config, @NonNull EncryptionMigrationTask encryptionMigrationTask, @NonNull RolloutDefinitionsCache... storages) {
RolloutCacheManagerImpl(@NonNull GeneralInfoStorage generalInfoStorage,
@NonNull RolloutCacheManagerConfig config,
@NonNull CleanUpDatabaseTask clean,
@NonNull EncryptionMigrationTask encryptionMigrationTask,
@NonNull RolloutDefinitionsCache... storages) {
mGeneralInfoStorage = checkNotNull(generalInfoStorage);
mCleanUpDatabaseTask = checkNotNull(clean);
mEncryptionMigrationTask = checkNotNull(encryptionMigrationTask);
mStorages = checkNotNull(storages);
mConfig = checkNotNull(config);
Expand All @@ -52,9 +63,19 @@ public RolloutCacheManagerImpl(@NonNull SplitClientConfig splitClientConfig, @No
@WorkerThread
@Override
public void validateCache(SplitTaskExecutionListener listener) {
execute();
mEncryptionMigrationTask.execute();
listener.taskExecuted(SplitTaskExecutionInfo.success(SplitTaskType.GENERIC_TASK));
try {
Logger.v("Rollout cache manager: Executing clearing task");
mCleanUpDatabaseTask.execute();
Logger.v("Rollout cache manager: Validating cache");
execute();
Logger.v("Rollout cache manager: Migrating encryption");
mEncryptionMigrationTask.execute();
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());
listener.taskExecuted(SplitTaskExecutionInfo.error(SplitTaskType.GENERIC_TASK));
}
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package io.split.android.client.service.synchronizer

import io.split.android.client.service.CleanUpDatabaseTask
import io.split.android.client.service.executor.SplitTaskExecutionListener
import io.split.android.client.service.executor.SplitTaskExecutor
import io.split.android.client.storage.RolloutDefinitionsCache
import io.split.android.client.storage.cipher.EncryptionMigrationTask
import io.split.android.client.storage.general.GeneralInfoStorage
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
Expand All @@ -25,11 +23,13 @@ class RolloutCacheManagerTest {
private lateinit var mSplitsCache: RolloutDefinitionsCache
private lateinit var mSegmentsCache: RolloutDefinitionsCache
private lateinit var mEncryptionMigrationTask: EncryptionMigrationTask
private lateinit var mCleanUpDatabaseTask: CleanUpDatabaseTask

@Before
fun setup() {
mGeneralInfoStorage = mock(GeneralInfoStorage::class.java)
mEncryptionMigrationTask = mock(EncryptionMigrationTask::class.java);
mEncryptionMigrationTask = mock(EncryptionMigrationTask::class.java)
mCleanUpDatabaseTask = mock(CleanUpDatabaseTask::class.java)
mSplitsCache = mock(RolloutDefinitionsCache::class.java)
mSegmentsCache = mock(RolloutDefinitionsCache::class.java)
}
Expand Down Expand Up @@ -133,8 +133,26 @@ class RolloutCacheManagerTest {
verify(mGeneralInfoStorage, times(0)).setRolloutCacheLastClearTimestamp(anyLong())
}

@Test
fun `validateCache executes cleanUpDatabaseTask`() {
mRolloutCacheManager = getCacheManager(10L, false)

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

verify(mCleanUpDatabaseTask).execute()
}

@Test
fun `validateCache executes encryptionMigrationTask`() {
mRolloutCacheManager = getCacheManager(10L, false)

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

verify(mEncryptionMigrationTask).execute()
}

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

private fun createMockedTimestamp(period: Long): Long {
Expand Down

0 comments on commit 428bb5b

Please sign in to comment.