Skip to content

Commit

Permalink
Merge pull request #114 from siddique-adobe/MOB-22521
Browse files Browse the repository at this point in the history
[MOB-22521] Updating timeout scale to seconds from milliseconds in the updateProposition configurable timeout API
  • Loading branch information
navratan-soni authored Dec 2, 2024
2 parents 27517bd + 208ec6c commit 4dad4d5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private Optimize() {}
* Experience Edge network.
*
* <p>The returned decision propositions are cached in-memory in the Optimize SDK extension and
* can be retrieved using {@link #getPropositions(List, long, AdobeCallback)} API.
* can be retrieved using {@link #getPropositions(List, AdobeCallback)} API.
*
* @param decisionScopes {@code List<DecisionScope>} containing scopes for which offers need to
* be updated.
Expand All @@ -58,12 +58,13 @@ private Optimize() {}
* @param data {@code Map<String, Object>} containing additional free-form data to be sent in
* the personalization query request.
*/
@Deprecated
public static void updatePropositions(
@NonNull final List<DecisionScope> decisionScopes,
@Nullable final Map<String, Object> xdm,
@Nullable final Map<String, Object> data) {
final long defaultTimeout = OptimizeConstants.EDGE_CONTENT_COMPLETE_RESPONSE_TIMEOUT;
updatePropositions(decisionScopes, xdm, data, defaultTimeout, null);

updatePropositions(decisionScopes, xdm, data, null);
}

/**
Expand All @@ -88,8 +89,9 @@ public static void updatePropositions(
@Nullable final Map<String, Object> xdm,
@Nullable final Map<String, Object> data,
@Nullable final AdobeCallback<Map<DecisionScope, OptimizeProposition>> callback) {
final long defaultTimeout = OptimizeConstants.EDGE_CONTENT_COMPLETE_RESPONSE_TIMEOUT;
updatePropositionsInternal(decisionScopes, xdm, data, defaultTimeout, callback);
final double defaultTimeoutSeconds =
OptimizeConstants.EDGE_CONTENT_COMPLETE_RESPONSE_TIMEOUT;
updatePropositionsInternal(decisionScopes, xdm, data, defaultTimeoutSeconds, callback);
}

/**
Expand All @@ -98,33 +100,33 @@ public static void updatePropositions(
* Experience Edge network.
*
* <p>The returned decision propositions are cached in-memory in the Optimize SDK extension and
* can be retrieved using {@link #getPropositions(List, long, AdobeCallback)} API.
* can be retrieved using {@link #getPropositions(List, double, AdobeCallback)} API.
*
* @param decisionScopes {@code List<DecisionScope>} containing scopes for which offers need to
* be updated.
* @param xdm {@code Map<String, Object>} containing additional XDM-formatted data to be sent in
* the personalization query request.
* @param data {@code Map<String, Object>} containing additional free-form data to be sent in
* the personalization query request.
* @param timeoutMillis {@code Long} containing additional configurable timeout to be sent in
* the personalization query request.
* @param timeoutSeconds {@code Double} containing additional configurable timeout(seconds) to
* be sent in the personalization query request.
* @param callback {@code AdobeCallback<Map<DecisionScope, OptimizeProposition>>} which will be
* invoked when decision propositions are received from the Edge network.
*/
public static void updatePropositions(
@NonNull final List<DecisionScope> decisionScopes,
@Nullable final Map<String, Object> xdm,
@Nullable final Map<String, Object> data,
final long timeoutMillis,
final double timeoutSeconds,
@Nullable final AdobeCallback<Map<DecisionScope, OptimizeProposition>> callback) {
updatePropositionsInternal(decisionScopes, xdm, data, timeoutMillis, callback);
updatePropositionsInternal(decisionScopes, xdm, data, timeoutSeconds, callback);
}

private static void updatePropositionsInternal(
@NonNull final List<DecisionScope> decisionScopes,
@Nullable final Map<String, Object> xdm,
@Nullable final Map<String, Object> data,
final long timeoutMillis,
final double timeoutSeconds,
@Nullable final AdobeCallback<Map<DecisionScope, OptimizeProposition>> callback) {

if (OptimizeUtils.isNullOrEmpty(decisionScopes)) {
Expand Down Expand Up @@ -175,6 +177,9 @@ private static void updatePropositionsInternal(
if (!OptimizeUtils.isNullOrEmpty(data)) {
eventData.put(OptimizeConstants.EventDataKeys.DATA, data);
}

long timeoutMillis = (long) (timeoutSeconds * OptimizeConstants.TIMEOUT_CONVERSION_FACTOR);

eventData.put(OptimizeConstants.EventDataKeys.TIMEOUT, timeoutMillis);

final Event event =
Expand Down Expand Up @@ -275,8 +280,8 @@ public void call(final Event event) {
public static void getPropositions(
@NonNull final List<DecisionScope> decisionScopes,
@NonNull final AdobeCallback<Map<DecisionScope, OptimizeProposition>> callback) {
long defaultTimeout = OptimizeConstants.GET_RESPONSE_CALLBACK_TIMEOUT;
getPropositionsInternal(decisionScopes, defaultTimeout, callback);
final double defaultTimeoutSeconds = OptimizeConstants.GET_RESPONSE_CALLBACK_TIMEOUT;
getPropositionsInternal(decisionScopes, defaultTimeoutSeconds, callback);
}

/**
Expand All @@ -290,14 +295,14 @@ public static void getPropositions(
*/
public static void getPropositions(
@NonNull final List<DecisionScope> decisionScopes,
final long timeoutMillis,
final double timeoutSeconds,
@NonNull final AdobeCallback<Map<DecisionScope, OptimizeProposition>> callback) {
getPropositionsInternal(decisionScopes, timeoutMillis, callback);
getPropositionsInternal(decisionScopes, timeoutSeconds, callback);
}

private static void getPropositionsInternal(
@NonNull final List<DecisionScope> decisionScopes,
final long timeoutMillis,
final double timeoutSeconds,
@NonNull final AdobeCallback<Map<DecisionScope, OptimizeProposition>> callback) {
if (OptimizeUtils.isNullOrEmpty(decisionScopes)) {
Log.warning(
Expand Down Expand Up @@ -345,8 +350,8 @@ private static void getPropositionsInternal(
.setEventData(eventData)
.build();

// Increased default response callback timeout to 10s to ensure prior update propositions
// requests have enough time to complete.
long timeoutMillis = (long) (timeoutSeconds * OptimizeConstants.TIMEOUT_CONVERSION_FACTOR);

MobileCore.dispatchEventWithResponseCallback(
event,
timeoutMillis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class OptimizeConstants {
static final String EXTENSION_VERSION = "3.2.0";
static final String EXTENSION_NAME = "com.adobe.optimize";
static final String FRIENDLY_NAME = "Optimize";
static final long DEFAULT_RESPONSE_CALLBACK_TIMEOUT = 500L;
static final long GET_RESPONSE_CALLBACK_TIMEOUT = 10000L;
static final long EDGE_CONTENT_COMPLETE_RESPONSE_TIMEOUT = 10000L;
static final double GET_RESPONSE_CALLBACK_TIMEOUT = 10;
static final double EDGE_CONTENT_COMPLETE_RESPONSE_TIMEOUT = 10;
static final long TIMEOUT_CONVERSION_FACTOR = 1000;

static final String ACTIVITY_ID = "activityId";
static final String XDM_ACTIVITY_ID = "xdm:activityId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

package com.adobe.marketing.mobile.optimize;

import static com.adobe.marketing.mobile.optimize.Optimize.failWithOptimizeError;
import static com.adobe.marketing.mobile.optimize.Optimize.getPropositions;
import static com.adobe.marketing.mobile.optimize.Optimize.updatePropositions;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand Down Expand Up @@ -460,7 +457,7 @@ public void testGetPropositions_validDecisionScope() throws Exception {
new DecisionScope(
"eyJhY3Rpdml0eUlkIjoieGNvcmU6b2ZmZXItYWN0aXZpdHk6MTExMTExMTExMTExMTExMSIsInBsYWNlbWVudElkIjoieGNvcmU6b2ZmZXItcGxhY2VtZW50OjExMTExMTExMTExMTExMTEifQ=="));

getPropositions(
Optimize.getPropositions(
scopes,
new AdobeCallbackWithError<Map<DecisionScope, OptimizeProposition>>() {
@Override
Expand Down Expand Up @@ -567,7 +564,7 @@ public void testGetPropositions_multipleValidDecisionScopes() {
"eyJhY3Rpdml0eUlkIjoieGNvcmU6b2ZmZXItYWN0aXZpdHk6MTExMTExMTExMTExMTExMSIsInBsYWNlbWVudElkIjoieGNvcmU6b2ZmZXItcGxhY2VtZW50OjExMTExMTExMTExMTExMTEifQ=="));
scopes.add(new DecisionScope("myMbox"));

getPropositions(
Optimize.getPropositions(
scopes,
new AdobeCallbackWithError<Map<DecisionScope, OptimizeProposition>>() {
@Override
Expand Down Expand Up @@ -637,7 +634,7 @@ public void testGetPropositions_invalidDecisionScopeInList() {
new DecisionScope(
"eyJhY3Rpdml0eUlkIjoiIiwicGxhY2VtZW50SWQiOiJ4Y29yZTpvZmZlci1wbGFjZW1lbnQ6MTExMTExMTExMTExMTExMSJ9"));

getPropositions(
Optimize.getPropositions(
scopes,
new AdobeCallbackWithError<Map<DecisionScope, OptimizeProposition>>() {
@Override
Expand Down Expand Up @@ -666,7 +663,7 @@ public void call(Map<DecisionScope, OptimizeProposition> propositionsMap) {
public void testGetPropositions_emptyDecisionScopesList() {
try (MockedStatic<Log> logMockedStatic = Mockito.mockStatic(Log.class)) {
// test
getPropositions(
Optimize.getPropositions(
new ArrayList<DecisionScope>(),
new AdobeCallbackWithError<Map<DecisionScope, OptimizeProposition>>() {
@Override
Expand Down Expand Up @@ -695,7 +692,7 @@ public void call(Map<DecisionScope, OptimizeProposition> propositionsMap) {
public void testGetPropositions_nullDecisionScopesList() {
try (MockedStatic<Log> logMockedStatic = Mockito.mockStatic(Log.class)) {
// test
getPropositions(
Optimize.getPropositions(
null,
new AdobeCallbackWithError<Map<DecisionScope, OptimizeProposition>>() {
@Override
Expand Down Expand Up @@ -906,7 +903,7 @@ public void test_clearCachedPropositions() {
@Test
public void testUpdatePropositions_timeoutError() {

long timeoutMillis = 100;
double timeoutSeconds = 0.1;
Map<String, Object> xdm = new HashMap<>();
Map<String, Object> data = new HashMap<>();
final List<DecisionScope> scopes = new ArrayList<>();
Expand Down Expand Up @@ -947,13 +944,13 @@ public void testUpdatePropositions_timeoutError() {
.thenAnswer(
(Answer<Void>)
invocation -> {
failWithOptimizeError(
Optimize.failWithOptimizeError(
callbackMockEvent,
AEPOptimizeError.Companion.getTimeoutError());
return null;
});

updatePropositions(scopes, xdm, data, timeoutMillis, callbackMock);
Optimize.updatePropositions(scopes, xdm, data, timeoutSeconds, callbackMock);
ArgumentCaptor<AEPOptimizeError> errorCaptor =
ArgumentCaptor.forClass(AEPOptimizeError.class);
verify(callbackMockEvent, times(1)).fail(errorCaptor.capture());
Expand All @@ -965,7 +962,7 @@ public void testUpdatePropositions_timeoutError() {
@Test
public void testGetPropositions_timeoutError() {

long timeoutMillis = 100;
double timeoutSeconds = 0.1;
final List<DecisionScope> scopes = new ArrayList<>();
scopes.add(
new DecisionScope(
Expand Down Expand Up @@ -1004,13 +1001,13 @@ public void testGetPropositions_timeoutError() {
.thenAnswer(
(Answer<Void>)
invocation -> {
failWithOptimizeError(
Optimize.failWithOptimizeError(
callbackMockEvent,
AEPOptimizeError.Companion.getTimeoutError());
return null;
});

getPropositions(scopes, timeoutMillis, callbackMock);
Optimize.getPropositions(scopes, timeoutSeconds, callbackMock);
ArgumentCaptor<AEPOptimizeError> errorCaptor =
ArgumentCaptor.forClass(AEPOptimizeError.class);
verify(callbackMockEvent, times(1)).fail(errorCaptor.capture());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fun OffersView(viewModel: MainViewModel) {
decisionScopes = decisionScopeList,
xdm = mapOf(Pair("xdmKey", "1234")),
data = data,
timeout = 200
timeoutSeconds = 0.2
)
}) {
Text(
Expand All @@ -203,7 +203,7 @@ fun OffersView(viewModel: MainViewModel) {

viewModel.getPropositions(
decisionScopes = decisionScopeList,
timeout = 200
timeoutSeconds = 0.2
)
}) {
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class MainViewModel: ViewModel() {
* Calls the Optimize SDK API to get the Propositions see [Optimize.getPropositions]
*
* @param [decisionScopes] a [List] of [DecisionScope]
* @param [timeout] a [Long] in milliseconds
* @param [timeoutSeconds] a [Double] in seconds
*/
fun getPropositions(decisionScopes: List<DecisionScope>, timeout: Long? = null) {
fun getPropositions(decisionScopes: List<DecisionScope>, timeoutSeconds: Double? = null) {
optimizePropositionStateMap.clear()
val callback = object : AdobeCallbackWithError<Map<DecisionScope, OptimizeProposition>> {
override fun call(propositions: Map<DecisionScope, OptimizeProposition>?) {
Expand All @@ -86,8 +86,8 @@ class MainViewModel: ViewModel() {
print("Error in getting Propositions.")
}
}
timeout?.let { milliseconds ->
Optimize.getPropositions(decisionScopes, milliseconds, callback)
timeoutSeconds?.let { seconds ->
Optimize.getPropositions(decisionScopes, seconds, callback)
} ?: Optimize.getPropositions(decisionScopes, callback)
}

Expand All @@ -97,13 +97,13 @@ class MainViewModel: ViewModel() {
* @param decisionScopes a [List] of [DecisionScope]
* @param xdm a [Map] of xdm params
* @param data a [Map] of data
* @param timeout a [Long] in milliseconds
* @param timeoutSeconds a [Double] in seconds
*/
fun updatePropositions(
decisionScopes: List<DecisionScope>,
xdm: Map<String, String>,
data: Map<String, Any>,
timeout: Long? = null
timeoutSeconds: Double? = null
) {
val callback = object : AdobeCallbackWithOptimizeError<Map<DecisionScope, OptimizeProposition>> {
override fun call(propositions: Map<DecisionScope, OptimizeProposition>?) {
Expand All @@ -119,12 +119,12 @@ class MainViewModel: ViewModel() {

}
optimizePropositionStateMap.clear()
timeout?.let { milliseconds ->
timeoutSeconds?.let { seconds ->
Optimize.updatePropositions(
decisionScopes,
xdm,
data,
milliseconds,
seconds,
callback
)
} ?: Optimize.updatePropositions(decisionScopes, xdm, data, callback)
Expand Down

0 comments on commit 4dad4d5

Please sign in to comment.