示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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();
        }
示例#4
0
        /// <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;
        }
示例#5
0
        /// <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;
        }
示例#6
0
        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();
        }
示例#7
0
        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();
        }
示例#8
0
        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();
        }