diff --git a/.github/workflows/csharp.yml b/.github/workflows/csharp.yml index 4ad1218b..25a3fb48 100644 --- a/.github/workflows/csharp.yml +++ b/.github/workflows/csharp.yml @@ -8,6 +8,7 @@ on: jobs: failOnDraftPullRequest: + name: Fail If Draft Pull Request if: github.event.pull_request.draft == true runs-on: ubuntu-latest steps: diff --git a/OptimizelySDK/Bucketing/DecisionService.cs b/OptimizelySDK/Bucketing/DecisionService.cs index dc2a2361..60830f3f 100644 --- a/OptimizelySDK/Bucketing/DecisionService.cs +++ b/OptimizelySDK/Bucketing/DecisionService.cs @@ -85,21 +85,6 @@ public DecisionService(Bucketer bucketer, IErrorHandler errorHandler, #endif } - /// - /// Get a Variation of an Experiment for a user to be allocated into. - /// - /// The Experiment the user will be bucketed into. - /// Optimizely user context. - /// Project config. - /// The Variation the user is allocated into. - public virtual Result GetVariation(Experiment experiment, - OptimizelyUserContext user, - ProjectConfig config - ) - { - return GetVariation(experiment, user, config, new OptimizelyDecideOption[] { }); - } - /// /// Get a Variation of an Experiment for a user to be allocated into. /// @@ -107,13 +92,19 @@ ProjectConfig config /// optimizely user context. /// Project Config. /// An array of decision options. + /// Whether to force a lookup of the user profile when UPS is enabled. /// The Variation the user is allocated into. public virtual Result GetVariation(Experiment experiment, OptimizelyUserContext user, ProjectConfig config, - OptimizelyDecideOption[] options + OptimizelyDecideOption[] options = null, + bool forceUserProfileLookup = false ) { + if (options == null) + { + options = new OptimizelyDecideOption[] { }; + } var reasons = new DecisionReasons(); var userId = user.GetUserId(); if (!ExperimentUtils.IsExperimentActive(experiment, Logger)) @@ -149,7 +140,7 @@ OptimizelyDecideOption[] options { try { - userProfile = _userProfileCache.GetUserProfile(userId); + userProfile = _userProfileCache.GetUserProfile(userId, forceUserProfileLookup); decisionVariationResult = GetStoredVariation(experiment, userProfile, config); reasons += decisionVariationResult.DecisionReasons; if (decisionVariationResult.ResultObject != null) diff --git a/OptimizelySDK/Bucketing/UserProfileCache.cs b/OptimizelySDK/Bucketing/UserProfileCache.cs index 7611e8da..d34833e2 100644 --- a/OptimizelySDK/Bucketing/UserProfileCache.cs +++ b/OptimizelySDK/Bucketing/UserProfileCache.cs @@ -33,9 +33,9 @@ public UserProfileCache(UserProfileService userProfileService, ILogger logger) _logger = logger; } - public UserProfile GetUserProfile(string userId) + public UserProfile GetUserProfile(string userId, bool forceLookup = false) { - if (_cache.TryGetValue(userId, out var userProfile)) + if (_cache.TryGetValue(userId, out var userProfile) && !forceLookup) { return _cache[userId]; } diff --git a/OptimizelySDK/Optimizely.cs b/OptimizelySDK/Optimizely.cs index 8235882a..3b5c37fa 100644 --- a/OptimizelySDK/Optimizely.cs +++ b/OptimizelySDK/Optimizely.cs @@ -310,7 +310,8 @@ public Variation Activate(string experimentKey, string userId, return null; } - var variation = GetVariation(experimentKey, userId, config, userAttributes); + var variation = GetVariation(experimentKey, userId, config, userAttributes, + forceUserProfileLookup: true); if (variation == null || variation.Key == null) { @@ -404,7 +405,8 @@ public Variation GetVariation(string experimentKey, string userId, ) { var config = ProjectConfigManager?.GetConfig(); - return GetVariation(experimentKey, userId, config, userAttributes); + return GetVariation(experimentKey, userId, config, userAttributes, + forceUserProfileLookup: true); } /// @@ -414,9 +416,10 @@ public Variation GetVariation(string experimentKey, string userId, /// ID for the user /// ProjectConfig to be used for variation /// Attributes for the users + /// Whether to force a lookup of the user profile when UPS is enabled. /// null|Variation Representing variation private Variation GetVariation(string experimentKey, string userId, ProjectConfig config, - UserAttributes userAttributes = null + UserAttributes userAttributes = null, bool forceUserProfileLookup = false ) { if (config == null) @@ -442,8 +445,10 @@ private Variation GetVariation(string experimentKey, string userId, ProjectConfi userAttributes = userAttributes ?? new UserAttributes(); var userContext = CreateUserContextCopy(userId, userAttributes); - var variation = DecisionService.GetVariation(experiment, userContext, config) - ?.ResultObject; + var variation = DecisionService. + GetVariation(experiment, userContext, config, + forceUserProfileLookup: forceUserProfileLookup)?. + ResultObject; var decisionInfo = new Dictionary { { "experimentKey", experimentKey }, { "variationKey", variation?.Key },