Esempio n. 1
0
        public async Task TryGetItemAsync_UsesCache()
        {
            var repository        = GetMockRepository();
            var cachingRepository = new CachingWithRetriesRepository <TesTask>(repository.Object);

            TesTask tesTask = null;
            var     success = await cachingRepository.TryGetItemAsync("tesTaskId1", item => tesTask = item);

            var success2 = await cachingRepository.TryGetItemAsync("tesTaskId1", item => tesTask = item);

            repository.Verify(mock => mock.TryGetItemAsync("tesTaskId1", It.IsAny <Action <TesTask> >()), Times.Once());
            Assert.IsTrue(success);
            Assert.IsTrue(success2);
        }
Esempio n. 2
0
        public async Task TryGetItemAsync_IfItemNotFound_DoesNotSetCache()
        {
            var repository        = GetMockRepository();
            var cachingRepository = new CachingWithRetriesRepository <TesTask>(repository.Object);

            TesTask tesTask = null;
            var     success = await cachingRepository.TryGetItemAsync("notFound", item => tesTask = item);

            var success2 = await cachingRepository.TryGetItemAsync("notFound", item => tesTask = item);

            repository.Verify(mock => mock.TryGetItemAsync("notFound", It.IsAny <Action <TesTask> >()), Times.Exactly(2));
            Assert.IsFalse(success);
            Assert.IsFalse(success2);
        }
Esempio n. 3
0
        public async Task DeleteItemAsync_RemovesItemFromCache()
        {
            var repository        = GetMockRepository();
            var cachingRepository = new CachingWithRetriesRepository <TesTask>(repository.Object);
            RepositoryItem <TesTask>           repositoryItem = null;
            Action <RepositoryItem <TesTask> > action         = item => repositoryItem = item;

            var success = await cachingRepository.TryGetItemAsync("tesTask1", action);

            await cachingRepository.DeleteItemAsync("tesTask1");

            var success2 = await cachingRepository.TryGetItemAsync("tesTask1", action);

            repository.Verify(mock => mock.TryGetItemAsync("tesTask1", It.IsAny <Action <RepositoryItem <TesTask> > >()), Times.Exactly(2));
            repository.Verify(mock => mock.DeleteItemAsync("tesTask1"), Times.Once());
            Assert.AreEqual(success, success2);
        }
Esempio n. 4
0
        public async Task TryGetItemAsync_ThrowsException_DoesNotSetCache()
        {
            SystemClock.SleepAsync = (_, __) => Task.FromResult(true);
            SystemClock.Sleep      = (_, __) => { };
            var repository        = GetMockRepository();
            var cachingRepository = new CachingWithRetriesRepository <TesTask>(repository.Object);

            TesTask tesTask = null;
            await Assert.ThrowsExceptionAsync <Exception>(async() => await cachingRepository.TryGetItemAsync("throws", item => tesTask = item));

            repository.Verify(mock => mock.TryGetItemAsync("throws", It.IsAny <Action <TesTask> >()), Times.Exactly(4));
        }