예제 #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);
        }
        public async Task GetAllSettings__CalledAfterAdd__CouldBeInvalidated()
        {
            var cacheManager = new ClientCacheManager();
            var client       = Factory.CreateNew(Fixture.ClientUrl, "default", true, cacheManager,
                                                 new RequestInterceptorHandler(Fixture.Client));

            var allSettings = await client.GetAllSettingsAsync();

            await client.CreateAsync(new BlockchainSettingsCreateRequest()
            {
                HotWalletAddress = "ANY_ADDRESS_POSSIBLE",
                Type             = "TYPE_1",
                SignServiceUrl   = "http://fake.sign.com/",
                ApiUrl           = "http://fake.api.com/"
            });

            var allSettings2 = await client.GetAllSettingsAsync();

            var allSettingsCountBeforeInvalidation = allSettings2.Collection.Count();
            await cacheManager.InvalidateCacheAsync();

            var allSettings3 = await client.GetAllSettingsAsync();

            Assert.IsNotNull(allSettings.Collection);
            Assert.IsNotNull(allSettings2.Collection);
            Assert.IsNotNull(allSettings3.Collection);
            Assert.IsTrue(allSettings.Collection.Count() == 1);
            Assert.IsTrue(allSettingsCountBeforeInvalidation == 1);
            Assert.IsTrue(allSettings3.Collection.Count() == 2);
        }
예제 #3
0
        public async Task GetAllExplorersAsync__CalledAfterAdd__CouldBeInvalidated()
        {
            var cacheManager = new ClientCacheManager();
            var client       = Factory.CreateNew(Fixture.ClientUrl, "default", true, cacheManager,
                                                 new RequestInterceptorHandler(Fixture.Client));

            var allSettings = await client.GetAllExplorersAsync();

            await client.CreateBlockchainExplorerAsync(new BlockchainExplorerCreateRequest
            {
                Name                = "Ropsten1",
                BlockchainType      = "EthereumClassic",
                ExplorerUrlTemplate = "https://ropsten.etherscan.io/tx/{tx-hash}"
            });

            var allSettings2 = await client.GetAllExplorersAsync();

            var allSettingsCountBeforeInvalidation = allSettings2.Collection.Count();
            await cacheManager.InvalidateCacheAsync();

            var allSettings3 = await client.GetAllExplorersAsync();

            Assert.IsNotNull(allSettings.Collection);
            Assert.IsNotNull(allSettings2.Collection);
            Assert.IsNotNull(allSettings3.Collection);
            Assert.IsTrue(allSettings.Collection.Count() == 1);
            Assert.IsTrue(allSettingsCountBeforeInvalidation == 1);
            Assert.IsTrue(allSettings3.Collection.Count() == 2);
        }
        public async Task WhenObjectNotDisposed_InvalidateAllOnEachCall()
        {
            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();

            await cacheManager.InvalidateCacheAsync();

            foreach (var subscriber in subscribers)
            {
                //2 Time for each subscriber
                subscriber.CacheInvalidatedCallsCount.Should().Be(2);
            }
        }
예제 #5
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);
        }