Skip to content

Commit e56dca7

Browse files
committed
add sample for blocking async
1 parent eb1cbe6 commit e56dca7

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyUserContextAndroid.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,21 +266,25 @@ public void decideAllAsync(@NonNull OptimizelyDecisionsCallback callback) {
266266
/**
267267
* Returns a decision result ({@link OptimizelyDecision}) for a given flag key and a user context, which contains all data required to deliver the flag.
268268
* <p>
269-
* Note: Despite the "Async" name, this method performs blocking synchronous decision-making.
270-
* For true asynchronous decision-making with callbacks, use the callback-based decideAsync() methods.
269+
* Note: This method blocks the calling thread until the decision is complete.
270+
* It should only be called from a background thread.
271+
* For callback-based asynchronous decision-making, use decideAsync() methods.
271272
* </p>
272273
* @param key A flag key for which a decision will be made.
273274
* @param options A list of options for decision-making.
274275
* @return A decision result.
275276
*/
276277
public OptimizelyDecision decideAsync(@NonNull String key,
277-
@NonNull List<OptimizelyDecideOption> options) {
278+
@NonNull List<OptimizelyDecideOption> options) {
278279
return coreDecide(key, options);
279280
}
280281

281282
/**
282283
* Returns a decision result ({@link OptimizelyDecision}) for a given flag key and a user context, which contains all data required to deliver the flag.
283-
*
284+
* <p>
285+
* Note: This method blocks the calling thread until the decision is complete.
286+
* It should only be called from a background thread.
287+
* </p>
284288
* @param key A flag key for which a decision will be made.
285289
* @return A decision result.
286290
*/
@@ -315,7 +319,10 @@ public Map<String, OptimizelyDecision> decideForKeysAsync(@NonNull List<String>
315319

316320
/**
317321
* Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys.
318-
*
322+
* <p>
323+
* Note: This method blocks the calling thread until decisions are complete.
324+
* It should only be called from a background thread.
325+
* </p>
319326
* @param options A list of options for decision-making.
320327
* @return All decision results mapped by flag keys.
321328
*/
@@ -325,7 +332,10 @@ public Map<String, OptimizelyDecision> decideAllAsync(@NonNull List<OptimizelyDe
325332

326333
/**
327334
* Returns a key-map of decision results ({@link OptimizelyDecision}) for all active flag keys.
328-
*
335+
* <p>
336+
* Note: This method blocks the calling thread until decisions are complete.
337+
* It should only be called from a background thread.
338+
* </p>
329339
* @return A dictionary of all decision results, mapped by flag keys.
330340
*/
331341
public Map<String, OptimizelyDecision> decideAllAsync() {

test-app/src/main/java/com/optimizely/ab/android/test_app/Samples/APISamplesInJava.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.optimizely.ab.android.event_handler.EventRescheduler;
3131
import com.optimizely.ab.android.sdk.OptimizelyClient;
3232
import com.optimizely.ab.android.sdk.OptimizelyManager;
33+
import com.optimizely.ab.android.sdk.OptimizelyUserContextAndroid;
3334
import com.optimizely.ab.android.sdk.cmab.CmabClientHelperAndroid;
3435
import com.optimizely.ab.android.sdk.cmab.DefaultCmabClient;
3536
import com.optimizely.ab.android.test_app.R;
@@ -123,7 +124,7 @@ static public void samplesForCmab(Context context) {
123124
Map<String, Object> attributes = new HashMap<>();
124125
attributes.put("country", "us");
125126
attributes.put("extra-1", 100);
126-
OptimizelyUserContext user = optimizelyClient.createUserContext(userId, attributes);
127+
OptimizelyUserContextAndroid user = optimizelyClient.createUserContext(userId, attributes);
127128

128129
String flagKey = "cmab-flag";
129130

@@ -141,7 +142,7 @@ static public void samplesForCmab(Context context) {
141142
}
142143
Log.d("Samples","=================================================================");
143144

144-
// decideAsync
145+
// decideAsync with callback
145146

146147
Log.d("Samples","=================================================================");
147148
Log.d("Samples","[CMAB] calling async decision for cmab...");
@@ -165,6 +166,31 @@ static public void samplesForCmab(Context context) {
165166
Thread.currentThread().interrupt();
166167
}
167168

169+
// decideAsync with blocking
170+
171+
Log.d("Samples","=================================================================");
172+
Log.d("Samples","[CMAB] calling async blocking decision for cmab...");
173+
Log.d("Samples","=================================================================");
174+
final CountDownLatch latch_2 = new CountDownLatch(1);
175+
Thread thread = new Thread(() -> {
176+
OptimizelyDecision blockDecision = user.decideAsync(flagKey, options);
177+
Log.d("Samples","=================================================================");
178+
Log.d("Samples","[CMAB] async blocking decision for cmab: " + blockDecision.toString());
179+
if (!blockDecision.getEnabled()) {
180+
Log.e("Samples","[ERROR] " + flagKey + " is expected to be enabled for this user!");
181+
}
182+
Log.d("Samples","=================================================================");
183+
latch_2.countDown();
184+
});
185+
thread.start();
186+
187+
try {
188+
latch_2.await(60, TimeUnit.SECONDS);
189+
Log.d("Samples", "[CMAB] Latch released. Async operation completed.");
190+
} catch (InterruptedException e) {
191+
e.printStackTrace();
192+
Thread.currentThread().interrupt();
193+
}
168194
}
169195

170196
static public void samplesForCmabConfig(Context context) {

0 commit comments

Comments
 (0)