Esempio n. 1
0
        public async Task ExecuteWithCache_WhenValueIsNotCachedWithConcurrencyAndAttributes_ShouldReturnCorrectValue(
            [Frozen] Mock <ICacheEntryRepository> cacheEntryRepo,
            CacheService sut,
            string key,
            TestCacheReturnValue expected,
            TestCacheReturnValue notExpected,
            Dictionary <string, string> attributes)
        {
            //arrange
            cacheEntryRepo.Setup(c => c.Get <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, attributes))
            .ReturnsTask(default(CacheEntry <TestCacheReturnValue>));


            //act
            Task <TestCacheReturnValue> actualTask = null;
            await sut.ExecuteWithCache(CancellationToken.None, key, () =>
            {
                actualTask = sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(notExpected), TimeSpan.Zero, attributes);
                return(Task.FromResult(expected));
            }, TimeSpan.Zero, attributes);

            var actual = await actualTask;

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 2
0
        public async Task ExecuteWithCache_WhenValueIsCachedAndAgeEntryIsTooOldWithAttributes_ShouldAddNewEntry(
            [Frozen] Mock <ICacheEntryRepository> cacheEntryRepo,
            CacheService sut,
            string key,
            TestCacheReturnValue expected,
            TestCacheReturnValue notExpected,
            DateTimeOffset now,
            Guid id,
            Dictionary <string, string> attributes)
        {
            //arrange
            cacheEntryRepo.Setup(c => c.Get <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, attributes))
            .ReturnsTask(new CacheEntry <TestCacheReturnValue>(
                             id,
                             key,
                             new Dictionary <string, string>(),
                             now.Subtract(TimeSpan.FromMinutes(10)),
                             notExpected));
            cacheEntryRepo.Setup(c => c.AddOrUpdate(CancellationToken.None, It.Is <CacheEntry <TestCacheReturnValue> >(entry =>
                                                                                                                       entry.CacheKey == key &&
                                                                                                                       entry.Value == expected &&
                                                                                                                       entry.Attributes == attributes)))
            .ReturnsDefaultTask()
            .Verifiable();

            //act
            await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.FromMinutes(5), attributes);

            //assert
            cacheEntryRepo.Verify();
        }
Esempio n. 3
0
        public async Task ExecuteWithCache_WhenValueIsNotCached_ShouldReturnCorrectValue(
            [Frozen] Mock <ICacheEntryRepository> cacheEntryRepo,
            CacheService sut,
            string key,
            TestCacheReturnValue expected)
        {
            //arrange
            cacheEntryRepo.Setup(c => c.Get <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, It.Is <IDictionary <string, string> >(d => d.Count == 0)))
            .ReturnsTask(default(CacheEntry <TestCacheReturnValue>));

            //act
            var actual = await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.Zero);

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 4
0
        public async Task Invalidate_WithIsYoungerThanMinAge_ShouldNotRemoveCacheEntry(
            [Frozen] Mock <ICacheEntryRepository> cacheEntryRepo,
            CacheService sut,
            string key,
            TestCacheReturnValue value,
            DateTimeOffset now)
        {
            //arrange
            var entry = new CacheEntry <TestCacheReturnValue>(Guid.NewGuid(), key, new Dictionary <string, string>(), now.Subtract(TimeSpan.FromMinutes(2)), value);

            cacheEntryRepo.Setup(c => c.Get <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, It.Is <IDictionary <string, string> >(d => d.Count == 0)))
            .ReturnsTask(entry);

            //act
            await sut.Invalidate <TestCacheReturnValue>(CancellationToken.None, key, TimeSpan.FromMinutes(2));

            //assert
            cacheEntryRepo.Verify(c => c.Remove <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, entry.Id), Times.Never());
        }
Esempio n. 5
0
        public async Task ExecuteWithCache_WhenValueIsCachedWithAttributes_ShouldReturnCorrectValue(
            [Frozen] Mock <ICacheEntryRepository> cacheEntryRepo,
            CacheService sut,
            string key,
            TestCacheReturnValue expected,
            TestCacheReturnValue notExpected,
            DateTimeOffset now,
            Dictionary <string, string> attributes)
        {
            //arrange
            cacheEntryRepo.Setup(c => c.Get <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, attributes))
            .ReturnsTask(new CacheEntry <TestCacheReturnValue>(Guid.NewGuid(), key, attributes, now, expected));

            //act
            var actual = await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(notExpected), TimeSpan.Zero, attributes);

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 6
0
        public async Task ExecuteWithCache_WhenValueIsNotCached_ShouldAddEntryToCache(
            [Frozen] Mock <ICacheEntryRepository> cacheEntryRepo,
            CacheService sut,
            string key,
            TestCacheReturnValue expected)
        {
            //arrange
            cacheEntryRepo.Setup(c => c.Get <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, It.Is <IDictionary <string, string> >(d => d.Count == 0)))
            .ReturnsTask(default(CacheEntry <TestCacheReturnValue>));
            cacheEntryRepo.Setup(c => c.AddOrUpdate(CancellationToken.None, It.Is <CacheEntry <TestCacheReturnValue> >(entry =>
                                                                                                                       entry.CacheKey == key && entry.Value == expected)))
            .ReturnsDefaultTask()
            .Verifiable();

            //act
            await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.Zero);

            //assert
            cacheEntryRepo.Verify();
        }
Esempio n. 7
0
        public async Task ExecuteWithCache_WhenValueIsCachedAndAgeEntryIsTooOld_ShouldReturnCorrectValue(
            [Frozen] Mock <ICacheEntryRepository> cacheEntryRepo,
            CacheService sut,
            string key,
            TestCacheReturnValue expected,
            TestCacheReturnValue notExpected,
            DateTimeOffset now)
        {
            //arrange
            cacheEntryRepo.Setup(c => c.Get <TestCacheReturnValue>(It.IsAny <CancellationToken>(), key, It.Is <IDictionary <string, string> >(d => d.Count == 0)))
            .ReturnsTask(new CacheEntry <TestCacheReturnValue>(
                             Guid.NewGuid(),
                             key,
                             new Dictionary <string, string>(),
                             now.Subtract(TimeSpan.FromMinutes(10)),
                             notExpected));

            //act
            var actual = await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.FromMinutes(5));

            //assert
            actual.Should().Be(expected);
        }
Esempio n. 8
0
		public async Task ExecuteWithCache_WhenValueIsNotCached_ShouldReturnCorrectValue(
			[Frozen] Mock<ICacheEntryRepository> cacheEntryRepo,
			CacheService sut,
			string key,
			TestCacheReturnValue expected)
		{
			//arrange
			cacheEntryRepo.Setup(c => c.Get<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, It.Is<IDictionary<string, string>>(d => d.Count == 0)))
			              .ReturnsTask(default(CacheEntry<TestCacheReturnValue>));

			//act
			var actual = await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.Zero);

			//assert
			actual.Should().Be(expected);
		}
Esempio n. 9
0
		public async Task Invalidate_WithIsYoungerThanMinAge_ShouldNotRemoveCacheEntry(
			[Frozen] Mock<ICacheEntryRepository> cacheEntryRepo,
			CacheService sut,
			string key,
			TestCacheReturnValue value,
			DateTimeOffset now)
		{
			//arrange
			var entry = new CacheEntry<TestCacheReturnValue>(Guid.NewGuid(), key, new Dictionary<string, string>(), now.Subtract(TimeSpan.FromMinutes(2)), value);
			cacheEntryRepo.Setup(c => c.Get<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, It.Is<IDictionary<string, string>>(d => d.Count == 0)))
			              .ReturnsTask(entry);

			//act
			await sut.Invalidate<TestCacheReturnValue>(CancellationToken.None, key, TimeSpan.FromMinutes(2));

			//assert
			cacheEntryRepo.Verify(c => c.Remove<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, entry.Id), Times.Never());
		}
Esempio n. 10
0
		public async Task ExecuteWithCache_WhenValueIsNotCachedWithConcurrencyAndAttributes_ShouldReturnCorrectValue(
			[Frozen] Mock<ICacheEntryRepository> cacheEntryRepo,
			CacheService sut,
			string key,
			TestCacheReturnValue expected,
			TestCacheReturnValue notExpected,
			Dictionary<string, string> attributes)
		{
			//arrange
			cacheEntryRepo.Setup(c => c.Get<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, attributes))
			              .ReturnsTask(default(CacheEntry<TestCacheReturnValue>));


			//act
			Task<TestCacheReturnValue> actualTask = null;
			await sut.ExecuteWithCache(CancellationToken.None, key, () =>
			{
				actualTask = sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(notExpected), TimeSpan.Zero, attributes);
				return Task.FromResult(expected);
			}, TimeSpan.Zero, attributes);
			var actual = await actualTask;

			//assert
			actual.Should().Be(expected);
		}
Esempio n. 11
0
		public async Task ExecuteWithCache_WhenValueIsCachedAndAgeEntryIsTooOldWithAttributes_ShouldAddNewEntry(
			[Frozen] Mock<ICacheEntryRepository> cacheEntryRepo,
			CacheService sut,
			string key,
			TestCacheReturnValue expected,
			TestCacheReturnValue notExpected,
			DateTimeOffset now,
			Guid id,
			Dictionary<string, string> attributes)
		{
			//arrange
			cacheEntryRepo.Setup(c => c.Get<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, attributes))
			              .ReturnsTask(new CacheEntry<TestCacheReturnValue>(
				              id,
				              key,
				              new Dictionary<string, string>(),
				              now.Subtract(TimeSpan.FromMinutes(10)),
				              notExpected));
			cacheEntryRepo.Setup(c => c.AddOrUpdate(CancellationToken.None, It.Is<CacheEntry<TestCacheReturnValue>>(entry =>
				entry.CacheKey == key &&
				entry.Value == expected &&
				entry.Attributes == attributes)))
			              .ReturnsDefaultTask()
			              .Verifiable();

			//act
			await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.FromMinutes(5), attributes);

			//assert
			cacheEntryRepo.Verify();
		}
Esempio n. 12
0
		public async Task ExecuteWithCache_WhenValueIsCachedWithAttributes_ShouldReturnCorrectValue(
			[Frozen] Mock<ICacheEntryRepository> cacheEntryRepo,
			CacheService sut,
			string key,
			TestCacheReturnValue expected,
			TestCacheReturnValue notExpected,
			DateTimeOffset now,
			Dictionary<string, string> attributes)
		{
			//arrange
			cacheEntryRepo.Setup(c => c.Get<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, attributes))
			              .ReturnsTask(new CacheEntry<TestCacheReturnValue>(Guid.NewGuid(), key, attributes, now, expected));

			//act
			var actual = await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(notExpected), TimeSpan.Zero, attributes);

			//assert
			actual.Should().Be(expected);
		}
Esempio n. 13
0
		public async Task ExecuteWithCache_WhenValueIsNotCachedWithAttributes_ShouldAddEntryToCache(
			[Frozen] Mock<ICacheEntryRepository> cacheEntryRepo,
			CacheService sut,
			string key,
			TestCacheReturnValue expected,
			Dictionary<string, string> attributes)
		{
			//arrange
			cacheEntryRepo.Setup(c => c.Get<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, attributes))
			              .ReturnsTask(default(CacheEntry<TestCacheReturnValue>));
			cacheEntryRepo.Setup(c => c.AddOrUpdate(CancellationToken.None, It.Is<CacheEntry<TestCacheReturnValue>>(entry =>
				entry.CacheKey == key && entry.Value == expected && entry.Attributes == attributes)))
			              .ReturnsDefaultTask()
			              .Verifiable();

			//act
			await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.Zero, attributes);

			//assert
			cacheEntryRepo.Verify();
		}
Esempio n. 14
0
		public async Task ExecuteWithCache_WhenValueIsCachedAndAgeEntryIsTooOld_ShouldRemoveOldEntry(
			[Frozen] Mock<ICacheEntryRepository> cacheEntryRepo,
			CacheService sut,
			string key,
			TestCacheReturnValue expected,
			TestCacheReturnValue notExpected,
			DateTimeOffset now,
			Guid id)
		{
			//arrange
			cacheEntryRepo.Setup(c => c.Get<TestCacheReturnValue>(It.IsAny<CancellationToken>(), key, It.Is<IDictionary<string, string>>(d => d.Count == 0)))
			              .ReturnsTask(new CacheEntry<TestCacheReturnValue>(
				              id,
				              key,
				              new Dictionary<string, string>(),
				              now.Subtract(TimeSpan.FromMinutes(10)),
				              notExpected));

			//act
			await sut.ExecuteWithCache(CancellationToken.None, key, () => Task.FromResult(expected), TimeSpan.FromMinutes(5));

			//assert
			cacheEntryRepo.Verify(c => c.Remove<TestCacheReturnValue>(CancellationToken.None, key, id));
		}