public async Task AutoPollConfigService_GetConfigAsync_WithTimer_ShouldInvokeFetchAndCacheSetAndCacheGet2x() { // Arrange var wd = new ManualResetEventSlim(false); this.cacheMock .Setup(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None)) .ReturnsAsync(cachedPc); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Returns(Task.FromResult(fetchedPc)); this.cacheMock .Setup(m => m.SetAsync(It.IsAny <string>(), fetchedPc)) .Callback(() => wd.Set()) .Returns(Task.FromResult(0)); var service = new AutoPollConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, TimeSpan.FromMinutes(1), TimeSpan.Zero, loggerMock.Object, true); // Act wd.Wait(TimeSpan.FromMinutes(1)); await service.GetConfigAsync(); service.Dispose(); // Assert this.cacheMock.Verify(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None), Times.Exactly(2)); this.cacheMock.Verify(m => m.SetAsync(It.IsAny <string>(), fetchedPc), Times.Once); this.fetcherMock.Verify(m => m.Fetch(cachedPc), Times.Once); }
public async Task AutoPollConfigService_GetConfigAsync_WithoutTimerWithCachedConfig_ShouldInvokeCacheGet1xAndSetNeverFetchNever() { // Arrange var localPc = cachedPc; this.cacheMock .Setup(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None)) .ReturnsAsync(localPc); this.fetcherMock .Setup(m => m.Fetch(localPc)) .Returns(Task.FromResult(fetchedPc)); this.cacheMock .Setup(m => m.SetAsync(It.IsAny <string>(), fetchedPc)) .Callback(() => localPc = fetchedPc) .Returns(Task.CompletedTask); var service = new AutoPollConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1), loggerMock.Object, false); // Act await service.GetConfigAsync(); // Assert this.cacheMock.Verify(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None), Times.Once); this.cacheMock.Verify(m => m.SetAsync(It.IsAny <string>(), fetchedPc), Times.Never); this.fetcherMock.Verify(m => m.Fetch(cachedPc), Times.Never); }
public async Task AutoPollConfigService_GetConfigAsync_WithTimer_ShouldInvokeFetchAndCacheSetAndCacheGet2x() { // Arrange var wd = new ManualResetEventSlim(false); this.cacheMock .Setup(m => m.Get()) .Returns(cachedPc); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Returns(Task.FromResult(fetchedPc)); this.cacheMock .Setup(m => m.Set(fetchedPc)) .Callback(() => wd.Set()); var service = new AutoPollConfigService( fetcherMock.Object, cacheMock.Object, TimeSpan.FromMinutes(1), TimeSpan.Zero, logFactoryMock.Object, true); // Act wd.Wait(TimeSpan.FromMinutes(1)); await service.GetConfigAsync(); // Assert this.cacheMock.Verify(m => m.Get(), Times.Exactly(2)); this.cacheMock.Verify(m => m.Set(fetchedPc), Times.Once); this.fetcherMock.Verify(m => m.Fetch(cachedPc), Times.Once); }
public async Task AutoPollConfigService_GetConfigAsync_WithoutTimer_ShouldInvokeFetchAndCacheSetAndCacheGet2x() { // Arrange var localPc = cachedPc; this.cacheMock .Setup(m => m.Get()) .Returns(localPc); this.fetcherMock .Setup(m => m.Fetch(localPc)) .Returns(Task.FromResult(fetchedPc)); this.cacheMock .Setup(m => m.Set(fetchedPc)) .Callback(() => localPc = fetchedPc); var service = new AutoPollConfigService( fetcherMock.Object, cacheMock.Object, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1), logFactoryMock.Object, false); // Act await service.GetConfigAsync(); // Assert this.cacheMock.Verify(m => m.Get(), Times.Exactly(2)); this.cacheMock.Verify(m => m.Set(fetchedPc), Times.Once); this.fetcherMock.Verify(m => m.Fetch(cachedPc), Times.Once); }