private void ShouldNotOverwriteNonOptionalConfigIfDoesNotExist()
            {
                _source.Optional = false;
                _consulConfigClientMock
                .SetupSequence(ccc => ccc.GetConfig("Test", default(CancellationToken)))
                .ReturnsAsync(
                    new QueryResult <KVPair[]>
                {
                    Response = new[]
                    {
                        new KVPair("Test")
                        {
                            Value = new List <byte> {
                                1
                            }.ToArray()
                        }
                    },
                    StatusCode = HttpStatusCode.OK
                })
                .ReturnsAsync(new QueryResult <KVPair[]> {
                    StatusCode = HttpStatusCode.NotFound
                });
                _configParserMock
                .Setup(cp => cp.Parse(It.IsAny <MemoryStream>()))
                .Returns(new Dictionary <string, string> {
                    { "Key", "Test" }
                });

                _consulConfigProvider.Load();

                _firstChangeToken.OnReload();

                _consulConfigProvider.TryGet("Key", out string _).Should().BeTrue();
            }
 private async Task PollForChanges(
     string key,
     Action <ConsulWatchExceptionContext> onException,
     CancellationToken cancellationToken)
 {
     while (!cancellationToken.IsCancellationRequested)
     {
         try
         {
             if (await HasValueChanged(key, cancellationToken).ConfigureAwait(false))
             {
                 ConfigurationReloadToken previousToken = Interlocked.Exchange(
                     ref _reloadToken,
                     new ConfigurationReloadToken());
                 previousToken.OnReload();
                 return;
             }
         }
         catch (Exception exception)
         {
             var exceptionContext = new ConsulWatchExceptionContext(cancellationToken, exception);
             onException?.Invoke(exceptionContext);
         }
     }
 }
        protected void OnReload()
        {
            ConfigurationReloadToken previousToken =
                Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());

            previousToken.OnReload();
        }
        private async Task PollForChanges(
            string key,
            Func <ConsulWatchExceptionContext, TimeSpan> onException,
            CancellationToken cancellationToken)
        {
            var consecutiveFailureCount = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    if (await HasValueChanged(key, cancellationToken).ConfigureAwait(false))
                    {
                        ConfigurationReloadToken previousToken = Interlocked.Exchange(
                            ref _reloadToken,
                            new ConfigurationReloadToken());
                        previousToken.OnReload();
                        return;
                    }

                    consecutiveFailureCount = 0;
                }
                catch (Exception exception)
                {
                    TimeSpan wait =
                        onException?.Invoke(
                            new ConsulWatchExceptionContext(cancellationToken, exception, ++consecutiveFailureCount)) ??
                        TimeSpan.FromSeconds(5);
                    await Task.Delay(wait, cancellationToken);
                }
            }
        }
Esempio n. 5
0
        private void HandleConfigurationChange(IConfiguration configuration)
        {
            Set(configuration);
            configuration.GetReloadToken().RegisterChangeCallback(_ => {
                HandleConfigurationChange(configuration);
            }, null);

            _changeToken.OnReload();
            _changeToken = new ConfigurationReloadToken();
        }
Esempio n. 6
0
        private async Task RefreshForChanges(string key, CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                if (await this.IsValueChanged(key, cancellationToken).ConfigureAwait(false))
                {
                    ConfigurationReloadToken previousToken = Interlocked.Exchange(ref this.reloadToken, new ConfigurationReloadToken());
                    previousToken.OnReload();

                    return;
                }
            }
        }
Esempio n. 7
0
        public void IChangeToken_ConfigurationReloadToken_CallCancleAgain()
        {
            var token             = new ConfigurationReloadToken();
            var isCallbackInvoked = false;

            token.RegisterChangeCallback(o => isCallbackInvoked = true, null);
            token.OnReload();
            Wait();
            Assert.True(isCallbackInvoked);
            Assert.True(token.HasChanged);

            isCallbackInvoked = false;
            var isCallbackInvokedAgain = false;

            token.RegisterChangeCallback(o => isCallbackInvokedAgain = true, null);
            Assert.True(token.HasChanged);
            Wait();
            token.OnReload();
            // 还是会调用第二次
            Assert.True(isCallbackInvokedAgain);
            // 新注册的回调还是会调用的
            Assert.True(isCallbackInvokedAgain);
        }
 private void OnConfigurationReload(object obj)
 {
     // Configuration changed;
     _changeToken.OnReload();
 }
        private void RaiseChanged()
        {
            ConfigurationReloadToken previousToken = Interlocked.Exchange(ref _changeToken, new ConfigurationReloadToken());

            previousToken.OnReload();
        }