Skip to content

Commit

Permalink
Merge pull request #817 from Microsoft/feature/pause-resume-target
Browse files Browse the repository at this point in the history
AnalyticsTransmissionTarget pause and resume APIs
  • Loading branch information
guperrot committed Sep 28, 2018
2 parents 6a879ee + 257c046 commit 42dc17f
Show file tree
Hide file tree
Showing 24 changed files with 830 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Analytics extends AbstractAppCenterService {
/**
* Constant marking event of the analytics group.
*/
private static final String ANALYTICS_GROUP = "group_analytics";
static final String ANALYTICS_GROUP = "group_analytics";

/**
* Activity suffix to exclude from generated page names.
Expand Down Expand Up @@ -637,7 +637,7 @@ private synchronized void pauseInstanceAsync() {

@Override
public void run() {
mChannel.pauseGroup(ANALYTICS_GROUP);
mChannel.pauseGroup(ANALYTICS_GROUP, null);
}
});
}
Expand All @@ -650,7 +650,7 @@ private synchronized void resumeInstanceAsync() {

@Override
public void run() {
mChannel.resumeGroup(ANALYTICS_GROUP);
mChannel.resumeGroup(ANALYTICS_GROUP, null);
}
});
}
Expand Down Expand Up @@ -702,6 +702,13 @@ <T> void postCommand(Runnable runnable, DefaultAppCenterFuture<T> future, T valu
postAsyncGetter(runnable, future, valueIfDisabledOrNotStarted);
}

@Override
protected synchronized void post(Runnable runnable) {

/* Override so that AnalyticsTransmissionTarget has access to it. */
super.post(runnable);
}

/**
* Post a command that will run on background even if SDK disabled (needs to be configured though).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.microsoft.appcenter.channel.Channel;
import com.microsoft.appcenter.ingestion.models.Log;
import com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog;
import com.microsoft.appcenter.ingestion.models.one.PartAUtils;
import com.microsoft.appcenter.utils.AppCenterLog;
import com.microsoft.appcenter.utils.async.AppCenterFuture;
import com.microsoft.appcenter.utils.async.DefaultAppCenterFuture;
Expand Down Expand Up @@ -244,6 +245,34 @@ public void run() {
return future;
}

/**
* Pauses log transmission for this target.
* This does not pause child targets.
*/
public void pause() {
Analytics.getInstance().post(new Runnable() {

@Override
public void run() {
mChannel.pauseGroup(Analytics.ANALYTICS_GROUP, mTransmissionTargetToken);
}
});
}

/**
* Resumes log transmission for this target.
* This does not resume child targets.
*/
public void resume() {
Analytics.getInstance().post(new Runnable() {

@Override
public void run() {
mChannel.resumeGroup(Analytics.ANALYTICS_GROUP, mTransmissionTargetToken);
}
});
}

/**
* Getter for transmission target token.
*
Expand Down Expand Up @@ -290,7 +319,7 @@ private synchronized static void addTicketToLog(@NonNull Log log) {

@NonNull
private String getEnabledPreferenceKey() {
return Analytics.getInstance().getEnabledPreferenceKeyPrefix() + mTransmissionTargetToken.split("-")[0];
return Analytics.getInstance().getEnabledPreferenceKeyPrefix() + PartAUtils.getTargetKey(mTransmissionTargetToken);
}

@WorkerThread
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public void notSendingLogsOnPause() {
Analytics.pause();

/* Check if Analytics group is paused. */
verify(channel).pauseGroup(eq(analytics.getGroupName()));
verify(channel).pauseGroup(analytics.getGroupName(), null);

/* Send logs to verify the logs are enqueued after pause. */
Analytics.trackEvent("test");
Expand All @@ -357,7 +357,7 @@ public void notSendingLogsOnPause() {
Analytics.resume();

/* Check if Analytics group is resumed. */
verify(channel).resumeGroup(eq(analytics.getGroupName()));
verify(channel).resumeGroup(analytics.getGroupName(), null);

/* Send logs to verify the logs are enqueued after resume. */
Analytics.trackEvent("test");
Expand All @@ -384,7 +384,7 @@ public void pauseResumeWhileDisabled() {
Analytics.pause();

/* Check if Analytics group is paused even while disabled. */
verify(channel, never()).pauseGroup(eq(analytics.getGroupName()));
verify(channel, never()).pauseGroup(analytics.getGroupName(), null);

/* Send logs to verify the logs are enqueued after pause. */
Analytics.trackEvent("test");
Expand All @@ -395,7 +395,7 @@ public void pauseResumeWhileDisabled() {
Analytics.resume();

/* Check if Analytics group is resumed even while paused. */
verify(channel, never()).resumeGroup(eq(analytics.getGroupName()));
verify(channel, never()).resumeGroup(analytics.getGroupName(), null);

/* Send logs to verify the logs are enqueued after resume. */
Analytics.trackEvent("test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.HashMap;

import static com.microsoft.appcenter.analytics.Analytics.ANALYTICS_GROUP;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -34,6 +35,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
Expand Down Expand Up @@ -395,4 +397,45 @@ public void addTicketToLog() {
/* And that we check expiry. */
verify(authenticationProvider).checkTokenExpiry();
}

@Test
public void pauseResume() {

/* Create a parent and child targets to test calls are not inherited. */
AnalyticsTransmissionTarget parent = Analytics.getTransmissionTarget("parent");
AnalyticsTransmissionTarget child = parent.getTransmissionTarget("child");

/* Call resume while not paused is only checked by channel so call is forwarded. */
child.resume();
verify(mChannel).resumeGroup(ANALYTICS_GROUP, "child");
reset(mChannel);

/* Test pause. */
parent.pause();
verify(mChannel).pauseGroup(ANALYTICS_GROUP, "parent");
verify(mChannel, never()).pauseGroup(ANALYTICS_GROUP, "child");

/* We can call it twice, double calls are checked by channel. */
parent.pause();
verify(mChannel, times(2)).pauseGroup(ANALYTICS_GROUP, "parent");

/* Test resume. */
parent.resume();
verify(mChannel).resumeGroup(ANALYTICS_GROUP, "parent");
verify(mChannel, never()).resumeGroup(ANALYTICS_GROUP, "child");

/* We can call it twice, double calls are checked by channel. */
parent.resume();
verify(mChannel, times(2)).resumeGroup(ANALYTICS_GROUP, "parent");

/* Disable analytics. */
Analytics.setEnabled(false).get();
reset(mChannel);

/* We cannot call channel while disabled. */
parent.pause();
parent.resume();
verify(mChannel, never()).pauseGroup(anyString(), anyString());
verify(mChannel, never()).resumeGroup(anyString(), anyString());
}
}
Loading

0 comments on commit 42dc17f

Please sign in to comment.