Esempio n. 1
0
        public async Task TryGet_EventsAreTriggeredSuccessfully(bool flag1, bool flag2)
        {
            var config = new DistributedCacheEventsWrapperConfig <int, int>();

            var successfulResults = new List <(int, bool, int, TimeSpan)>();
            var failedResults     = new List <(int, TimeSpan, Exception)>();

            if (flag1)
            {
                config.OnTryGetCompletedSuccessfully = (key, found, value, duration) =>
                {
                    successfulResults.Add((key, found, value, duration));
                };
            }

            if (flag2)
            {
                config.OnTryGetException = (key, duration, exception) =>
                {
                    failedResults.Add((key, duration, exception));
                    return(key == 3);
                };
            }

            var innerCache = new MockDistributedCache <int, int>();
            var cache      = new DistributedCacheEventsWrapper <int, int>(config, innerCache);

            await cache.TryGet(1).ConfigureAwait(false);

            if (flag1)
            {
                successfulResults.Should().ContainSingle();
                successfulResults.Last().Item1.Should().Be(1);
                successfulResults.Last().Item2.Should().BeFalse();
                successfulResults.Last().Item3.Should().Be(0);
                successfulResults.Last().Item4.Should().BePositive().And.BeCloseTo(TimeSpan.Zero, TimeSpan.FromMilliseconds(100));
            }
            else
            {
                successfulResults.Should().BeEmpty();
            }

            await cache.Set(2, 3, TimeSpan.FromSeconds(1)).ConfigureAwait(false);

            await cache.TryGet(2).ConfigureAwait(false);

            if (flag1)
            {
                successfulResults.Should().HaveCount(2);
                successfulResults.Last().Item1.Should().Be(2);
                successfulResults.Last().Item2.Should().BeTrue();
                successfulResults.Last().Item3.Should().Be(3);
                successfulResults.Last().Item4.Should().BePositive().And.BeCloseTo(TimeSpan.Zero, TimeSpan.FromMilliseconds(100));
            }
            else
            {
                successfulResults.Should().BeEmpty();
            }

            innerCache.ThrowExceptionOnNextAction();
            Func <Task> action = () => cache.TryGet(3);

            if (flag2)
            {
                await action().ConfigureAwait(false);

                failedResults.Should().ContainSingle();
                failedResults.Last().Item1.Should().Be(3);
                failedResults.Last().Item2.Should().BePositive().And.BeCloseTo(TimeSpan.Zero, TimeSpan.FromMilliseconds(100));
            }
            else
            {
                await action.Should().ThrowAsync <Exception>().ConfigureAwait(false);

                failedResults.Should().BeEmpty();
            }
        }