Esempio n. 1
0
        public void ShouldNotOverwriteNonOptionalConfigIfDoesNotExistOnReload()
        {
            const string key          = "Key";
            const string value        = "Test";
            var          originalData = new Dictionary <string, string> {
                { key, value }
            };
            Action <object> onChangeAction = null;

            _consulConfigSourceMock.SetupGet(ccs => ccs.Optional).Returns(false);
            _consulConfigSourceMock.SetupGet(ccs => ccs.ReloadOnChange).Returns(true);
            _configQueryResultMock.SetupGet(cqr => cqr.Exists).Returns(false);
            _changeTokenMock
            .Setup(ct => ct.RegisterChangeCallback(It.IsAny <Action <object> >(), It.IsAny <object>()))
            .Callback((Action <object> action, object state) =>
            {
                onChangeAction = action;
            })
            .Returns(null as IDisposable);

            var configProvider = new ConsulConfigurationProvider(_consulConfigSourceMock.Object, _consulConfigClientMock.Object);

            DoLoad(originalData);

            onChangeAction(null);

            string actualValue;

            Assert.That(configProvider.TryGet(key, out actualValue), Is.True);
        }
Esempio n. 2
0
        public void SetUp()
        {
            _configParserMock       = new Mock <IConfigurationParser>(MockBehavior.Strict);
            _consulConfigSourceMock = new Mock <IConsulConfigurationSource>(MockBehavior.Strict);
            _consulConfigSourceMock.SetupGet(ccs => ccs.Parser).Returns(_configParserMock.Object);
            _consulConfigSourceMock.SetupGet(ccs => ccs.Key).Returns(_Key);
            _consulConfigSourceMock.SetupGet(ccs => ccs.Optional).Returns(false);
            _consulConfigSourceMock.SetupGet(ccs => ccs.ReloadOnChange).Returns(false);
            _consulConfigSourceMock.SetupGet(ccs => ccs.OnLoadException).Returns(null);
            _consulConfigSourceMock.SetupGet(ccs => ccs.OnWatchException).Returns(null);

            _changeTokenMock       = new Mock <IChangeToken>(MockBehavior.Strict);
            _configQueryResultMock = new Mock <IConfigQueryResult>(MockBehavior.Strict);

            _consulConfigClientMock = new Mock <IConsulConfigurationClient>(MockBehavior.Strict);
            _consulConfigClientMock
            .Setup(ccc => ccc.GetConfig())
            .ReturnsAsync(_configQueryResultMock.Object);
            _consulConfigClientMock
            .Setup(ccc => ccc.Watch(_consulConfigSourceMock.Object.OnWatchException))
            .Returns(_changeTokenMock.Object);

            _consulConfigProvider =
                new ConsulConfigurationProvider(_consulConfigSourceMock.Object, _consulConfigClientMock.Object);
        }
Esempio n. 3
0
            public Load()
            {
                _consulConfigSourceMock.Setup(ccs => ccs.Parser).Returns(_configParserMock.Object);
                _consulConfigSourceMock.Setup(ccs => ccs.ReloadOnChange).Returns(false);

                _consulConfigProvider = new ConsulConfigurationProvider(
                    _consulConfigSourceMock.Object,
                    _consulConfigClientMock.Object);
            }
            public Load()
            {
                _source = new ConsulConfigurationSource("Test", default(CancellationToken))
                {
                    Parser         = _configParserMock.Object,
                    ReloadOnChange = false
                };

                _consulConfigProvider = new ConsulConfigurationProvider(_source, _consulConfigClientMock.Object);
            }
Esempio n. 5
0
        public void ShouldWatchForChangesIfSourceReloadOnChangesIsTrue()
        {
            _consulConfigSourceMock.SetupGet(ccs => ccs.ReloadOnChange).Returns(true);
            _changeTokenMock
            .Setup(ct => ct.RegisterChangeCallback(It.IsAny <Action <object> >(), It.IsAny <object>()))
            .Returns(null as IDisposable);

            var configProvider =
                new ConsulConfigurationProvider(_consulConfigSourceMock.Object, _consulConfigClientMock.Object);

            Assert.That(
                () => _consulConfigClientMock.Verify(ccs => ccs.Watch(_consulConfigSourceMock.Object.OnWatchException)),
                Throws.Nothing);
        }
Esempio n. 6
0
            public Reload()
            {
                _firstChangeToken = new ConfigurationReloadToken();
                _consulConfigClientMock
                .SetupSequence(ccc => ccc.Watch(It.IsAny <Action <ConsulWatchExceptionContext> >()))
                .Returns(_firstChangeToken)
                .Returns(new ConfigurationReloadToken());
                _consulConfigSourceMock.Setup(ccs => ccs.OnWatchException).Returns(context => { });
                _consulConfigSourceMock.Setup(ccs => ccs.Parser).Returns(_configParserMock.Object);
                _consulConfigSourceMock.Setup(ccs => ccs.ReloadOnChange).Returns(true);

                _consulConfigProvider = new ConsulConfigurationProvider(
                    _consulConfigSourceMock.Object,
                    _consulConfigClientMock.Object);
            }
            public Reload()
            {
                _source = new ConsulConfigurationSource("Test", default(CancellationToken))
                {
                    Parser         = _configParserMock.Object,
                    ReloadOnChange = true
                };
                _firstChangeToken = new ConfigurationReloadToken();
                _consulConfigClientMock
                .SetupSequence(ccc => ccc.Watch("Test", It.IsAny <Action <ConsulWatchExceptionContext> >(), default(CancellationToken)))
                .Returns(_firstChangeToken)
                .Returns(new ConfigurationReloadToken());

                _consulConfigProvider = new ConsulConfigurationProvider(
                    _source,
                    _consulConfigClientMock.Object);
            }
Esempio n. 8
0
        public void ShouldNotThrowIfDoesNotExistOnReload(bool optional)
        {
            Action <object> onChangeAction = null;

            _consulConfigSourceMock.SetupGet(ccs => ccs.Optional).Returns(optional);
            _consulConfigSourceMock.SetupGet(ccs => ccs.ReloadOnChange).Returns(true);
            _configQueryResultMock.SetupGet(cqr => cqr.Exists).Returns(false);
            _changeTokenMock
            .Setup(ct => ct.RegisterChangeCallback(It.IsAny <Action <object> >(), It.IsAny <object>()))
            .Callback((Action <object> action, object state) =>
            {
                onChangeAction = action;
            })
            .Returns(null as IDisposable);

            var configProvider =
                new ConsulConfigurationProvider(_consulConfigSourceMock.Object, _consulConfigClientMock.Object);

            Assert.That(() => onChangeAction(null), Throws.Nothing);
        }
Esempio n. 9
0
        public void ShouldReloadConfigIfReloadOnChangesAndDataInConsulHasChanged()
        {
            Action <object> onChangeAction = null;

            _consulConfigSourceMock.SetupGet(ccs => ccs.ReloadOnChange).Returns(true);
            _configQueryResultMock.SetupGet(cqr => cqr.Exists).Returns(false);
            _changeTokenMock
            .Setup(ct => ct.RegisterChangeCallback(It.IsAny <Action <object> >(), It.IsAny <object>()))
            .Callback((Action <object> action, object state) =>
            {
                onChangeAction = action;
            })
            .Returns(null as IDisposable);

            var configProvider =
                new ConsulConfigurationProvider(_consulConfigSourceMock.Object, _consulConfigClientMock.Object);

            onChangeAction(null);

            Assert.That(() => _consulConfigClientMock.Verify(ccc => ccc.GetConfig()), Throws.Nothing);
        }
        public ConsulConfigurationProviderTests()
        {
            _kvEndpoint = new Mock <IKVEndpoint>(MockBehavior.Strict);
            var consulClient = new Mock <IConsulClient>(MockBehavior.Strict);

            consulClient
            .Setup(cc => cc.KV)
            .Returns(_kvEndpoint.Object);
            consulClient
            .Setup(cc => cc.Dispose());
            var consulClientFactory = new Mock <IConsulClientFactory>(MockBehavior.Strict);

            consulClientFactory
            .Setup(ccf => ccf.Create())
            .Returns(consulClient.Object);
            _parser = new Mock <IConfigurationParser>(MockBehavior.Strict);
            _source = new ConsulConfigurationSource("Test")
            {
                Parser = _parser.Object
            };
            _provider = new ConsulConfigurationProvider(
                _source,
                consulClientFactory.Object);
        }