| 
1 | 1 | /*  | 
2 |  | - *    Copyright 2020-2021, 2022-2023 Optimizely and contributors  | 
 | 2 | + *    Copyright 2020-2024 Optimizely and contributors  | 
3 | 3 |  *  | 
4 | 4 |  *    Licensed under the Apache License, Version 2.0 (the "License");  | 
5 | 5 |  *    you may not use this file except in compliance with the License.  | 
 | 
16 | 16 | 
 
  | 
17 | 17 | using System;  | 
18 | 18 | using System.Collections.Generic;  | 
19 |  | -using System.Threading;  | 
20 | 19 | using Castle.Core.Internal;  | 
21 | 20 | using Moq;  | 
22 | 21 | using NUnit.Framework;  | 
@@ -193,7 +192,7 @@ public void SetAttributeToOverrideAttribute()  | 
193 | 192 |             Assert.AreEqual(user.GetAttributes()["k1"], true);  | 
194 | 193 |         }  | 
195 | 194 | 
 
  | 
196 |  | -        #region decide  | 
 | 195 | +        #region Decide  | 
197 | 196 | 
 
  | 
198 | 197 |         [Test]  | 
199 | 198 |         public void TestDecide()  | 
@@ -408,11 +407,43 @@ public void DecideWhenConfigIsNull()  | 
408 | 407 | 
 
  | 
409 | 408 |             Assert.IsTrue(TestData.CompareObjects(decision, decisionExpected));  | 
410 | 409 |         }  | 
 | 410 | +          | 
 | 411 | +        [Test]  | 
 | 412 | +        public void DecideWithUspShouldOnlyLookupSaveOnce()  | 
 | 413 | +        {  | 
 | 414 | +            var flagKeyFromTestDataJson = "double_single_variable_feature";  | 
 | 415 | +            var userProfileServiceMock = new Mock<UserProfileService>();  | 
 | 416 | +            var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,  | 
 | 417 | +                LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);  | 
 | 418 | +            var user = optimizely.CreateUserContext(UserID);  | 
 | 419 | +              | 
 | 420 | +            _ = user.Decide(flagKeyFromTestDataJson);  | 
 | 421 | +              | 
 | 422 | +            userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);  | 
 | 423 | +            userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),  | 
 | 424 | +                Times.Once);  | 
 | 425 | +        }  | 
411 | 426 | 
 
  | 
412 |  | -        #endregion decide  | 
 | 427 | +        #endregion Decide  | 
413 | 428 | 
 
  | 
414 |  | -        #region decideAll  | 
 | 429 | +        #region DecideForKeys  | 
415 | 430 | 
 
  | 
 | 431 | +        [Test]  | 
 | 432 | +        public void DecideForKeysWithUspShouldOnlyLookupSaveOnceWithMultipleFlags()  | 
 | 433 | +        {  | 
 | 434 | +            var flagKeys = new [] { "double_single_variable_feature", "boolean_feature" };  | 
 | 435 | +            var userProfileServiceMock = new Mock<UserProfileService>();  | 
 | 436 | +            var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,  | 
 | 437 | +                LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);  | 
 | 438 | +            var userContext = optimizely.CreateUserContext(UserID);  | 
 | 439 | +              | 
 | 440 | +            _ = userContext.DecideForKeys(flagKeys);  | 
 | 441 | +              | 
 | 442 | +            userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);  | 
 | 443 | +            userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),  | 
 | 444 | +                Times.Once);  | 
 | 445 | +        }  | 
 | 446 | +          | 
416 | 447 |         [Test]  | 
417 | 448 |         public void DecideForKeysWithOneFlag()  | 
418 | 449 |         {  | 
@@ -442,6 +473,25 @@ public void DecideForKeysWithOneFlag()  | 
442 | 473 |                 new string[0]);  | 
443 | 474 |             Assert.IsTrue(TestData.CompareObjects(decision, expDecision));  | 
444 | 475 |         }  | 
 | 476 | +          | 
 | 477 | +        #endregion DecideForKeys  | 
 | 478 | +          | 
 | 479 | +        #region DecideAll  | 
 | 480 | +          | 
 | 481 | +        [Test]  | 
 | 482 | +        public void DecideAllWithUspShouldOnlyLookupSaveOnce()  | 
 | 483 | +        {  | 
 | 484 | +            var userProfileServiceMock = new Mock<UserProfileService>();  | 
 | 485 | +            var optimizely = new Optimizely(TestData.Datafile, EventDispatcherMock.Object,  | 
 | 486 | +                LoggerMock.Object, ErrorHandlerMock.Object, userProfileServiceMock.Object);  | 
 | 487 | +            var user = optimizely.CreateUserContext(UserID);  | 
 | 488 | +              | 
 | 489 | +            _ = user.DecideAll();  | 
 | 490 | +              | 
 | 491 | +            userProfileServiceMock.Verify(l => l.Lookup(UserID), Times.Once);  | 
 | 492 | +            userProfileServiceMock.Verify(l => l.Save(It.IsAny<Dictionary<string, object>>()),  | 
 | 493 | +                Times.Once);  | 
 | 494 | +        }  | 
445 | 495 | 
 
  | 
446 | 496 |         [Test]  | 
447 | 497 |         public void DecideAllTwoFlag()  | 
 | 
0 commit comments