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_RefreshConfigAsync_ConfigCahged_ShouldRaiseEvent() { // Arrange byte eventChanged = 0; 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)); var service = new AutoPollConfigService( fetcherMock.Object, cacheMock.Object, TimeSpan.FromMinutes(1), TimeSpan.Zero, logFactoryMock.Object, false); service.OnConfigurationChanged += (o, s) => { eventChanged++; }; // Act await service.RefreshConfigAsync(); // Assert Assert.AreEqual(1, eventChanged); }
public async Task AutoPollConfigService_RefreshConfigAsync_ShouldNotInvokeCacheGetAndFetchAndCacheSet() { // Arrange 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)); var service = new AutoPollConfigService( fetcherMock.Object, cacheMock.Object, TimeSpan.FromMinutes(1), TimeSpan.Zero, logFactoryMock.Object, false); // Act await service.RefreshConfigAsync(); // Assert this.cacheMock.Verify(m => m.Get(), Times.Once); this.cacheMock.Verify(m => m.Set(fetchedPc), Times.Once); this.fetcherMock.Verify(m => m.Fetch(cachedPc), Times.Once); }
/// <summary> /// Create an instance of ConfigCatClient and setup AutoPoll mode /// </summary> /// <param name="configuration">Configuration for AutoPolling mode</param> /// <exception cref="ArgumentException">When the configuration contains any invalid property</exception> /// <exception cref="ArgumentNullException">When the configuration is null</exception> public ConfigCatClient(AutoPollConfiguration configuration) : this((ConfigurationBase)configuration) { var autoPollService = new AutoPollConfigService( new HttpConfigFetcher(configuration.CreateUri(), "a-" + version, configuration.Logger, configuration.HttpClientHandler, this.configDeserializer, configuration.IsCustomBaseUrl), this.cacheParameters, TimeSpan.FromSeconds(configuration.PollIntervalSeconds), TimeSpan.FromSeconds(configuration.MaxInitWaitTimeSeconds), configuration.Logger ); autoPollService.OnConfigurationChanged += configuration.RaiseOnConfigurationChanged; this.configService = autoPollService; }
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); }
/// <summary> /// Create an instance of BetterConfigClient and setup AutoPoll mode /// </summary> /// <param name="configuration">Configuration for AutoPolling mode</param> /// <exception cref="ArgumentException">When the configuration contains any invalid property</exception> /// <exception cref="ArgumentNullException">When the configuration is null</exception> public BetterConfigClient(AutoPollConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } configuration.Validate(); InitializeClient(configuration); var configService = new AutoPollConfigService( new HttpConfigFetcher(configuration.Url, "a-" + version, configuration.LoggerFactory), new InMemoryConfigCache(), TimeSpan.FromSeconds(configuration.PollIntervalSeconds), TimeSpan.FromSeconds(configuration.MaxInitWaitTimeSeconds), configuration.LoggerFactory); configService.OnConfigurationChanged += (sender, args) => { this.OnConfigurationChanged?.Invoke(sender, args); }; this.configService = configService; }
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); }
public async Task AutoPollConfigService_RefreshConfigAsync_ConfigChanged_ShouldRaiseEvent() { // Arrange byte eventChanged = 0; 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)) .Returns(Task.CompletedTask); var service = new AutoPollConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, TimeSpan.FromMinutes(1), TimeSpan.Zero, loggerMock.Object, false); service.OnConfigurationChanged += (o, s) => { eventChanged++; }; // Act await service.RefreshConfigAsync(); // Assert Assert.AreEqual(1, eventChanged); }
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_RefreshConfigAsync_ShouldOnceInvokeCacheGetAndFetchAndCacheSet() { // Arrange 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)) .Returns(Task.CompletedTask); var service = new AutoPollConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, TimeSpan.FromMinutes(1), TimeSpan.Zero, loggerMock.Object, false); // Act await service.RefreshConfigAsync(); // 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.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); }