Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set pending shared state before updating Identity Map #81

Merged
merged 2 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.adobe.marketing.mobile.Extension;
import com.adobe.marketing.mobile.ExtensionApi;
import com.adobe.marketing.mobile.SharedStateResolution;
import com.adobe.marketing.mobile.SharedStateResolver;
import com.adobe.marketing.mobile.SharedStateResult;
import com.adobe.marketing.mobile.SharedStateStatus;
import com.adobe.marketing.mobile.services.Log;
Expand Down Expand Up @@ -250,10 +251,14 @@ private void handleUrlVariableResponse(
* @param event the edge update identity {@link Event}
*/
void handleUpdateIdentities(@NonNull final Event event) {
// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);

final Map<String, Object> eventData = event.getEventData();

if (eventData == null) {
Log.trace(LOG_TAG, LOG_SOURCE, "Cannot update identifiers, event data is null.");
resolver.resolve(state.getIdentityProperties().toXDMData(false));
return;
}

Expand All @@ -265,11 +270,12 @@ void handleUpdateIdentities(@NonNull final Event event) {
LOG_SOURCE,
"Failed to update identifiers as no identifiers were found in the event data."
);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to your change, but looks like toXDMData(allowEmpty) is called with false in all cases, can we remove that param? 😁

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The iOS code sets the value to true when handling identity and get identifiers requests, so I'll leave this as is for now and update if needed when I work on syncing the iOS and Android implementations.

return;
}

state.updateCustomerIdentifiers(map);
shareIdentityXDMSharedState(event);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
}

/**
Expand All @@ -278,10 +284,14 @@ void handleUpdateIdentities(@NonNull final Event event) {
* @param event the edge remove identity request {@link Event}
*/
void handleRemoveIdentity(@NonNull final Event event) {
// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);

final Map<String, Object> eventData = event.getEventData();

if (eventData == null) {
Log.trace(LOG_TAG, LOG_SOURCE, "Cannot remove identifiers, event data is null.");
resolver.resolve(state.getIdentityProperties().toXDMData(false));
return;
}

Expand All @@ -293,11 +303,12 @@ void handleRemoveIdentity(@NonNull final Event event) {
LOG_SOURCE,
"Failed to remove identifiers as no identifiers were found in the event data."
);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
return;
}

state.removeCustomerIdentifiers(map);
shareIdentityXDMSharedState(event);
resolver.resolve(state.getIdentityProperties().toXDMData(false));
emdobrin marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -325,8 +336,10 @@ private void handleGetIdentifiersRequest(@NonNull final Event event) {
* @param event the identity request reset {@link Event}
*/
void handleRequestReset(@NonNull final Event event) {
// Add pending shared state to avoid race condition between updating and reading identity map
final SharedStateResolver resolver = getApi().createPendingXDMSharedState(event);
state.resetIdentifiers();
shareIdentityXDMSharedState(event);
resolver.resolve(state.getIdentityProperties().toXDMData(false));

// dispatch reset complete event
final Event responseEvent = new Event.Builder(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.adobe.marketing.mobile.EventType;
import com.adobe.marketing.mobile.ExtensionApi;
import com.adobe.marketing.mobile.SharedStateResolution;
import com.adobe.marketing.mobile.SharedStateResolver;
import com.adobe.marketing.mobile.SharedStateResult;
import com.adobe.marketing.mobile.SharedStateStatus;
import java.util.Collections;
Expand All @@ -51,6 +52,9 @@ public class IdentityExtensionTests {
@Mock
ExtensionApi mockExtensionApi;

@Mock
SharedStateResolver mockSharedStateResolver;

@Mock
IdentityState mockIdentityState;

Expand Down Expand Up @@ -660,6 +664,7 @@ public void test_handleUpdateIdentities_whenValidData_updatesCustomerIdentifiers
})
.when(mockIdentityState)
.updateCustomerIdentifiers(any());
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);

extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

Expand All @@ -682,7 +687,10 @@ public void test_handleUpdateIdentities_whenValidData_updatesCustomerIdentifiers
verify(mockIdentityState).updateCustomerIdentifiers(identityMapCaptor.capture());
assertEquals(identityXDM, identityMapCaptor.getValue().asXDMMap());

verify(mockExtensionApi).createXDMSharedState(properties.toXDMData(false), updateIdentityEvent);
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(updateIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));

verify(mockExtensionApi, never()).dispatch(any());
}

Expand All @@ -691,6 +699,7 @@ public void test_handleUpdateIdentities_nullEventData_returns() {
// setup
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);

extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

Expand All @@ -705,17 +714,20 @@ public void test_handleUpdateIdentities_nullEventData_returns() {

// verify that identifiers are not updated
verify(mockIdentityState, never()).updateCustomerIdentifiers(any());
// verify that no shared state is created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify that no event is dispatched
verify(mockExtensionApi, never()).dispatch(any());

// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(updateIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

@Test
public void test_handleUpdateIdentities_EmptyEventData_returns() {
// setup
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);
extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

// test
Expand All @@ -730,10 +742,12 @@ public void test_handleUpdateIdentities_EmptyEventData_returns() {

// verify that identifiers are not updated
verify(mockIdentityState, never()).updateCustomerIdentifiers(any());
// verify that no shared state is created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify that no event is dispatched
verify(mockExtensionApi, never()).dispatch(any());

// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(updateIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

// ========================================================================================
Expand Down Expand Up @@ -761,6 +775,8 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
.when(mockIdentityState)
.removeCustomerIdentifiers(any());

when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);

extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

// test
Expand All @@ -776,11 +792,9 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
removedIdentityMapCaptor.getValue().toString()
);

// verify shared state
final Map<String, Object> expectedState = properties.toXDMData(false);
final ArgumentCaptor<Map<String, Object>> stateCaptor = ArgumentCaptor.forClass(Map.class);
verify(mockExtensionApi).createXDMSharedState(stateCaptor.capture(), eq(removeIdentityEvent));
assertEquals(expectedState, stateCaptor.getValue());
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(removeIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

@Test
Expand All @@ -792,6 +806,7 @@ public void test_handleRemoveIdentity_whenNullData_returns() {
);
final IdentityProperties properties = new IdentityProperties(identityXDM);
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);
extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

// test
Expand All @@ -801,13 +816,17 @@ public void test_handleRemoveIdentity_whenNullData_returns() {
// verify identifiers not removed
verify(mockIdentityState, never()).removeCustomerIdentifiers(any());

// verify shared state is never created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(removeIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

@Test
public void test_handleRemoveIdentity_eventWithEmptyData_returns() {
// setup
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);
extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

// test
Expand All @@ -817,8 +836,9 @@ public void test_handleRemoveIdentity_eventWithEmptyData_returns() {
// verify identifiers not removed
verify(mockIdentityState, never()).removeCustomerIdentifiers(any());

// verify shared state is never created
verify(mockExtensionApi, never()).createXDMSharedState(any(), any());
// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(notARemoveIdentityEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}

// ========================================================================================
Expand Down Expand Up @@ -883,6 +903,7 @@ public void test_handleRequestContent_EventIsNotAdIdEvent() {
public void test_handleRequestReset() {
final IdentityProperties properties = new IdentityProperties();
when(mockIdentityState.getIdentityProperties()).thenReturn(properties);
when(mockExtensionApi.createPendingXDMSharedState(any())).thenReturn(mockSharedStateResolver);

extension = new IdentityExtension(mockExtensionApi, mockIdentityState);

Expand All @@ -892,6 +913,9 @@ public void test_handleRequestReset() {
extension.handleRequestReset(resetEvent);

verify(mockIdentityState).resetIdentifiers();
verify(mockExtensionApi).createXDMSharedState(properties.toXDMData(false), resetEvent); // will fail because of new ecid

// verify pending state is created and resolved
verify(mockExtensionApi).createPendingXDMSharedState(eq(resetEvent));
verify(mockSharedStateResolver).resolve(eq(properties.toXDMData(false)));
}
}