示例#1
0
        public async Task WorksCorrectlyForOneThread_InvalidateCacheAsync()
        {
            // arrange
            var(h3, y1) = MakeFakeHttpClientHandler();

            // act

            var httpBuilder   = HttpClientGenerator.BuildForUrl("http://fake.host");
            var cacheManager  = new ClientCacheManager();
            var testInterface = httpBuilder
                                .WithCachingStrategy(new AttributeBasedCachingStrategy())
                                .WithAdditionalDelegatingHandler(h3)
                                .Create(cacheManager)
                                .Generate <ITestInterface>();

            var result = await testInterface.TestMethodWithCache();

            var result1 = await testInterface.TestMethodWithCache();

            cacheManager.InvalidateCacheAsync().Wait();
            var result2 = await testInterface.TestMethodWithCache();

            cacheManager.Dispose();
            // assert
            result.Should().Be(_result1Str);
            result1.Should().Be(_result1Str);
            result2.Should().Be(_result2Str);
        }
示例#2
0
        public async Task WorksCorrectlyForManyThreads_InvalidateCacheAsync()
        {
            // arrange
            var(h3, y1) = MakeFakeHttpClientHandler();

            var bag           = new ConcurrentBag <List <string> >();
            var httpBuilder   = HttpClientGenerator.BuildForUrl("http://fake.host");
            var cacheManager  = new ClientCacheManager();
            var testInterface = httpBuilder
                                .WithCachingStrategy(new AttributeBasedCachingStrategy())
                                .WithAdditionalDelegatingHandler(h3)
                                .Create(cacheManager)
                                .Generate <ITestInterface>();

            var cacheModifyingTasks = Enumerable
                                      .Range(0, 100)
                                      .Select(x => Task.Factory.StartNew(async() =>
            {
                var list    = new List <string>(2);
                var result1 = await testInterface.TestMethodWithCache();
                await cacheManager.InvalidateCacheAsync();
                var result2 = await testInterface.TestMethodWithCache();

                list.Add($"{Thread.CurrentThread.Name} {result1}");
                list.Add($"{Thread.CurrentThread.Name} {result2}");
                bag.Add(list);
            }).Unwrap());

            // act

            await Task.WhenAll(cacheModifyingTasks);

            cacheManager.Dispose();
            var results   = bag.ToArray().SelectMany(x => x);
            var res2Count = results.Count(x => x.Contains(_result2Str));

            // assert
            res2Count.Should().BeGreaterThan(0);
        }
        public async Task WhenObjectDisposed_UnsubscribeAll()
        {
            var subscribers = Enumerable.Range(0, 10)
                              .Select(x => new MockSubscriber())
                              .ToList();

            ClientCacheManager cacheManager = new ClientCacheManager();

            foreach (var subscriber in subscribers)
            {
                cacheManager.OnInvalidate += subscriber.InvalidateCacheAsync;
            }

            await cacheManager.InvalidateCacheAsync();

            cacheManager.Dispose();
            await cacheManager.InvalidateCacheAsync();

            foreach (var subscriber in subscribers)
            {
                //1 Time for each subscriber
                subscriber.CacheInvalidatedCallsCount.Should().Be(1);
            }
        }