Пример #1
0
        public static async Task GetOrAddAsync_TwoTimesCalledWithNoDateTimeExpire_ResultAlwaysTheSameAndGetValueFuncCalledOneTime(ICache <int, string> cache)
        {
            const string testValue = "Test1";

            var getValueCalled  = false;
            var getValueCalled1 = false;
            var getValueCalled2 = false;
            var value           = await cache.GetOrAddAsync(1, () =>
            {
                getValueCalled = true;
                return(testValue);
            }, CacheExpirationPolicy.AfterAbsoluteDateTime(DateTime.Now.AddMilliseconds(400))).ConfigureAwait(false);

            Assert.Equal(testValue, value);

            await Task.Delay(50).ConfigureAwait(false);

            var secondValue = await cache.GetOrAddAsync(1, () =>
            {
                getValueCalled1 = true;
                return(testValue);
            }, CacheExpirationPolicy.AfterAbsoluteDateTime(DateTime.Now.AddMilliseconds(400))).ConfigureAwait(false);

            await Task.Delay(400).ConfigureAwait(false);

            var thirdValue = await cache.GetOrAddAsync(1, () =>
            {
                getValueCalled2 = true;
                return(testValue);
            }, CacheExpirationPolicy.AfterAbsoluteDateTime(DateTime.Now.AddMilliseconds(200))).ConfigureAwait(false);

            Assert.Equal(testValue, secondValue);
            Assert.Equal(testValue, thirdValue);
            Assert.True(getValueCalled);
            Assert.False(getValueCalled1);
            Assert.True(getValueCalled2);
        }
Пример #2
0
        public static async Task GetOrAddAsync_TwoTimesCalledWithDateTimeExpire_ResultAlwaysTheSameAndGetValueFuncCalledTwoTime(ICache <int, string> cache)
        {
            const string testValue = "Test1";

            var getValueCalledCount = 0;
            var value = await cache.GetOrAddAsync(1, () =>
            {
                getValueCalledCount++;
                return(testValue);
            }, CacheExpirationPolicy.AfterAbsoluteDateTime(DateTime.Now.AddMilliseconds(50))).ConfigureAwait(false);

            Assert.Equal(testValue, value);

            await Task.Delay(100).ConfigureAwait(false);

            var secondValue = await cache.GetOrAddAsync(1, () =>
            {
                getValueCalledCount++;
                return(testValue);
            }, CacheExpirationPolicy.NeverExpire).ConfigureAwait(false);

            Assert.Equal(testValue, secondValue);
            Assert.Equal(2, getValueCalledCount);
        }