Skip to content

Commit 90fdcf9

Browse files
[FSSDK-11177] review update 1
1 parent babc4b8 commit 90fdcf9

File tree

6 files changed

+54
-63
lines changed

6 files changed

+54
-63
lines changed

OptimizelySDK.Tests/CmabTests/DecisionServiceCmabTest.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ public void TestGetVariationWithCmabExperimentZeroTrafficAllocation()
138138
var result = _decisionService.GetVariation(experiment, userContext, mockConfig.Object);
139139

140140
Assert.IsNotNull(result);
141-
Assert.IsNull(result.ResultObject, "No variation should be returned with 0 traffic");
142-
Assert.IsNull(result.ResultObject?.CmabUuid);
141+
Assert.IsNull(result.ResultObject.Variation, "No variation should be returned with 0 traffic");
142+
Assert.IsNull(result.ResultObject.CmabUuid);
143143

144144
var reasons = result.DecisionReasons.ToReport(true);
145145
var expectedMessage =
@@ -183,8 +183,8 @@ public void TestGetVariationWithCmabExperimentServiceError()
183183
var result = _decisionService.GetVariation(experiment, userContext, mockConfig.Object);
184184

185185
Assert.IsNotNull(result);
186-
Assert.IsNull(result.ResultObject, "Should return null on error");
187-
// CmabUuid is now in VariationDecisionResult, not DecisionReasons
186+
Assert.IsNull(result.ResultObject.Variation, "Should return null on error");
187+
Assert.IsTrue(result.ResultObject.CmabError);
188188

189189
var reasonsList = result.DecisionReasons.ToReport(true);
190190
Assert.IsTrue(reasonsList.Exists(reason =>
@@ -232,8 +232,7 @@ public void TestGetVariationWithCmabExperimentUnknownVariationId()
232232
var result = _decisionService.GetVariation(experiment, userContext, mockConfig.Object);
233233

234234
Assert.IsNotNull(result);
235-
Assert.IsNull(result.ResultObject);
236-
// CmabUuid is now in VariationDecisionResult, not DecisionReasons
235+
Assert.IsNull(result.ResultObject.Variation, "Should return null on error");
237236

238237
var reasons = result.DecisionReasons.ToReport(true);
239238
var expectedMessage =

OptimizelySDK.Tests/OptimizelyFactoryTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public void SetCmabCacheConfigStoresCacheSizeAndTtl()
271271
Assert.IsNotNull(config);
272272
Assert.AreEqual(cacheSize, config.CacheSize);
273273
Assert.AreEqual(cacheTtl, config.CacheTtl);
274-
Assert.IsNull(config.CustomCache);
274+
Assert.IsNull(config.Cache);
275275
}
276276

277277
[Test]
@@ -284,7 +284,7 @@ public void SetCmabCustomCacheStoresCustomCacheInstance()
284284
var config = GetCurrentCmabConfiguration();
285285

286286
Assert.IsNotNull(config);
287-
Assert.AreSame(customCache, config.CustomCache);
287+
Assert.AreSame(customCache, config.Cache);
288288
Assert.IsNull(config.CacheSize);
289289
Assert.IsNull(config.CacheTtl);
290290
}

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -227,34 +227,14 @@ public virtual Result<VariationDecisionResult> GetVariation(Experiment experimen
227227
var bucketingId = GetBucketingId(userId, user.GetAttributes()).ResultObject;
228228

229229
#if USE_CMAB
230-
// Check if this is a CMAB experiment
231230
if (experiment.Cmab != null)
232231
{
233232
var cmabDecisionResult =
234233
GetDecisionForCmabExperiment(experiment, user, config, bucketingId, options);
235234
reasons += cmabDecisionResult.DecisionReasons;
236235

237-
var cmabResult = cmabDecisionResult.ResultObject;
238-
if (cmabResult != null)
239-
{
240-
variation = cmabResult.Variation;
241-
242-
// For CMAB experiments, we don't save to user profile
243-
// Return VariationDecisionResult with CMAB UUID
244-
if (variation != null)
245-
{
246-
return Result<VariationDecisionResult>.NewResult(cmabResult, reasons);
247-
}
248-
249-
// If cmabResult.CmabError is true, it means there was an error fetching
250-
// Return null variation but log that it was an error, not just no bucketing
251-
if (cmabResult.CmabError)
252-
{
253-
return Result<VariationDecisionResult>.NullResult(reasons);
254-
}
255-
}
256-
257-
return Result<VariationDecisionResult>.NullResult(reasons);
236+
return Result<VariationDecisionResult>.NewResult(
237+
cmabDecisionResult.ResultObject, reasons);
258238
}
259239
#endif
260240

@@ -312,7 +292,7 @@ OptimizelyDecideOption[] options
312292
var reasons = new DecisionReasons();
313293
var userId = user.GetUserId();
314294

315-
// Create dummy traffic allocation for CMAB
295+
// dummy traffic allocation for CMAB
316296
var cmabTrafficAllocation = new List<TrafficAllocation>
317297
{
318298
new TrafficAllocation
@@ -322,7 +302,6 @@ OptimizelyDecideOption[] options
322302
},
323303
};
324304

325-
// Check if user is in CMAB traffic allocation
326305
var bucketResult = Bucketer.BucketToEntityId(config, experiment, bucketingId, userId,
327306
cmabTrafficAllocation);
328307
reasons += bucketResult.DecisionReasons;
@@ -341,7 +320,6 @@ OptimizelyDecideOption[] options
341320
{
342321
var cmabDecision = CmabService.GetDecision(config, user, experiment.Id, options);
343322

344-
// Get the variation from the project config
345323
var variation = config.GetVariationFromIdByExperimentId(experiment.Id,
346324
cmabDecision.VariationId);
347325

OptimizelySDK/Cmab/CmabConfig.cs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,55 @@ namespace OptimizelySDK.Cmab
2525
public class CmabConfig
2626
{
2727
/// <summary>
28-
/// Initializes a new instance of the CmabConfig class with default cache settings.
28+
/// Gets or sets the maximum number of entries in the CMAB cache.
29+
/// If null, the default value (1000) will be used.
2930
/// </summary>
30-
/// <param name="cacheSize">Maximum number of entries in the cache. Default is 1000.</param>
31-
/// <param name="cacheTtl">Time-to-live for cache entries. Default is 30 minutes.</param>
32-
public CmabConfig(int? cacheSize = null, TimeSpan? cacheTtl = null)
33-
{
34-
CacheSize = cacheSize;
35-
CacheTtl = cacheTtl;
36-
CustomCache = null;
37-
}
31+
public int? CacheSize { get; private set; }
3832

3933
/// <summary>
40-
/// Initializes a new instance of the CmabConfig class with a custom cache implementation.
34+
/// Gets or sets the time-to-live for CMAB cache entries.
35+
/// If null, the default value (30 minutes) will be used.
4136
/// </summary>
42-
/// <param name="customCache">Custom cache implementation for CMAB decisions.</param>
43-
public CmabConfig(ICacheWithRemove<CmabCacheEntry> customCache)
44-
{
45-
CustomCache = customCache ?? throw new ArgumentNullException(nameof(customCache));
46-
CacheSize = null;
47-
CacheTtl = null;
48-
}
37+
public TimeSpan? CacheTtl { get; private set; }
4938

5039
/// <summary>
51-
/// Gets the maximum number of entries in the CMAB cache.
52-
/// If null, the default value (1000) will be used.
40+
/// Gets or sets the custom cache implementation for CMAB decisions.
41+
/// If provided, CacheSize and CacheTtl will be ignored.
5342
/// </summary>
54-
public int? CacheSize { get; }
43+
public ICacheWithRemove<CmabCacheEntry> Cache { get; private set; }
5544

5645
/// <summary>
57-
/// Gets the time-to-live for CMAB cache entries.
58-
/// If null, the default value (30 minutes) will be used.
46+
/// Sets the maximum number of entries in the CMAB cache.
5947
/// </summary>
60-
public TimeSpan? CacheTtl { get; }
48+
/// <param name="cacheSize">Maximum number of entries in the cache.</param>
49+
/// <returns>This CmabConfig instance for method chaining.</returns>
50+
public CmabConfig SetCacheSize(int cacheSize)
51+
{
52+
CacheSize = cacheSize;
53+
return this;
54+
}
6155

6256
/// <summary>
63-
/// Gets the custom cache implementation for CMAB decisions.
64-
/// If provided, CacheSize and CacheTtl will be ignored.
57+
/// Sets the time-to-live for CMAB cache entries.
6558
/// </summary>
66-
public ICacheWithRemove<CmabCacheEntry> CustomCache { get; }
59+
/// <param name="cacheTtl">Time-to-live for cache entries.</param>
60+
/// <returns>This CmabConfig instance for method chaining.</returns>
61+
public CmabConfig SetCacheTtl(TimeSpan cacheTtl)
62+
{
63+
CacheTtl = cacheTtl;
64+
return this;
65+
}
66+
67+
/// <summary>
68+
/// Sets a custom cache implementation for CMAB decisions.
69+
/// When set, CacheSize and CacheTtl will be ignored.
70+
/// </summary>
71+
/// <param name="cache">Custom cache implementation for CMAB decisions.</param>
72+
/// <returns>This CmabConfig instance for method chaining.</returns>
73+
public CmabConfig SetCache(ICacheWithRemove<CmabCacheEntry> cache)
74+
{
75+
Cache = cache ?? throw new ArgumentNullException(nameof(cache));
76+
return this;
77+
}
6778
}
6879
}

OptimizelySDK/Optimizely.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ private void InitializeComponents(IEventDispatcher eventDispatcher = null,
291291
var config = cmabConfig ?? new CmabConfig();
292292
ICacheWithRemove<CmabCacheEntry> cache;
293293

294-
if (config.CustomCache != null)
294+
if (config.Cache != null)
295295
{
296-
cache = config.CustomCache;
296+
cache = config.Cache;
297297
}
298298
else
299299
{

OptimizelySDK/OptimizelyFactory.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public static void SetLogger(ILogger logger)
9696
/// <param name="cacheTtl">Time-to-live for CMAB cache entries.</param>
9797
public static void SetCmabCacheConfig(int cacheSize, TimeSpan cacheTtl)
9898
{
99-
CmabConfiguration = new CmabConfig(cacheSize, cacheTtl);
99+
CmabConfiguration = new CmabConfig()
100+
.SetCacheSize(cacheSize)
101+
.SetCacheTtl(cacheTtl);
100102
}
101103

102104
/// <summary>
@@ -105,7 +107,8 @@ public static void SetCmabCacheConfig(int cacheSize, TimeSpan cacheTtl)
105107
/// <param name="customCache">Custom cache implementation.</param>
106108
public static void SetCmabCustomCache(ICacheWithRemove<CmabCacheEntry> customCache)
107109
{
108-
CmabConfiguration = new CmabConfig(customCache);
110+
CmabConfiguration = new CmabConfig()
111+
.SetCache(customCache);
109112
}
110113
#endif
111114

0 commit comments

Comments
 (0)