Ejemplo n.º 1
0
        public void ShouldThrowIfConfigDoesNotExistAndIsNotOptonalWhenLoad()
        {
            ConsulLoadExceptionContext actualExceptionContext = null;

            _consulConfigSourceMock.SetupGet(ccs => ccs.Optional).Returns(false);
            _configQueryResultMock.Setup(cqr => cqr.Exists).Returns(false);

            _consulConfigSourceMock
            .SetupGet(ccs => ccs.OnLoadException)
            .Returns(exceptionContext =>
            {
                actualExceptionContext = exceptionContext;
            });

            try
            {
                _consulConfigProvider.Load();
            }
            catch
            {
            }

            Assert.That(
                actualExceptionContext.Exception.Message,
                Is.EqualTo($"The configuration for key {_Key} was not found and is not optional."));
        }
Ejemplo n.º 2
0
        private async Task DoLoad(CancellationToken cancellationToken)
        {
            try
            {
                var result = await GetKvPairs(false, cancellationToken).ConfigureAwait(false);

                if (result.HasValue())
                {
                    SetData(result);
                }
                else if (!_source.Optional)
                {
                    throw new Exception($"The configuration for key {_source.Key} was not found and is not optional.");
                }

                SetLastIndex(result);
            }
            catch (Exception exception)
            {
                var exceptionContext = new ConsulLoadExceptionContext(_source, exception);
                _source.OnLoadException?.Invoke(exceptionContext);
                if (!exceptionContext.Ignore)
                {
                    throw;
                }
            }
        }
Ejemplo n.º 3
0
        private async Task DoLoad(bool reloading)
        {
            try
            {
                QueryResult <KVPair[]> result = await _consulConfigClient
                                                .GetConfig(_source.Key, _source.CancellationToken)
                                                .ConfigureAwait(false);

                if (!result.HasValue() && !_source.Optional)
                {
                    if (!reloading)
                    {
                        throw new Exception(
                                  $"The configuration for key {_source.Key} was not found and is not optional.");
                    }

                    // Don't overwrite mandatory config with empty data if not found when reloading
                    return;
                }

                Data = (result?.Response ?? new KVPair[0])
                       .Where(kvp => kvp.HasValue())
                       .SelectMany(kvp => kvp.ConvertToConfig(_source.Key, _source.Parser))
                       .ToDictionary(kvp => kvp.Key, kvp => kvp.Value, StringComparer.OrdinalIgnoreCase);
            }
            catch (Exception exception)
            {
                var exceptionContext = new ConsulLoadExceptionContext(_source, exception);
                _source.OnLoadException?.Invoke(exceptionContext);
                if (!exceptionContext.Ignore)
                {
                    throw;
                }
            }
        }
        private async Task DoLoad(bool reloading)
        {
            try
            {
                QueryResult <KVPair> queryResult = await _consulConfigClient.GetConfig().ConfigureAwait(false);

                if (!queryResult.HasValue() && !_source.Optional)
                {
                    if (!reloading)
                    {
                        throw new Exception(
                                  $"The configuration for key {_source.Key} was not found and is not optional.");
                    }

                    // Don't overwrite mandatory config with empty data if not found when reloading
                    return;
                }

                Data = ConvertResultToDictionary(queryResult);
            }
            catch (Exception exception)
            {
                var exceptionContext = new ConsulLoadExceptionContext(_source, exception);
                _source.OnLoadException?.Invoke(exceptionContext);
                if (!exceptionContext.Ignore)
                {
                    throw;
                }
            }
        }
Ejemplo n.º 5
0
        private void HandleLoadException(Exception exception)
        {
            var exceptionContext = new ConsulLoadExceptionContext(_source, exception);

            _source.OnLoadException?.Invoke(exceptionContext);
            if (!exceptionContext.Ignore)
            {
                throw exception;
            }
        }
            private void ShouldSetSourceInLoadExceptionContextWhenExceptionDuringLoad()
            {
                ConsulLoadExceptionContext exceptionContext = null;

                _consulConfigClientMock
                .Setup(ccc => ccc.GetConfig("Test", default(CancellationToken)))
                .ThrowsAsync(new Exception());
                _source.OnLoadException = context =>
                {
                    context.Ignore   = true;
                    exceptionContext = context;
                };

                _consulConfigProvider.Load();

                exceptionContext.Source.Should().BeSameAs(_source);
            }
            private void ShouldSetExceptionInLoadExceptionContextWhenExceptionDuringLoad()
            {
                ConsulLoadExceptionContext exceptionContext = null;
                var expectedException = new Exception("Failed to load from Consul agent");

                _consulConfigClientMock
                .Setup(ccc => ccc.GetConfig("Test", default(CancellationToken)))
                .ThrowsAsync(expectedException);
                _source.OnLoadException = context =>
                {
                    context.Ignore   = true;
                    exceptionContext = context;
                };

                _consulConfigProvider.Load();

                exceptionContext.Exception.Should().BeSameAs(expectedException);
            }
Ejemplo n.º 8
0
            private void ShouldSetSourceInLoadExceptionContextWhenExceptionDuringLoad()
            {
                ConsulLoadExceptionContext exceptionContext = null;

                _consulConfigClientMock.Setup(ccc => ccc.GetConfig()).ThrowsAsync(new Exception());
                _consulConfigSourceMock
                .Setup(ccs => ccs.OnLoadException)
                .Returns(
                    context =>
                {
                    context.Ignore   = true;
                    exceptionContext = context;
                });

                _consulConfigProvider.Load();

                exceptionContext.Source.Should().BeSameAs(_consulConfigSourceMock.Object);
            }
Ejemplo n.º 9
0
            private void ShouldSetExceptionInLoadExceptionContextWhenExceptionDuringLoad()
            {
                ConsulLoadExceptionContext exceptionContext = null;
                var expectedException = new Exception("Failed to load from Consul agent");

                _consulConfigClientMock.Setup(ccc => ccc.GetConfig()).ThrowsAsync(expectedException);
                _consulConfigSourceMock
                .Setup(ccs => ccs.OnLoadException)
                .Returns(
                    context =>
                {
                    context.Ignore   = true;
                    exceptionContext = context;
                });

                _consulConfigProvider.Load();

                exceptionContext.Exception.Should().BeSameAs(expectedException);
            }
Ejemplo n.º 10
0
        public void ShouldSetSourceInLoadExceptionContextWhenExceptionDuringLoad()
        {
            ConsulLoadExceptionContext actualExceptionContext = null;

            _consulConfigClientMock.Setup(ccc => ccc.GetConfig()).ThrowsAsync(new Exception());
            _consulConfigSourceMock
            .SetupGet(ccs => ccs.OnLoadException)
            .Returns(exceptionContext =>
            {
                actualExceptionContext = exceptionContext;
            });

            try
            {
                _consulConfigProvider.Load();
            }
            catch {}

            Assert.That(actualExceptionContext.Source, Is.SameAs(_consulConfigSourceMock.Object));
        }
Ejemplo n.º 11
0
        public void ShouldSetExceptionInLoadExceptionContextWhenExceptionDuringLoad()
        {
            ConsulLoadExceptionContext actualExceptionContext = null;
            var expectedException = new Exception("Failed to load from Consul agent");

            _consulConfigClientMock.Setup(ccc => ccc.GetConfig()).ThrowsAsync(expectedException);
            _consulConfigSourceMock
            .SetupGet(ccs => ccs.OnLoadException)
            .Returns(exceptionContext =>
            {
                actualExceptionContext = exceptionContext;
            });

            try
            {
                _consulConfigProvider.Load();
            }
            catch {}

            Assert.That(actualExceptionContext.Exception, Is.SameAs(expectedException));
        }