public async Task LazyLoadConfigService_GetConfigAsync_ReturnsNotExpiredContent_ShouldNotInvokeFetchAndCacheSet() { // Arrange var cachedPc = new ProjectConfig(null, DateTime.UtcNow, null); this.cacheMock .Setup(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None)) .ReturnsAsync(cachedPc); var service = new LazyLoadConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, loggerMock.Object, defaultExpire); // Act var projectConfig = await service.GetConfigAsync(); // Assert Assert.AreEqual(cachedPc, projectConfig); this.fetcherMock.Verify(m => m.Fetch(It.IsAny <ProjectConfig>()), Times.Never); this.cacheMock.Verify(m => m.SetAsync(It.IsAny <string>(), It.IsAny <ProjectConfig>()), Times.Never); }
public async Task LazyLoadConfigService_GetConfigAsync_ReturnsNotExpiredContent_ShouldNotInvokeFetchAndCacheSet() { // Arrange var cachedPc = new ProjectConfig(null, DateTime.UtcNow, null); this.cacheMock .Setup(m => m.Get()) .Returns(cachedPc); var service = new LazyLoadConfigService( fetcherMock.Object, cacheMock.Object, logFactoryMock.Object, defaultExpire); // Act var projectConfig = await service.GetConfigAsync(); // Assert Assert.AreEqual(cachedPc, projectConfig); this.fetcherMock.Verify(m => m.Fetch(It.IsAny <ProjectConfig>()), Times.Never); this.cacheMock.Verify(m => m.Set(It.IsAny <ProjectConfig>()), Times.Never); }
public async Task LazyLoadConfigService_GetConfigAsync_ReturnsExpiredContent_ShouldInvokeFetchAndCacheSet() { // Arrange this.cacheMock .Setup(m => m.Get()) .Returns(cachedPc); this.cacheMock .Setup(m => m.Set(fetchedPc)) .Verifiable(); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Returns(Task.FromResult(fetchedPc)) .Verifiable(); var service = new LazyLoadConfigService( fetcherMock.Object, cacheMock.Object, logFactoryMock.Object, defaultExpire); // Act var projectConfig = await service.GetConfigAsync(); // Assert Assert.AreEqual(fetchedPc, projectConfig); this.fetcherMock.VerifyAll(); this.cacheMock.VerifyAll(); }
/// <summary> /// Create an instance of ConfigCatClient and setup LazyLoad mode /// </summary> /// <param name="configuration">Configuration for LazyLoading mode</param> /// <exception cref="ArgumentException">When the configuration contains any invalid property</exception> /// <exception cref="ArgumentNullException">When the configuration is null</exception> public ConfigCatClient(LazyLoadConfiguration configuration) : this((ConfigurationBase)configuration) { var lazyLoadService = new LazyLoadConfigService( new HttpConfigFetcher(configuration.CreateUri(), "l-" + version, configuration.Logger, configuration.HttpClientHandler, this.configDeserializer, configuration.IsCustomBaseUrl), this.cacheParameters, configuration.Logger, TimeSpan.FromSeconds(configuration.CacheTimeToLiveSeconds)); this.configService = lazyLoadService; }
/// <summary> /// Create an instance of BetterConfigClient and setup LazyLoad mode /// </summary> /// <param name="configuration">Configuration for LazyLoading mode</param> /// <exception cref="ArgumentException">When the configuration contains any invalid property</exception> /// <exception cref="ArgumentNullException">When the configuration is null</exception> public BetterConfigClient(LazyLoadConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } configuration.Validate(); InitializeClient(configuration); var configService = new LazyLoadConfigService( new HttpConfigFetcher(configuration.Url, "l-" + version, configuration.LoggerFactory), new InMemoryConfigCache(), configuration.LoggerFactory, TimeSpan.FromSeconds(configuration.CacheTimeToLiveSeconds)); this.configService = configService; }
public async Task LazyLoadConfigService_RefreshConfigAsync_ShouldNotInvokeCacheGetAndFetchAndCacheSet() { // Arrange byte callOrder = 1; this.cacheMock .Setup(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None)) .ReturnsAsync(cachedPc) .Callback(() => Assert.AreEqual(1, callOrder++)) .Verifiable(); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Returns(Task.FromResult(fetchedPc)) .Callback(() => Assert.AreEqual(2, callOrder++)) .Verifiable(); this.cacheMock .Setup(m => m.SetAsync(It.IsAny <string>(), fetchedPc)) .Returns(Task.CompletedTask) .Callback(() => Assert.AreEqual(3, callOrder)) .Verifiable(); var service = new LazyLoadConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, loggerMock.Object, defaultExpire); // Act await service.RefreshConfigAsync(); // Assert this.fetcherMock.VerifyAll(); this.cacheMock.VerifyAll(); }
public async Task LazyLoadConfigService_RefreshConfigAsync_ShouldNotInvokeCacheGetAndFetchAndCacheSet() { // Arrange byte callOrder = 1; this.cacheMock .Setup(m => m.Get()) .Returns(cachedPc) .Callback(() => Assert.AreEqual(1, callOrder++)) .Verifiable(); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Returns(Task.FromResult(fetchedPc)) .Callback(() => Assert.AreEqual(2, callOrder++)) .Verifiable(); this.cacheMock .Setup(m => m.Set(fetchedPc)) .Callback(() => Assert.AreEqual(3, callOrder)) .Verifiable(); var service = new LazyLoadConfigService( fetcherMock.Object, cacheMock.Object, logFactoryMock.Object, defaultExpire); // Act await service.RefreshConfigAsync(); // Assert this.fetcherMock.VerifyAll(); this.cacheMock.VerifyAll(); }
public async Task LazyLoadConfigService_GetConfigAsync_ReturnsExpiredContent_ShouldInvokeFetchAndCacheSet() { // Arrange this.cacheMock .Setup(m => m.GetAsync(It.IsAny <string>(), CancellationToken.None)) .ReturnsAsync(cachedPc); this.cacheMock .Setup(m => m.SetAsync(It.IsAny <string>(), fetchedPc)) .Returns(Task.CompletedTask) .Verifiable(); this.fetcherMock .Setup(m => m.Fetch(cachedPc)) .Returns(Task.FromResult(fetchedPc)) .Verifiable(); var service = new LazyLoadConfigService( fetcherMock.Object, new CacheParameters { ConfigCache = cacheMock.Object }, loggerMock.Object, defaultExpire); // Act var projectConfig = await service.GetConfigAsync(); // Assert Assert.AreEqual(fetchedPc, projectConfig); this.fetcherMock.VerifyAll(); this.cacheMock.VerifyAll(); }