Exemplo n.º 1
0
        public void RetryPolicy_WhenException_Retry(bool isPositive, bool expectedException, int retriesConfiguration, int expectedExecutions)
        {
            var       policyRegistry = new StrategyBuilder();
            var       timesExecuted  = 0;
            Exception exception      = null;

            var strategy = new RetryStrategy
            {
                RetryCount = retriesConfiguration,
                OnRetry    = (ex) => exception = ex
            };

            var policy          = policyRegistry.Add <Exception>(strategy).Build();
            var executionResult = policy.Execute(() =>
            {
                timesExecuted++;
                var num = GetNumber(isPositive);
                if (num <= 0)
                {
                    throw new Exception();
                }
                return(num);
            });

            Assert.Equal(expectedExecutions, timesExecuted);
            Assert.Equal(expectedException, executionResult.Exception != null);
            Assert.Equal(expectedException, exception != null);
        }
Exemplo n.º 2
0
        public void CircutBreakerPolicy_WhenException_Break(bool isPositive, int executionTimes, int retriesConfiguration, int expectedExecutions)
        {
            var policyRegistry = new StrategyBuilder();
            var timesExecuted  = 0;

            var strategy = new CircutBreakerStrategy
            {
                AllowedExceptionsCount = retriesConfiguration,
                CooldownTimeout        = TimeSpan.FromMilliseconds(100),
            };

            var handler = policyRegistry.Add <Exception>(strategy).Build();

            for (int i = 0; i < executionTimes; i++)
            {
                var executionResult = handler.Execute(() =>
                {
                    timesExecuted++;
                    var num = GetNumber(isPositive);
                    if (num <= 0)
                    {
                        throw new Exception();
                    }
                    return(num);
                });
            }
            Assert.Equal(expectedExecutions, timesExecuted);

            Thread.Sleep(100);

            var executionResult2 = handler.Execute(() =>
            {
                timesExecuted++;
                var num = GetNumber(isPositive);
                if (num <= 0)
                {
                    throw new Exception();
                }
                return(num);
            });

            Assert.Equal(++expectedExecutions, timesExecuted);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Called when process is starting
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task StartAsync(CancellationToken cancellationToken)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;

            string environment = null;

            FileConfigurationProvider ProviderForFile(string name) => new FileConfigurationProvider
            {
                File           = name,
                Path           = environment.IsNullOrEmpty() ? Path.Combine(AppContext.BaseDirectory, $"configs") : Path.Combine(AppContext.BaseDirectory, $"configs", environment),
                ReloadOnAccess = false
            };

            // set active configuration
            EnvironmentConfiguration.UseProvider(ProviderForFile("environment.config.json"));
            environment = EnvironmentConfiguration.Instance.Environment;

            // load custom configurations from file

            // Signals core application configuration
            ApplicationConfiguration.UseProvider(ProviderForFile("application.config.json"));
            // Signals core background application configuration
            BackgroundApplicationConfiguration.UseProvider(ProviderForFile("background.application.config.json"));
            // Application custom domain configuration
            DomainConfiguration.UseProvider(ProviderForFile("domain.config.json"));

            // use general exception handling strategy
            var strategyBuilder = new StrategyBuilder();

            strategyBuilder.Add <Exception>(new RetryStrategy {
                RetryCount = 3, RetryCooldown = TimeSpan.FromMinutes(5)
            }).SetAutoHandling(false);

            // configure Signals aspects
            var config = new BackgroundApplicationBootstrapConfiguration
            {
                // configure dependency injection
                RegistrationService = new RegistrationService(),
                // recurring task registry
                TaskRegistry = new FluentRegistry(),
                // configure caching
                CacheConfiguration = new InMemoryCacheConfiguration
                {
                    DataProvider     = new InMemoryDataProvider(),
                    ExpirationPolicy = CacheExpirationPolicy.Sliding,
                    ExpirationTime   = TimeSpan.FromMinutes(1)
                },
                // configure logging in database
                LoggerConfiguration = new DatabaseLoggingConfiguration
                {
                    Database     = DomainConfiguration.Instance.DatabaseConfiguration.ActiveConfiguration.Database,
                    Host         = DomainConfiguration.Instance.DatabaseConfiguration.ActiveConfiguration.IpAddress,
                    Username     = DomainConfiguration.Instance.DatabaseConfiguration.ActiveConfiguration.Uid,
                    Password     = DomainConfiguration.Instance.DatabaseConfiguration.ActiveConfiguration.Pwd,
                    DataProvider = DataProvider.SqlClient,
                    TableName    = "LogEntity"
                },
                // configure localization from json files
                LocalizationConfiguration = new JsonDataProviderConfiguration
                {
                    DirectoryPath       = Path.Combine(AppContext.BaseDirectory, "system.resources"),
                    FileExtension       = "app",
                    LocalizationSources = new List <LocalizationSource>
                    {
                        new LocalizationSource
                        {
                            Name       = "Mail messages",
                            SourcePath = "mailmessages"
                        },
                        new LocalizationSource
                        {
                            Name       = "Validation rules",
                            SourcePath = "validationrules"
                        },
                        new LocalizationSource
                        {
                            Name       = "Pages",
                            SourcePath = "pages"
                        },
                        new LocalizationSource
                        {
                            Name       = "Processes",
                            SourcePath = "processes"
                        }
                    }
                },
                // configure pubsub with MSSQL broker
                ChannelConfiguration = new MsSqlChannelConfiguration
                {
                    // make sure broker is enabled in the database
                    // ex: ALTER DATABASE [acme.db] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
                    ConnectionString         = DomainConfiguration.Instance.DatabaseConfiguration.ActiveConfiguration.ConnectionString,
                    DbTableName              = DomainConfiguration.Instance.NotificationConfiguration.DbTableName,
                    MessageListeningStrategy = MessageListeningStrategy.Broker
                },
                StrategyBuilder = new StrategyBuilder().SetAutoHandling(false),
            };

            // set default serialization settings
            config.JsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
            config.JsonSerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

            // bootstrap the configuration from the references assemblies
            var assemblies = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "App.*.dll").Select(file => Assembly.LoadFrom(file)).ToArray();

            config.Bootstrap(assemblies);

            // set current culture and UI culture
            Thread.CurrentThread.CurrentCulture   = new CultureInfo(DomainConfiguration.Instance.LocalizationConfiguration.DefaultCulture);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            return(Task.CompletedTask);
        }