Пример #1
0
        public async Task CachedSecretProvider_GetTwoSecretsAsync_SkippedCache_ShouldReturnDifferentValues()
        {
            // Arrange
            string expectedFirstSecret  = Guid.NewGuid().ToString("N");
            string expectedSecondSecret = Guid.NewGuid().ToString("N");

            var      spyTestProvider = new TestSecretProviderStub(expectedFirstSecret);
            TimeSpan cacheInterval   = TimeSpan.FromSeconds(3);

            const string          keyName = "MyValue";
            ICachedSecretProvider cachedSecretProvider = new CachedSecretProvider(
                spyTestProvider,
                new CacheConfiguration(cacheInterval),
                new MemoryCache(new MemoryCacheOptions()));

            // Act
            Secret actualFirst = await cachedSecretProvider.GetSecretAsync(keyName, ignoreCache : true);

            await Task.Delay(TimeSpan.FromMilliseconds(100));

            spyTestProvider.SecretValue = expectedSecondSecret;
            Secret actualSecond = await cachedSecretProvider.GetSecretAsync(keyName, ignoreCache : true);

            // Assert
            Assert.True(actualFirst != null, "actualFirst != null");
            Assert.True(actualSecond != null, "actualSecond != null");

            Assert.Equal(expectedFirstSecret, actualFirst.Value);
            Assert.Equal(expectedSecondSecret, actualSecond.Value);
            Assert.Equal(2, spyTestProvider.CallsMadeSinceCreation);
        }
Пример #2
0
        public async Task CacheSecretProvider_GetSecretAsync_InvalidateSecret_ShouldRequestNewSecretEvenWithinCacheDuration()
        {
            // Arrange
            string expectedFirstSecret  = Guid.NewGuid().ToString("N");
            string expectedSecondSecret = Guid.NewGuid().ToString("N");
            var    testProviderStub     = new TestSecretProviderStub(expectedFirstSecret);
            var    cacheInterval        = TimeSpan.FromSeconds(10);

            const string keyName = "MySecret";
            var          cachedSecretProvider = new CachedSecretProvider(
                testProviderStub,
                new CacheConfiguration(cacheInterval));

            Secret actualFirst = await cachedSecretProvider.GetSecretAsync(keyName);

            testProviderStub.SecretValue = expectedSecondSecret;

            // Act
            await cachedSecretProvider.InvalidateSecretAsync(keyName);

            // Assert
            Secret actualSecond = await cachedSecretProvider.GetSecretAsync(keyName);

            Assert.True(actualFirst != null, "actualFirst != null");
            Assert.True(actualSecond != null, "actualSecond != null");

            Assert.Equal(expectedFirstSecret, actualFirst.Value);
            Assert.Equal(expectedSecondSecret, actualSecond.Value);
            Assert.Equal(2, testProviderStub.CallsMadeSinceCreation);
        }