public void AutoPollConfigService_WithoutTimer_InvokeDispose_ShouldDisposeService() { // Arrange long counter = -1; long e1; this.cacheMock .Setup(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None)) .ReturnsAsync(cachedPc); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Callback(() => Interlocked.Increment(ref counter)) .Returns(Task.FromResult(cachedPc)); var service = new AutoPollConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, TimeSpan.FromSeconds(0.2d), TimeSpan.Zero, loggerMock.Object, false); // Act Thread.Sleep(TimeSpan.FromSeconds(1)); e1 = Interlocked.Read(ref counter); service.Dispose(); // Assert Assert.AreEqual(-1, e1); }
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 void AutoPollConfigService_Dispose_ShouldStopTimer() { // Arrange long counter = 0; long e1, e2; this.cacheMock .Setup(m => m.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(cachedPc)); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Callback(() => Interlocked.Increment(ref counter)) .Returns(Task.FromResult(cachedPc)); var service = new AutoPollConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object, CacheKey = "" }, TimeSpan.FromSeconds(0.2d), TimeSpan.Zero, loggerMock.Object, false); // Act Thread.Sleep(TimeSpan.FromSeconds(1)); e1 = Interlocked.Read(ref counter); service.Dispose(); // Assert Thread.Sleep(TimeSpan.FromSeconds(2)); e2 = Interlocked.Read(ref counter); Console.WriteLine(e2 - e1); Assert.IsTrue(e2 - e1 <= 1); }