예제 #1
0
        public void TryGetValue_UnderLargeLoads_ReturnsValuesQuickly()
        {
            //Arrange
            DateTime      startTime = DateTime.Now;
            CacheTestData data      = new CacheTestData();

            //Act
            _cache = new SlickCache <int, string>(1000001);
            AddMembers(1000000);
            for (int key = 0; key < 1000000; key++)
            {
                try
                {
                    data.GetSuccessful = _cache.TryGetValue(key, out data.Value);
                    data.ExpectedValue = (key * key).ToString();
                }
                catch (Exception e)
                {
                    data.Errors = e.Message;
                }

                //Assert
                //Test normal below capacity behaviour
                Assert.That(data.GetSuccessful, Is.True, $"Retrieval was not successfull for key: {key}. Due to: {data.Errors}.");
                Assert.That(data.Value, Is.EqualTo(data.ExpectedValue));
            }
            var secs = DateTime.Now.Subtract(startTime).Seconds;

            Assert.That(secs < 20, $"Expected Operation to take < 10 seconds, but took {secs}.");
        }
예제 #2
0
        public void MaxCacheSize_InheritsFromPolicyWhenSpecified_InsteadOfParameter()
        {
            //Arrange
            CacheTestData[] data =
            {
                new CacheTestData {
                    ExpectedValue = "0"
                },
                new CacheTestData {
                    ExpectedValue = "1"
                }
            };
            _cache = new SlickCache <int, string>(1, _evictionPolicy);

            //Act
            //By adding 2 members, if the cache policy is used, then we will be able to retrieve both members.
            //If the maxSize of 1 paramater specified above is used, the 2nd value will be null.
            AddMembers(2);
            for (int index = 0; index < data.Length; index++)
            {
                CacheTestData test = data[index];
                try
                {
                    test.GetSuccessful = _cache.TryGetValue(index, out test.Value);
                }
                catch (Exception e)
                {
                    test.Errors = e.Message;
                }

                //Assert
                Assert.That(test.Value, Is.EqualTo(test.ExpectedValue), $"Value was not what was expected. Errors: {test.Errors}");
                Assert.That(test.GetSuccessful, Is.EqualTo(true));
            }
        }
예제 #3
0
        public void TryGetValue_WithValueOutOfRange_ReturnsNegativeResult()
        {
            //Arrange
            CacheTestData data = new CacheTestData();

            //Act
            AddMembers(1);
            for (int key = 0; key < 3; key++)
            {
                try
                {
                    data.GetSuccessful = _cache.TryGetValue(key, out data.Value);
                }
                catch (Exception e)
                {
                    data.Errors = e.Message;
                }

                //Assert
                //Test retrieval out of range
                if (key > 1)
                {
                    Assert.That(data.GetSuccessful, Is.False, $"Was not expecting positive result for key: {key}. Due to: {data.Errors}.");
                    Assert.That(data.Value, Is.EqualTo(null));
                }
            }
        }
예제 #4
0
        public void AddOrUpdate_SuccessfullyUpdatesCache_ByReturningUpdatedValue()
        {
            //Arrange
            CacheTestData data = new CacheTestData()
            {
                ExpectedValue = "2"
            };

            //Act
            try
            {
                AddMembers(2);
                //Test updating the value of the first key to 2, instead of the default 0.
                _cache.AddOrUpdate(0, "2");
                _cache.TryGetValue(0, out data.Value);
            }
            catch (Exception e)
            {
                data.Errors = e.Message;
            }

            //Assert
            Assert.That(data.Errors, Is.Null.Or.Empty, $"Expected there to be no exceptions during update. Error: {data.Errors}.");
            Assert.That(data.Value, Is.EqualTo(data.ExpectedValue), $"Expected value to be the updated value of {data.ExpectedValue}, not {data.Value}.");
        }
예제 #5
0
        public void AddOrUpdate_InsertedBelowCapacity_SuccessfullyReturnsSameValue()
        {
            //Arrange
            CacheTestData data = new CacheTestData();

            //Act
            AddMembers(2);
            for (int key = 0; key < 2; key++)
            {
                try
                {
                    data.GetSuccessful = _cache.TryGetValue(key, out data.Value);
                    data.ExpectedValue = (key * key).ToString(); //We expect the value to follow this formula used at creation.
                }
                catch (Exception e)
                {
                    data.Errors = e.Message;
                }

                //Assert
                //Test normal below capacity behaviour
                Assert.That(data.GetSuccessful, Is.True, $"Retrieval was not successfull for key: {key}. Due to {data.Errors}.");
                Assert.That(data.Value, Is.EqualTo(data.ExpectedValue));
            }
        }
예제 #6
0
        public void TryGetValue_BetweenMostRecentAndLeastRecent_ReturnsValidValue()
        {
            //Arrange
            CacheTestData data = new CacheTestData();

            //Act
            AddMembers(3);
            try
            {
                data.GetSuccessful = _cache.TryGetValue(1, out data.Value);
            }
            catch (Exception e)
            {
                data.Errors = e.Message;
            }

            //Assert
            Assert.That(data.GetSuccessful, Is.True);
            Assert.That(data.Value, Is.Not.Null.Or.Empty);
        }
예제 #7
0
        public void InitialCacheState_StartsEmpty_AndReturnsNothing()
        {
            //Arrange
            CacheTestData data = new CacheTestData {
                ExpectedValue = null
            };

            //Act
            try
            {
                data.GetSuccessful = _cache.TryGetValue(0, out data.Value);
            }
            catch (Exception e)
            {
                data.Errors = e.Message;
            }

            //Assert
            Assert.That(data.Value, Is.EqualTo(data.ExpectedValue), $"Expected value to be null. Error: {data.Errors}.");
            Assert.That(data.GetSuccessful, Is.EqualTo(false));
        }