Skip to content

Commit

Permalink
Wip test changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 10, 2024
1 parent 552b9ec commit 9f7f97a
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

import androidx.annotation.NonNull;

import java.util.concurrent.atomic.AtomicBoolean;

import io.split.android.client.dtos.KeyImpression;
import io.split.android.client.service.ServiceConstants;
import io.split.android.client.service.executor.SplitTaskExecutionInfo;
import io.split.android.client.service.executor.SplitTaskExecutionListener;
import io.split.android.client.service.executor.SplitTaskExecutionStatus;
import io.split.android.client.service.executor.SplitTaskExecutor;
import io.split.android.client.service.impressions.ImpressionsTaskFactory;
import io.split.android.client.service.impressions.observer.ImpressionsObserver;
Expand All @@ -19,7 +24,21 @@ class DebugTracker implements PeriodicTracker {
private final ImpressionsTaskFactory mImpressionsTaskFactory;
private final RetryBackoffCounterTimer mRetryTimer;
private final int mImpressionsRefreshRate;
private final AtomicBoolean mIsSynchronizing = new AtomicBoolean(true);
private final ImpressionsObserver mImpressionsObserver;
/** @noinspection FieldCanBeLocal*/
private final SplitTaskExecutionListener mTaskExecutionListener = new SplitTaskExecutionListener() {
@Override
public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) {
// this listener intercepts impressions recording task
if (taskInfo.getStatus() == SplitTaskExecutionStatus.ERROR) {
if (Boolean.TRUE.equals(taskInfo.getBoolValue(SplitTaskExecutionInfo.DO_NOT_RETRY))) {
mIsSynchronizing.compareAndSet(true, false);
stopPeriodicRecording();
}
}
}
};
private String mImpressionsRecorderTaskId;

DebugTracker(@NonNull ImpressionsObserver impressionsObserver,
Expand All @@ -30,6 +49,7 @@ class DebugTracker implements PeriodicTracker {
int impressionsRefreshRate) {
mImpressionsObserver = checkNotNull(impressionsObserver);
mImpressionsSyncHelper = checkNotNull(impressionsSyncHelper);
mImpressionsSyncHelper.addListener(mTaskExecutionListener);
mTaskExecutor = checkNotNull(taskExecutor);
mImpressionsTaskFactory = checkNotNull(taskFactory);
mRetryTimer = retryTimer;
Expand All @@ -50,7 +70,9 @@ private void flushImpressions() {

@Override
public void startPeriodicRecording() {
scheduleImpressionsRecorderTask();
if (mIsSynchronizing.get()) {
scheduleImpressionsRecorderTask();
}
}

private void scheduleImpressionsRecorderTask() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
import java.util.concurrent.atomic.AtomicBoolean;

import io.split.android.client.service.ServiceConstants;
import io.split.android.client.service.executor.SplitTask;
import io.split.android.client.service.executor.SplitTaskExecutionInfo;
import io.split.android.client.service.executor.SplitTaskExecutionListener;
import io.split.android.client.service.executor.SplitTaskExecutionStatus;
import io.split.android.client.service.executor.SplitTaskExecutor;
import io.split.android.client.service.executor.SplitTaskSerialWrapper;
import io.split.android.client.service.executor.SplitTaskType;
import io.split.android.client.service.impressions.ImpressionsCounter;
import io.split.android.client.service.impressions.ImpressionsTaskFactory;
import io.split.android.client.service.impressions.unique.UniqueKeysTracker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import io.split.android.client.dtos.KeyImpression;
import io.split.android.client.service.ServiceConstants;
import io.split.android.client.service.executor.SplitTaskExecutionInfo;
import io.split.android.client.service.executor.SplitTaskExecutionListener;
import io.split.android.client.service.executor.SplitTaskExecutionStatus;
import io.split.android.client.service.executor.SplitTaskExecutor;
import io.split.android.client.service.impressions.ImpressionsTaskFactory;
import io.split.android.client.service.impressions.observer.ImpressionsObserver;
Expand All @@ -25,6 +28,22 @@ class OptimizedTracker implements PeriodicTracker {
private final int mImpressionsRefreshRate;
private String mImpressionsRecorderTaskId;
private final AtomicBoolean mTrackingIsEnabled;
private final AtomicBoolean mIsSynchronizing = new AtomicBoolean(true);
/**
* @noinspection FieldCanBeLocal
*/
private final SplitTaskExecutionListener mTaskExecutionListener = new SplitTaskExecutionListener() {
@Override
public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) {
// this listener intercepts impressions recording task
if (taskInfo.getStatus() == SplitTaskExecutionStatus.ERROR) {
if (Boolean.TRUE.equals(taskInfo.getBoolValue(SplitTaskExecutionInfo.DO_NOT_RETRY))) {
mIsSynchronizing.compareAndSet(true, false);
stopPeriodicRecording();
}
}
}
};

OptimizedTracker(@NonNull ImpressionsObserver impressionsObserver,
@NonNull RecorderSyncHelper<KeyImpression> impressionsSyncHelper,
Expand All @@ -36,6 +55,7 @@ class OptimizedTracker implements PeriodicTracker {
boolean isTrackingEnabled) {
mImpressionsObserver = checkNotNull(impressionsObserver);
mImpressionsSyncHelper = checkNotNull(impressionsSyncHelper);
mImpressionsSyncHelper.addListener(mTaskExecutionListener);
mTaskExecutor = checkNotNull(taskExecutor);
mImpressionsTaskFactory = checkNotNull(taskFactory);

Expand All @@ -51,7 +71,9 @@ public void flush() {

@Override
public void startPeriodicRecording() {
scheduleImpressionsRecorderTask();
if (mIsSynchronizing.get()) {
scheduleImpressionsRecorderTask();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.split.android.client.service.impressions.strategy

import io.split.android.client.dtos.KeyImpression
import io.split.android.client.impressions.Impression
import io.split.android.client.service.executor.SplitTaskExecutionInfo
import io.split.android.client.service.executor.SplitTaskExecutionListener
import io.split.android.client.service.executor.SplitTaskExecutor
Expand All @@ -17,7 +16,14 @@ import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.any
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.Mockito.argThat
import org.mockito.Mockito.eq
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.spy
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.mockito.invocation.InvocationOnMock

Expand Down Expand Up @@ -101,47 +107,6 @@ 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,
// )
//
// 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`() {
val listenerCaptor = ArgumentCaptor.forClass(SplitTaskExecutionListener::class.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package io.split.android.client.service.impressions.strategy

import io.split.android.client.dtos.KeyImpression
import io.split.android.client.service.executor.SplitTaskExecutionInfo
import io.split.android.client.service.executor.SplitTaskExecutionListener
import io.split.android.client.service.executor.SplitTaskExecutor
import io.split.android.client.service.executor.SplitTaskType
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
import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.Mock
import org.mockito.Mockito.*
import org.mockito.MockitoAnnotations
Expand Down Expand Up @@ -100,4 +104,50 @@ class DebugTrackerTest {

verify(impressionsObserver).persist()
}

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

val impressionsRecorderTask = mock(ImpressionsRecorderTask::class.java)
`when`(taskFactory.createImpressionsRecorderTask()).thenReturn(impressionsRecorderTask)
`when`(taskExecutor.schedule(eq(impressionsRecorderTask), eq(0L), eq(30L), any())).thenReturn("250")
`when`(syncHelper.addListener(listenerCaptor.capture())).thenAnswer { it }
`when`(syncHelper.taskExecuted(argThat {
it.taskType == SplitTaskType.IMPRESSIONS_RECORDER
})).thenAnswer {
listenerCaptor.value.taskExecuted(
SplitTaskExecutionInfo.error(
SplitTaskType.IMPRESSIONS_RECORDER,
mapOf(SplitTaskExecutionInfo.DO_NOT_RETRY to true)
)
)
it
}

tracker = DebugTracker(
impressionsObserver,
syncHelper,
taskExecutor,
taskFactory,
retryBackoffCounterTimer,
30
)

tracker.startPeriodicRecording()
val spy = spy(tracker)
// simulate sync helper trigger
syncHelper.taskExecuted(
SplitTaskExecutionInfo.error(
SplitTaskType.IMPRESSIONS_RECORDER,
mapOf(SplitTaskExecutionInfo.DO_NOT_RETRY to true)
)
)

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

verify(taskExecutor).stopTask("250")
verify(impressionsObserver).persist()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.split.android.client.service.impressions.ImpressionsCounter
import io.split.android.client.service.impressions.ImpressionsTaskFactory
import io.split.android.client.service.impressions.unique.SaveUniqueImpressionsTask
import io.split.android.client.service.impressions.unique.UniqueKeysTracker
import io.split.android.client.service.sseclient.sseclient.RetryBackoffCounterTimer
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.any
Expand All @@ -31,15 +30,6 @@ class NoneStrategyTest {
@Mock
private lateinit var uniqueKeysTracker: UniqueKeysTracker

@Mock
private lateinit var countRetryTimer: RetryBackoffCounterTimer

@Mock
private lateinit var uniqueKeysRetryTimer: RetryBackoffCounterTimer

@Mock
private lateinit var tracker: PeriodicTracker

private lateinit var strategy: NoneStrategy

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ class NoneTrackerTest {
verify(taskExecutor, never()).submit(eq(countTask), any())
}


@Test
fun `stop periodic recording does not save unique keys when tracking is disabled`() {
val countTask = mock(SaveUniqueImpressionsTask::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,49 +150,6 @@ class OptimizedStrategyTest {
)
}

// @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(DO_NOT_RETRY to true)
// )
// )
// it
// }
//
// strategy = OptimizedStrategy(
// impressionsObserver,
// impressionsCounter,
// impressionsSyncHelper,
// taskExecutor,
// taskFactory,
// telemetryRuntimeProducer,
// dedupeTimeInterval
// )
//
// strategy.startPeriodicRecording()
// // simulate sync helper trigger
// impressionsSyncHelper.taskExecuted(
// SplitTaskExecutionInfo.error(
// SplitTaskType.IMPRESSIONS_RECORDER,
// mapOf(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`() {
val listenerCaptor = ArgumentCaptor.forClass(SplitTaskExecutionListener::class.java)
Expand Down
Loading

0 comments on commit 9f7f97a

Please sign in to comment.