Skip to content

Commit

Permalink
Add test for disabled trait cache and fix the bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Sloy committed Nov 11, 2019
1 parent 7064705 commit 8b61630
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ public void testShouldTriggerUpdateIfTraitChanges() {
inOrder.verify(appboyUser, times(1)).setEmail(TRAIT_EMAIL_UPDATED);
}

@Test
public void testShouldNotTriggerUpdateIfTraitDiffingDisabled() {
givenIntegrationWithOptions(AppboyIntegrationOptions.builder()
.enableTraitDiffing(false)
.build()
);
Traits traits = createTraits(USER_ID);
traits.putEmail(TRAIT_EMAIL);
callIdentifyWithTraits(traits);
callIdentifyWithTraits(traits);

Traits traitsUpdate = createTraits(USER_ID);
traitsUpdate.putEmail(TRAIT_EMAIL_UPDATED);
callIdentifyWithTraits(traitsUpdate);
callIdentifyWithTraits(traitsUpdate);

InOrder inOrder = Mockito.inOrder(appboyUser);
inOrder.verify(appboyUser, times(1)).setEmail(TRAIT_EMAIL);
inOrder.verify(appboyUser, times(1)).setEmail(TRAIT_EMAIL_UPDATED);
}

@Test
public void testAvoidTriggeringRepeatedUserIdUpdates() {
givenIntegrationWithOptions(AppboyIntegrationOptions.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.appboy.Appboy;
import com.appboy.configuration.AppboyConfig;
Expand Down Expand Up @@ -90,7 +91,9 @@ public String key() {
private final String mToken;
private final Logger mLogger;
private final boolean mAutomaticInAppMessageRegistrationEnabled;
@NonNull
private final UserIdMapper mUserIdMapper;
@Nullable
private final TraitsCache mTraitsCache;

public AppboyIntegration(Context context, Appboy appboy, String token, Logger logger,
Expand Down Expand Up @@ -119,25 +122,17 @@ public void identify(IdentifyPayload identify) {
super.identify(identify);

String userId = identify.userId();
if (!StringUtils.isNullOrBlank(userId)) {
String cachedUserId = mTraitsCache != null ? mTraitsCache.load().userId() : null;
if (!StringUtils.isNullOrBlank(userId) && !userId.equals(cachedUserId)) {
mAppboy.changeUser(mUserIdMapper.transformUserId(userId));

String cachedUserId = mTraitsCache.load().userId();

if (!userId.equals(cachedUserId)) {
mAppboy.changeUser(mUserIdMapper.transformUserId(userId));

if (mTraitsCache != null) {
mTraitsCache.clear();
}
if (mTraitsCache != null) {
mTraitsCache.clear();
}
}

Traits traits = identify.traits();

if (traits == null) {
return;
}

if (mTraitsCache != null) {
Traits lastEmittedTraits = mTraitsCache.load();

Expand Down

0 comments on commit 8b61630

Please sign in to comment.