Пример #1
0
        public void Test_CanGetDefaults()
        {
            ReasonableStakhanoviseDefaultsProvider defaultsProvider =
                new ReasonableStakhanoviseDefaultsProvider();

            StakhanoviseSetupDefaults setupDefaults = defaultsProvider
                                                      .GetDefaults();

            Assert.IsNotNull(setupDefaults);

            Assert.AreEqual(ExpectedDefaultWorkerCount,
                            setupDefaults.WorkerCount);

            AssertCorrectDefaultQueuedTaskMapping(setupDefaults);
            AssertCorrectDefaultExecutingAssemblyList(setupDefaults);
            AssertCorrectDefaultCalculateDelayMillisecondsTaskAfterFailureFn(setupDefaults);
            AssertCorrectDefaultIsTaskErrorRecoverableFn(setupDefaults);

            Assert.AreEqual(ReasonableStakhanoviseDefaultsProvider.DefaultFaultErrorThresholdCount,
                            setupDefaults.FaultErrorThresholdCount);
            Assert.AreEqual(ReasonableStakhanoviseDefaultsProvider.DefaultAppMetricsCollectionIntervalMilliseconds,
                            setupDefaults.AppMetricsCollectionIntervalMilliseconds);

            Assert.IsTrue(setupDefaults.AppMetricsMonitoringEnabled);
            Assert.IsTrue(setupDefaults.SetupBuiltInDbAsssets);
        }
        public void Test_CanRead_FullConfig()
        {
            NetCoreConfigurationStakhanoviseDefaultsProvider provider =
                new NetCoreConfigurationStakhanoviseDefaultsProvider(TestDataDirectory,
                                                                     SampleSettingsFileFull,
                                                                     "Lvd.Stakhanovise.Net.Config");

            StakhanoviseSetupDefaults defaults =
                provider.GetDefaults();

            Assert.NotNull(defaults);

            Assert.AreEqual(12, defaults.WorkerCount);
            Assert.AreEqual(13, defaults.FaultErrorThresholdCount);
            Assert.AreEqual(1234, defaults.AppMetricsCollectionIntervalMilliseconds);
            Assert.AreEqual(true, defaults.AppMetricsMonitoringEnabled);
            Assert.AreEqual(true, defaults.SetupBuiltInDbAsssets);

            AssertExecutorAssembliesMatchTestAssemblies(defaults);

            Assert.NotNull(defaults.CalculateDelayMillisecondsTaskAfterFailure);
            AssertCalculateDelayTicksTaskAfterFailureFnCorrect(defaults,
                                                               expected: (token) => ( long )Math.Ceiling(Math.Exp(token.LastQueuedTaskResult.ErrorCount + 1)),
                                                               numberOfRuns: 100);

            Assert.NotNull(defaults.IsTaskErrorRecoverable);
            AssertIsTaskErrorRecoverableFnCorrect(defaults,
                                                  expected: (task, exc) => !(exc is NullReferenceException) &&
                                                  !(exc is ArgumentException) &&
                                                  !(exc is ApplicationException),
                                                  numberOfRuns: 100);

            AssertConnectionStringCorrect(defaults);
            AssertMappingMatchesNonDefaultTestMapping(defaults);
        }
        public void Test_CanRead_ConnStringOnly()
        {
            ReasonableStakhanoviseDefaultsProvider reasonableDefaultsProvider =
                new ReasonableStakhanoviseDefaultsProvider();

            NetCoreConfigurationStakhanoviseDefaultsProvider provider =
                new NetCoreConfigurationStakhanoviseDefaultsProvider(TestDataDirectory,
                                                                     SampleSettingsFileConnStringOnly,
                                                                     "Lvd.Stakhanovise.Net.Config");

            StakhanoviseSetupDefaults defaults =
                provider.GetDefaults();

            StakhanoviseSetupDefaults reasonableDefaults =
                reasonableDefaultsProvider.GetDefaults();

            Assert.NotNull(defaults);

            AssertDefaultsFromConfigMatchReasonableDefaults(defaults,
                                                            reasonableDefaults);

            AssertExecutorAssembliesMatchReasonableDefaultsAssemblies(defaults,
                                                                      reasonableDefaults);

            AssertConnectionStringCorrect(defaults);

            AssertMappingMatchesReasonableDefaultsMapping(defaults,
                                                          reasonableDefaults);
        }
        private void AssertMappingMatchesNonDefaultTestMapping(StakhanoviseSetupDefaults defaults)
        {
            QueuedTaskMapping nonDefaultTestMapping =
                GetNonDefaultTestMapping();

            AssertMappingsEqual(nonDefaultTestMapping,
                                defaults.Mapping);
        }
 private void AssertExecutorAssembliesMatchReasonableDefaultsAssemblies(StakhanoviseSetupDefaults defaults,
                                                                        StakhanoviseSetupDefaults reasonableDefaults)
 {
     Assert.AreEqual(reasonableDefaults.ExecutorAssemblies.Length,
                     defaults.ExecutorAssemblies.Length);
     CollectionAssert.AreEqual(reasonableDefaults.ExecutorAssemblies,
                               defaults.ExecutorAssemblies);
 }
        private void AssertMappingMatchesReasonableDefaultsMapping(StakhanoviseSetupDefaults defaults,
                                                                   StakhanoviseSetupDefaults reasonableDefaults)
        {
            QueuedTaskMapping defaultMapping =
                reasonableDefaults.Mapping;

            AssertMappingsEqual(defaultMapping,
                                defaults.Mapping);
        }
Пример #7
0
        private void AssertCorrectDefaultIsTaskErrorRecoverableFn(StakhanoviseSetupDefaults setupDefaults)
        {
            IQueuedTask mockTask = MockQueuedTask();

            Assert.NotNull(setupDefaults.IsTaskErrorRecoverable);
            Assert.IsFalse(setupDefaults.IsTaskErrorRecoverable(mockTask, new NullReferenceException()));
            Assert.IsFalse(setupDefaults.IsTaskErrorRecoverable(mockTask, new ArgumentException()));

            Assert.IsTrue(setupDefaults.IsTaskErrorRecoverable(mockTask, new ApplicationException()));
            Assert.IsTrue(setupDefaults.IsTaskErrorRecoverable(mockTask, new Exception()));
            Assert.IsTrue(setupDefaults.IsTaskErrorRecoverable(mockTask, new NotSupportedException()));
        }
        private void AssertIsTascKErrorRecoverableFnCorrectCompareWithExpectedFn(StakhanoviseSetupDefaults defaults,
                                                                                 Func <IQueuedTask, Exception, bool> expected,
                                                                                 int numberOfRuns)
        {
            Faker faker = new Faker();
            Mock <IQueuedTask> taskMock = new Mock <IQueuedTask>();

            for (int i = 0; i < numberOfRuns; i++)
            {
                Exception exc = faker.System.Exception();
                Assert.AreEqual(expected.Invoke(taskMock.Object, exc),
                                defaults.IsTaskErrorRecoverable.Invoke(taskMock.Object, exc));
            }
        }
        private void AssertIsTaskErrorRecoverableFnCorrect(StakhanoviseSetupDefaults defaults,
                                                           Func <IQueuedTask, Exception, bool> expected,
                                                           int numberOfRuns)
        {
            AssertIsTaskErrorRecoverableFnCorrectForNonRecoverableErrors(defaults,
                                                                         numberOfRuns);

            AssertIsTaskErrorRecoverableFnCorrectForRecoverableErrors(defaults,
                                                                      numberOfRuns);

            AssertIsTascKErrorRecoverableFnCorrectCompareWithExpectedFn(defaults,
                                                                        expected,
                                                                        numberOfRuns);
        }
        private void AssertIsTaskErrorRecoverableFnCorrectForRecoverableErrors(StakhanoviseSetupDefaults defaults, int numberOfRuns)
        {
            Faker faker = new Faker();
            Mock <IQueuedTask> taskMock = new Mock <IQueuedTask>();

            for (int i = 0; i < numberOfRuns; i++)
            {
                Assert.IsTrue(defaults.IsTaskErrorRecoverable(taskMock.Object,
                                                              new FileNotFoundException(faker.Lorem.Sentence())));
                Assert.IsTrue(defaults.IsTaskErrorRecoverable(taskMock.Object,
                                                              new FileLoadException(faker.Lorem.Sentence())));
                Assert.IsTrue(defaults.IsTaskErrorRecoverable(taskMock.Object,
                                                              new ArithmeticException(faker.Lorem.Sentence())));
            }
        }
        private void AssertIsTaskErrorRecoverableFnCorrectForNonRecoverableErrors(StakhanoviseSetupDefaults defaults, int numberOfRuns)
        {
            Faker faker = new Faker();
            Mock <IQueuedTask> taskMock = new Mock <IQueuedTask>();

            for (int i = 0; i < numberOfRuns; i++)
            {
                Assert.IsFalse(defaults.IsTaskErrorRecoverable(taskMock.Object,
                                                               new NullReferenceException(faker.Lorem.Sentence())));
                Assert.IsFalse(defaults.IsTaskErrorRecoverable(taskMock.Object,
                                                               new ArgumentException(faker.Lorem.Sentence())));
                Assert.IsFalse(defaults.IsTaskErrorRecoverable(taskMock.Object,
                                                               new ApplicationException(faker.Lorem.Sentence())));
            }
        }
 private void AssertDefaultsFromConfigMatchReasonableDefaults(StakhanoviseSetupDefaults defaults,
                                                              StakhanoviseSetupDefaults reasonableDefaults)
 {
     Assert.AreEqual(reasonableDefaults.WorkerCount,
                     defaults.WorkerCount);
     Assert.AreEqual(reasonableDefaults.CalculateDelayMillisecondsTaskAfterFailure,
                     defaults.CalculateDelayMillisecondsTaskAfterFailure);
     Assert.AreEqual(reasonableDefaults.IsTaskErrorRecoverable,
                     defaults.IsTaskErrorRecoverable);
     Assert.AreEqual(reasonableDefaults.FaultErrorThresholdCount,
                     defaults.FaultErrorThresholdCount);
     Assert.AreEqual(reasonableDefaults.AppMetricsCollectionIntervalMilliseconds,
                     defaults.AppMetricsCollectionIntervalMilliseconds);
     Assert.AreEqual(reasonableDefaults.AppMetricsMonitoringEnabled,
                     defaults.AppMetricsMonitoringEnabled);
     Assert.AreEqual(reasonableDefaults.SetupBuiltInDbAsssets,
                     defaults.SetupBuiltInDbAsssets);
 }
        private void AssertCalculateDelayTicksTaskAfterFailureFnCorrect(StakhanoviseSetupDefaults defaults,
                                                                        Func <IQueuedTaskToken, long> expected,
                                                                        int numberOfRuns)
        {
            for (int i = 0; i < numberOfRuns; i++)
            {
                Mock <IQueuedTaskResult> resultMock = new Mock <IQueuedTaskResult>();
                resultMock.SetupGet(r => r.ErrorCount)
                .Returns(i);

                Mock <IQueuedTaskToken> tokenMock = new Mock <IQueuedTaskToken>();
                tokenMock.SetupGet(t => t.LastQueuedTaskResult)
                .Returns(resultMock.Object);

                long expectedVal = expected
                                   .Invoke(tokenMock.Object);
                long actualVal = defaults.CalculateDelayMillisecondsTaskAfterFailure
                                 .Invoke(tokenMock.Object);

                Assert.AreEqual(expectedVal, actualVal);
            }
        }
Пример #14
0
        public StakhanoviseSetupDefaults GetDefaults()
        {
            IConfiguration config = GetConfig();

            IConfigurationSection section = config
                                            .GetSection(mConfigSectionName);

            StakhanoviseSetupDefaults defaults = mFallbackDefaultsProvider
                                                 .GetDefaults();

            if (section != null)
            {
                StakhanoviseSetupDefaultsConfig defaultsConfig = section.Get <StakhanoviseSetupDefaultsConfig>();
                if (defaultsConfig != null)
                {
                    defaults = MergeDefaultsFromConfig(defaults,
                                                       defaultsConfig,
                                                       config);
                }
            }

            return(defaults);
        }
 private void AssertConnectionStringEmpty(StakhanoviseSetupDefaults defaults)
 {
     Assert.Null(defaults.ConnectionString);
 }
Пример #16
0
 private void AssertCorrectDefaultQueuedTaskMapping(StakhanoviseSetupDefaults setupDefaults)
 {
     Assert.NotNull(setupDefaults.Mapping);
     AssertQueuedTaskMappingsMatch(ExpectedDefaultMapping,
                                   setupDefaults.Mapping);
 }
 private void AssertExecutorAssembliesMatchTestAssemblies(StakhanoviseSetupDefaults defaults)
 {
     Assert.NotNull(defaults.ExecutorAssemblies);
     Assert.AreEqual(1, defaults.ExecutorAssemblies.Length);
     Assert.AreEqual("WinSCPnet.dll", Path.GetFileName(defaults.ExecutorAssemblies[0].Location));
 }
Пример #18
0
 private void AssertCorrectDefaultExecutingAssemblyList(StakhanoviseSetupDefaults setupDefaults)
 {
     Assert.NotNull(setupDefaults.ExecutorAssemblies);
     Assert.AreEqual(1, setupDefaults.ExecutorAssemblies.Length);
     Assert.AreEqual(ExpectedExecutingAssembly, setupDefaults.ExecutorAssemblies[0]);
 }
Пример #19
0
        private void AssertCorrectDefaultCalculateDelayMillisecondsTaskAfterFailureFn(StakhanoviseSetupDefaults setupDefaults)
        {
            Assert.NotNull(setupDefaults.CalculateDelayMillisecondsTaskAfterFailure);
            for (int iErrorCount = 0; iErrorCount < TestCalculateDelayMillisecondsErrorCountMax; iErrorCount++)
            {
                long expectedDelayMilliseconds =
                    ( long )Math.Pow(10, iErrorCount + 1);

                IQueuedTaskToken mockTokenWithErrorCount =
                    MockQueuedTaskTokenWithErrorCount(iErrorCount);

                long actualDelayMilliseconds = setupDefaults
                                               .CalculateDelayMillisecondsTaskAfterFailure(mockTokenWithErrorCount);

                Assert.AreEqual(expectedDelayMilliseconds,
                                actualDelayMilliseconds);
            }
        }
 private void AssertConnectionStringCorrect(StakhanoviseSetupDefaults defaults)
 {
     Assert.NotNull(defaults.ConnectionString);
     Assert.IsNotEmpty(defaults.ConnectionString);
     Assert.AreEqual(TestConnectionString, defaults.ConnectionString);
 }
Пример #21
0
        private StakhanoviseSetupDefaults MergeDefaultsFromConfig(StakhanoviseSetupDefaults targetDefaults,
                                                                  StakhanoviseSetupDefaultsConfig defaultsConfig,
                                                                  IConfiguration config)
        {
            ScriptOptions parseOptions = ConstructParseOptions();

            Assembly[] executorAssemblies = ParseExecutorAssembliesFromConfig(defaultsConfig);
            if (executorAssemblies != null)
            {
                targetDefaults.ExecutorAssemblies = executorAssemblies;
            }

            Func <IQueuedTaskToken, long> calculateDelayTicksTaskAfterFailureFn =
                CompileCalculateDelayTicksTaskAfterFailureFnFromConfig(defaultsConfig, parseOptions);

            if (calculateDelayTicksTaskAfterFailureFn != null)
            {
                targetDefaults.CalculateDelayMillisecondsTaskAfterFailure = calculateDelayTicksTaskAfterFailureFn;
            }

            Func <IQueuedTask, Exception, bool> isTaskErrorRecoverableFn =
                CompileIsTaskErrorRecoverableFnFromConfig(defaultsConfig, parseOptions);

            if (isTaskErrorRecoverableFn != null)
            {
                targetDefaults.IsTaskErrorRecoverable = isTaskErrorRecoverableFn;
            }

            if (!string.IsNullOrWhiteSpace(defaultsConfig.ConnectionStringName))
            {
                targetDefaults.ConnectionString = config.GetConnectionString(defaultsConfig.ConnectionStringName);
            }

            QueuedTaskMapping mappingFromConfig =
                GetQueudTaskMappingFromDefaultsConfig(defaultsConfig);

            targetDefaults.Mapping = MergeMappingFromConfig(targetDefaults.Mapping,
                                                            mappingFromConfig);

            if (defaultsConfig.WorkerCount > 0)
            {
                targetDefaults.WorkerCount = defaultsConfig.WorkerCount;
            }

            if (defaultsConfig.FaultErrorThresholdCount > 0)
            {
                targetDefaults.FaultErrorThresholdCount = defaultsConfig.FaultErrorThresholdCount;
            }

            if (defaultsConfig.AppMetricsCollectionIntervalMilliseconds > 0)
            {
                targetDefaults.AppMetricsCollectionIntervalMilliseconds = defaultsConfig.AppMetricsCollectionIntervalMilliseconds;
            }

            if (defaultsConfig.AppMetricsMonitoringEnabled.HasValue)
            {
                targetDefaults.AppMetricsMonitoringEnabled = defaultsConfig.AppMetricsMonitoringEnabled.Value;
            }

            if (defaultsConfig.SetupBuiltInDbAsssets.HasValue)
            {
                targetDefaults.SetupBuiltInDbAsssets = defaultsConfig.SetupBuiltInDbAsssets.Value;
            }

            return(targetDefaults);
        }