Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 10, 2024
1 parent 9f371a0 commit ed204a7
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ public class StrategyImpressionManager implements ImpressionManager, PeriodicTra
private final PeriodicTracker[] mPeriodicTracker;

public StrategyImpressionManager(Pair<ProcessStrategy, PeriodicTracker> noneComponents, Pair<ProcessStrategy, PeriodicTracker> strategy) {
mProcessStrategy = strategy.first;
mNoneStrategy = noneComponents.first;
mPeriodicTracker = new PeriodicTracker[]{noneComponents.second, strategy.second};
this(noneComponents.first, noneComponents.second, strategy.first, strategy.second);
}

StrategyImpressionManager(ProcessStrategy noneStrategy, PeriodicTracker noneTracker, ProcessStrategy strategy, PeriodicTracker strategyTracker) {
mProcessStrategy = strategy;
mNoneStrategy = noneStrategy;
mPeriodicTracker = new PeriodicTracker[]{noneTracker, strategyTracker};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ public Pair<ProcessStrategy, PeriodicTracker> getNoneComponents() {
mSplitTaskFactory,
mImpressionsCounter,
uniqueKeysTracker,
mImpressionStrategyConfig.isUserConsentGranted()
);
mImpressionStrategyConfig.isUserConsentGranted());
NoneTracker noneTracker = new NoneTracker(
mSplitTaskExecutor,
mSplitTaskFactory,
Expand All @@ -107,6 +106,6 @@ public Pair<ProcessStrategy, PeriodicTracker> getNoneComponents() {
mImpressionStrategyConfig.getImpressionsCounterRefreshRate(),
mImpressionStrategyConfig.getUniqueKeysRefreshRate(),
mImpressionStrategyConfig.isUserConsentGranted());
return new Pair<>(noneStrategy, noneTracker); // TODO
return new Pair<>(noneStrategy, noneTracker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
import io.split.android.client.service.executor.SplitTaskType;
import io.split.android.client.service.http.HttpFetcher;
import io.split.android.client.service.http.HttpRecorder;
import io.split.android.client.service.impressions.ImpressionManager;
import io.split.android.client.service.impressions.ImpressionManagerConfig;
import io.split.android.client.service.impressions.ImpressionsCountRecorderTask;
import io.split.android.client.service.impressions.ImpressionsMode;
import io.split.android.client.service.impressions.ImpressionsRecorderTask;
import io.split.android.client.service.impressions.SaveImpressionsCountTask;
import io.split.android.client.service.impressions.StrategyImpressionManager;
import io.split.android.client.service.mysegments.LoadMySegmentsTask;
import io.split.android.client.service.mysegments.MySegmentsSyncTask;
import io.split.android.client.service.mysegments.MySegmentsTaskFactory;
Expand Down Expand Up @@ -141,7 +141,7 @@ public class SynchronizerTest {
private AttributesSynchronizerRegistryImpl mAttributesSynchronizerRegistry;
@Mock
private FeatureFlagsSynchronizer mFeatureFlagsSynchronizer;
ImpressionManager mImpressionManager;
private StrategyImpressionManager mImpressionManager;

private final String mUserKey = "user_key";

Expand Down Expand Up @@ -195,7 +195,7 @@ public void setup(SplitClientConfig splitClientConfig, ImpressionManagerConfig.M
.thenReturn(mRetryTimerSplitsUpdate);
when(mRetryBackoffFactory.createWithFixedInterval(any(), eq(1), eq(3)))
.thenReturn(mRetryTimerEventsRecorder);
mImpressionManager = Mockito.mock(ImpressionManager.class);
mImpressionManager = Mockito.mock(StrategyImpressionManager.class);

mSynchronizer = new SynchronizerImpl(splitClientConfig, mTaskExecutor, mSingleThreadedTaskExecutor,
mTaskFactory, mWorkManagerWrapper, mRetryBackoffFactory, mTelemetryRuntimeProducer, mAttributesSynchronizerRegistry, mMySegmentsSynchronizerRegistry, mImpressionManager, mFeatureFlagsSynchronizer, mSplitStorageContainer.getEventsStorage());
Expand Down Expand Up @@ -587,7 +587,7 @@ public void destroy() {
.synchronizeInBackground(false)
.build();
setup(config);
ImpressionManager impressionManager = mock(ImpressionManager.class);
StrategyImpressionManager impressionManager = mock(StrategyImpressionManager.class);
when(mRetryBackoffFactory.create(any(), anyInt()))
.thenReturn(mRetryTimerSplitsSync)
.thenReturn(mRetryTimerSplitsUpdate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ class StrategyImpressionManagerTest {
@Mock
private lateinit var strategy: ProcessStrategy

@Mock
private lateinit var noneStrategy: ProcessStrategy

@Mock
private lateinit var noneTracker: PeriodicTracker

private lateinit var impressionManager: StrategyImpressionManager

@Before
fun setUp() {
MockitoAnnotations.openMocks(this)
impressionManager = StrategyImpressionManager(strategy, tracker)
impressionManager = StrategyImpressionManager(noneStrategy, noneTracker, strategy, tracker)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ class DebugStrategyTest {
@Mock
private lateinit var telemetryRuntimeProducer: TelemetryRuntimeProducer

@Mock
private lateinit var tracker: PeriodicTracker

@Mock
private lateinit var impressionsObserver: ImpressionsObserver

Expand All @@ -52,7 +49,6 @@ class DebugStrategyTest {
taskExecutor,
taskFactory,
telemetryRuntimeProducer,
tracker
)
}

Expand Down Expand Up @@ -88,41 +84,6 @@ class DebugStrategyTest {
verify(taskExecutor, never()).submit(impressionsRecorderTask, impressionsSyncHelper)
}

@Test
fun `flush calls flush on tracker`() {
strategy.flush()

verify(tracker).flush()
}

@Test
fun `startPeriodicRecording calls startPeriodicRecording on tracker`() {
strategy.startPeriodicRecording()

verify(tracker).startPeriodicRecording()
}

@Test
fun `stopPeriodicRecording calls stopPeriodicRecording on tracker`() {
strategy.stopPeriodicRecording()

verify(tracker).stopPeriodicRecording()
}

@Test
fun `stopPeriodicRecording calls persist on observer`() {
strategy.stopPeriodicRecording()

verify(impressionsObserver).persist()
}

@Test
fun `enableTracking calls enableTracking on tracker`() {
strategy.enableTracking(true)

verify(tracker).enableTracking(true)
}

@Test
fun `apply calls testAndSet on observer`() {
val impression = createUniqueImpression()
Expand All @@ -140,47 +101,46 @@ class DebugStrategyTest {
spy(impression).withPreviousTime(20421)
}

@Test
fun `call stop periodic tracking when sync listener returns do not retry`() {
val listenerCaptor = ArgumentCaptor.forClass(SplitTaskExecutionListener::class.java)

`when`(impressionsSyncHelper.addListener(listenerCaptor.capture())).thenAnswer { it }
`when`(impressionsSyncHelper.taskExecuted(argThat {
it.taskType == SplitTaskType.IMPRESSIONS_RECORDER
})).thenAnswer {
listenerCaptor.value.taskExecuted(
SplitTaskExecutionInfo.error(
SplitTaskType.IMPRESSIONS_RECORDER,
mapOf(SplitTaskExecutionInfo.DO_NOT_RETRY to true)
)
)
it
}

strategy = DebugStrategy(
impressionsObserver,
impressionsSyncHelper,
taskExecutor,
taskFactory,
telemetryRuntimeProducer,
tracker
)

strategy.startPeriodicRecording()
// simulate sync helper trigger
impressionsSyncHelper.taskExecuted(
SplitTaskExecutionInfo.error(
SplitTaskType.IMPRESSIONS_RECORDER,
mapOf(SplitTaskExecutionInfo.DO_NOT_RETRY to true)
)
)

// start periodic recording again to verify it is not working anymore
strategy.startPeriodicRecording()

verify(tracker, times(1)).startPeriodicRecording()
verify(tracker).stopPeriodicRecording()
}
// @Test
// fun `call stop periodic tracking when sync listener returns do not retry`() {
// val listenerCaptor = ArgumentCaptor.forClass(SplitTaskExecutionListener::class.java)
//
// `when`(impressionsSyncHelper.addListener(listenerCaptor.capture())).thenAnswer { it }
// `when`(impressionsSyncHelper.taskExecuted(argThat {
// it.taskType == SplitTaskType.IMPRESSIONS_RECORDER
// })).thenAnswer {
// listenerCaptor.value.taskExecuted(
// SplitTaskExecutionInfo.error(
// SplitTaskType.IMPRESSIONS_RECORDER,
// mapOf(SplitTaskExecutionInfo.DO_NOT_RETRY to true)
// )
// )
// it
// }
//
// strategy = DebugStrategy(
// impressionsObserver,
// impressionsSyncHelper,
// taskExecutor,
// taskFactory,
// telemetryRuntimeProducer,
// )
//
// strategy.startPeriodicRecording()
// // simulate sync helper trigger
// impressionsSyncHelper.taskExecuted(
// SplitTaskExecutionInfo.error(
// SplitTaskType.IMPRESSIONS_RECORDER,
// mapOf(SplitTaskExecutionInfo.DO_NOT_RETRY to true)
// )
// )
//
// // start periodic recording again to verify it is not working anymore
// strategy.startPeriodicRecording()
//
// verify(tracker, times(1)).startPeriodicRecording()
// verify(tracker).stopPeriodicRecording()
// }

@Test
fun `do not submit recording task when push fails with do not retry`() {
Expand Down Expand Up @@ -221,7 +181,6 @@ class DebugStrategyTest {
taskExecutor,
taskFactory,
telemetryRuntimeProducer,
tracker
)

// call apply two times; first one will trigger the recording task and second one should not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.split.android.client.dtos.KeyImpression
import io.split.android.client.service.executor.SplitTaskExecutor
import io.split.android.client.service.impressions.ImpressionsRecorderTask
import io.split.android.client.service.impressions.ImpressionsTaskFactory
import io.split.android.client.service.impressions.observer.ImpressionsObserver
import io.split.android.client.service.sseclient.sseclient.RetryBackoffCounterTimer
import io.split.android.client.service.synchronizer.RecorderSyncHelper
import org.junit.Before
Expand All @@ -14,6 +15,8 @@ import org.mockito.MockitoAnnotations

class DebugTrackerTest {

@Mock
private lateinit var impressionsObserver: ImpressionsObserver
@Mock
private lateinit var syncHelper: RecorderSyncHelper<KeyImpression>
@Mock
Expand All @@ -29,6 +32,7 @@ class DebugTrackerTest {
fun setUp() {
MockitoAnnotations.openMocks(this)
tracker = DebugTracker(
impressionsObserver,
syncHelper,
taskExecutor,
taskFactory,
Expand Down Expand Up @@ -89,4 +93,11 @@ class DebugTrackerTest {
verify(taskExecutor).stopTask("250")
verify(taskExecutor).schedule(task2, 0L, 30L, syncHelper)
}

@Test
fun `stopPeriodicRecording calls persist on observer`() {
tracker.stopPeriodicRecording()

verify(impressionsObserver).persist()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,6 @@ class NoneStrategyTest {
1
)
}

@Test
fun `flush calls flush on tracker`() {
strategy.flush()

verify(tracker).flush()
}

@Test
fun `startPeriodicRecording calls startPeriodicRecording on tracker`() {
strategy.startPeriodicRecording()

verify(tracker).startPeriodicRecording()
}

@Test
fun `stopPeriodicRecording calls stopPeriodicRecording on tracker`() {
strategy.stopPeriodicRecording()

verify(tracker).stopPeriodicRecording()
}

@Test
fun `enableTracking calls enableTracking on tracker`() {
strategy.enableTracking(true)

verify(tracker).enableTracking(true)
}
}

fun createUniqueImpression(
Expand Down
Loading

0 comments on commit ed204a7

Please sign in to comment.