Skip to content

Commit

Permalink
Update recorder sync helper to handle multiple listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Dec 10, 2024
1 parent 9f7f97a commit 4c3b592
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -26,7 +28,7 @@ public class RecorderSyncHelperImpl<T extends InBytesSizable> implements Recorde
private final int mMaxQueueSize;
private final long mMaxQueueSizeInBytes;
private final SplitTaskType mTaskType;
private WeakReference<SplitTaskExecutionListener> mTaskExecutionListener;
private final Set<WeakReference<SplitTaskExecutionListener>> mTaskExecutionListener;

public RecorderSyncHelperImpl(SplitTaskType taskType,
StoragePusher<T> storage,
Expand All @@ -40,7 +42,7 @@ public RecorderSyncHelperImpl(SplitTaskType taskType,
mTotalPushedSizeInBytes = new AtomicLong(0);
mMaxQueueSize = maxQueueSize;
mMaxQueueSizeInBytes = maxQueueSizeInBytes;
mTaskExecutionListener = new WeakReference<>(null);
mTaskExecutionListener = new HashSet<>();
}

@Override
Expand All @@ -67,19 +69,28 @@ public void taskExecuted(@NonNull SplitTaskExecutionInfo taskInfo) {
SplitTaskExecutionInfo.NON_SENT_BYTES));
}

if (mTaskExecutionListener.get() != null) {
mTaskExecutionListener.get().taskExecuted(taskInfo);
for (WeakReference<SplitTaskExecutionListener> reference : mTaskExecutionListener) {
SplitTaskExecutionListener listener = reference.get();
if (listener != null) {
listener.taskExecuted(taskInfo);
}
}
}

@Override
public void addListener(SplitTaskExecutionListener listener) {
mTaskExecutionListener = new WeakReference<>(listener);
mTaskExecutionListener.add(new WeakReference<>(listener));
}

@Override
public void removeListener(SplitTaskExecutionListener listener) {
mTaskExecutionListener = new WeakReference<>(null);
for (WeakReference<SplitTaskExecutionListener> reference : mTaskExecutionListener) {
SplitTaskExecutionListener listenerRef = reference.get();
if (listenerRef != null && listenerRef.equals(listener)) {
mTaskExecutionListener.remove(reference);
break;
}
}
}

private void pushAsync(T entity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,40 @@ public void listenerIsNotCalledOnceRemoved() {
mTaskExecutor);

SplitTaskExecutionListener listener = mock(SplitTaskExecutionListener.class);
SplitTaskExecutionListener listener2 = mock(SplitTaskExecutionListener.class);
SplitTaskExecutionInfo executionInfo = SplitTaskExecutionInfo.success(SplitTaskType.IMPRESSIONS_RECORDER);

syncHelper.addListener(listener);
syncHelper.addListener(listener2);
syncHelper.taskExecuted(executionInfo);
syncHelper.removeListener(listener);
syncHelper.taskExecuted(executionInfo);

verify(listener, times(1)).taskExecuted(executionInfo);
verify(listener2, times(2)).taskExecuted(executionInfo);
}

@Test
public void multipleListenersCanBeAdded() {
mStoragePusher = mock(StoragePusher.class);
mTaskExecutor = mock(SplitTaskExecutor.class);

RecorderSyncHelperImpl<KeyImpression> syncHelper = new RecorderSyncHelperImpl<>(
SplitTaskType.IMPRESSIONS_RECORDER,
mStoragePusher,
1,
1,
mTaskExecutor);

SplitTaskExecutionListener listener1 = mock(SplitTaskExecutionListener.class);
SplitTaskExecutionListener listener2 = mock(SplitTaskExecutionListener.class);
SplitTaskExecutionInfo executionInfo = SplitTaskExecutionInfo.success(SplitTaskType.IMPRESSIONS_RECORDER);

syncHelper.addListener(listener1);
syncHelper.addListener(listener2);
syncHelper.taskExecuted(executionInfo);

verify(listener1).taskExecuted(executionInfo);
verify(listener2).taskExecuted(executionInfo);
}
}

0 comments on commit 4c3b592

Please sign in to comment.