示例#1
0
        public async Task AddWorkerIfNeeded_Returns_Expected(WorkerConcurrencyOptions options,
                                                             int[] latencies1, int[] latencies2, bool readyForInvocations1, bool readyForInvocations2,
                                                             TimeSpan elapsedFromLastAdding, bool expected)
        {
            Mock <IFunctionInvocationDispatcher>        functionInvocationDispatcher        = new Mock <IFunctionInvocationDispatcher>(MockBehavior.Strict);
            Mock <IFunctionInvocationDispatcherFactory> functionInvocationDispatcherFactory = new Mock <IFunctionInvocationDispatcherFactory>(MockBehavior.Strict);

            functionInvocationDispatcherFactory.Setup(x => x.GetFunctionDispatcher()).Returns(functionInvocationDispatcher.Object);

            Dictionary <string, WorkerStatus> workerStatuses = new Dictionary <string, WorkerStatus>();

            workerStatuses.Add("test1", new WorkerStatus()
            {
                IsReady        = readyForInvocations1,
                LatencyHistory = latencies1.Select(x => TimeSpan.FromMilliseconds(x))
            });
            workerStatuses.Add("test2", new WorkerStatus()
            {
                IsReady        = readyForInvocations2,
                LatencyHistory = latencies2.Select(x => TimeSpan.FromMilliseconds(x))
            });

            WorkerConcurrencyManager concurrancyManager = new WorkerConcurrencyManager(functionInvocationDispatcherFactory.Object, _testEnvironment, Options.Create(options),
                                                                                       _functionsHostingConfigurations, _applicationLifetime, _loggerFactory);
            await concurrancyManager.StartAsync(CancellationToken.None);

            bool value = concurrancyManager.NewWorkerIsRequired(workerStatuses, elapsedFromLastAdding);

            Assert.Equal(value, expected);
        }
示例#2
0
 public async Task StartAsync_DoesNotGetDispatcher(string workerRuntime)
 {
     _testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionWorkerRuntime, workerRuntime);
     Mock <IFunctionInvocationDispatcherFactory> functionInvocationDispatcherFactory = new Mock <IFunctionInvocationDispatcherFactory>(MockBehavior.Strict);
     WorkerConcurrencyManager concurrancyManager = new WorkerConcurrencyManager(functionInvocationDispatcherFactory.Object, _testEnvironment, Options.Create(new WorkerConcurrencyOptions()),
                                                                                _functionsHostingConfigurations, _applicationLifetime, _loggerFactory);
     await concurrancyManager.StartAsync(CancellationToken.None);
 }
示例#3
0
        public async Task Start_HttpWorker_DoesNot_StartTimer()
        {
            Mock <IFunctionInvocationDispatcherFactory> functionInvocationDispatcherFactory = new Mock <IFunctionInvocationDispatcherFactory>(MockBehavior.Strict);

            functionInvocationDispatcherFactory.Setup(x => x.GetFunctionDispatcher()).Returns(new HttpFunctionInvocationDispatcher());
            WorkerConcurrencyManager concurrancyManger = new WorkerConcurrencyManager(functionInvocationDispatcherFactory.Object, _testEnvironment, Options.Create(new WorkerConcurrencyOptions()),
                                                                                      _functionsHostingConfigurations, _applicationLifetime, _loggerFactory);
            await concurrancyManger.StartAsync(CancellationToken.None);

            await Task.Delay(1000);

            var sratedLog = _loggerProvider.GetAllLogMessages().FirstOrDefault(x => x.FormattedMessage.StartsWith("Http dynamic worker concurrency is not supported."));

            Assert.True(sratedLog != null);
        }
示例#4
0
        public async Task IsOverloaded_Returns_Expected(WorkerConcurrencyOptions options, int[] latencies, bool expected)
        {
            Mock <IFunctionInvocationDispatcher>        functionInvocationDispatcher        = new Mock <IFunctionInvocationDispatcher>(MockBehavior.Strict);
            Mock <IFunctionInvocationDispatcherFactory> functionInvocationDispatcherFactory = new Mock <IFunctionInvocationDispatcherFactory>(MockBehavior.Strict);

            functionInvocationDispatcherFactory.Setup(x => x.GetFunctionDispatcher()).Returns(functionInvocationDispatcher.Object);
            WorkerConcurrencyManager concurrancyManger = new WorkerConcurrencyManager(functionInvocationDispatcherFactory.Object, _testEnvironment, Options.Create(options),
                                                                                      _functionsHostingConfigurations, _applicationLifetime, _loggerFactory);
            await concurrancyManger.StartAsync(CancellationToken.None);

            WorkerStatus status = new WorkerStatus()
            {
                LatencyHistory = latencies.Select(x => TimeSpan.FromMilliseconds(x))
            };

            Assert.Equal(concurrancyManger.IsOverloaded(status), expected);
        }
示例#5
0
        public async Task Start_DoesNot_StartTimer_WhenDynamicConcurrencyDisabled()
        {
            Mock <IFunctionInvocationDispatcher>        functionInvocationDispatcher        = new Mock <IFunctionInvocationDispatcher>(MockBehavior.Strict);
            Mock <IFunctionInvocationDispatcherFactory> functionInvocationDispatcherFactory = new Mock <IFunctionInvocationDispatcherFactory>(MockBehavior.Strict);

            functionInvocationDispatcherFactory.Setup(x => x.GetFunctionDispatcher()).Returns(functionInvocationDispatcher.Object);
            _testEnvironment.SetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerDynamicConcurrencyEnabled, "false");
            WorkerConcurrencyManager concurrancyManger = new WorkerConcurrencyManager(functionInvocationDispatcherFactory.Object, _testEnvironment, Options.Create(new WorkerConcurrencyOptions()),
                                                                                      _functionsHostingConfigurations, _applicationLifetime, _loggerFactory);
            await concurrancyManger.StartAsync(CancellationToken.None);

            await Task.Delay(1000);

            var sratedLog = _loggerProvider.GetAllLogMessages().FirstOrDefault(x => x.FormattedMessage.StartsWith("Starting dynamic worker concurrency monitoring."));

            Assert.True(sratedLog == null);

            _testEnvironment.SetEnvironmentVariable(RpcWorkerConstants.FunctionsWorkerDynamicConcurrencyEnabled, null);
        }
示例#6
0
        public async Task Start_StartsTimer_WhenDynamicConcurrencyEnabled()
        {
            WorkerConcurrencyOptions options = new WorkerConcurrencyOptions()
            {
                AdjustmentPeriod = TimeSpan.Zero
            };

            Mock <IFunctionInvocationDispatcher>        functionInvocationDispatcher        = new Mock <IFunctionInvocationDispatcher>(MockBehavior.Strict);
            Mock <IFunctionInvocationDispatcherFactory> functionInvocationDispatcherFactory = new Mock <IFunctionInvocationDispatcherFactory>(MockBehavior.Strict);

            functionInvocationDispatcherFactory.Setup(x => x.GetFunctionDispatcher()).Returns(functionInvocationDispatcher.Object);
            WorkerConcurrencyManager concurrancyManger = new WorkerConcurrencyManager(functionInvocationDispatcherFactory.Object, _testEnvironment, Options.Create(options),
                                                                                      _functionsHostingConfigurations, _applicationLifetime, _loggerFactory);
            await concurrancyManger.StartAsync(CancellationToken.None);

            await TestHelpers.Await(() =>
            {
                var sratedLog = _loggerProvider.GetAllLogMessages().FirstOrDefault(x => x.FormattedMessage.StartsWith("Starting dynamic worker concurrency monitoring."));
                return(sratedLog != null);
            }, pollingInterval : 1000, timeout : 10 * 1000);
        }
示例#7
0
        public async Task ActivateWorkerConcurency_FunctionsHostingConfiguration_WorkAsExpected()
        {
            TestEnvironment testEnvironment = new TestEnvironment();

            testEnvironment.SetEnvironmentVariable(EnvironmentSettingNames.FunctionWorkerRuntime, RpcWorkerConstants.NodeLanguageWorkerName);
            Mock <IFunctionInvocationDispatcher>        functionInvocationDispatcher        = new Mock <IFunctionInvocationDispatcher>(MockBehavior.Strict);
            Mock <IFunctionInvocationDispatcherFactory> functionInvocationDispatcherFactory = new Mock <IFunctionInvocationDispatcherFactory>(MockBehavior.Strict);

            functionInvocationDispatcherFactory.Setup(x => x.GetFunctionDispatcher()).Returns(functionInvocationDispatcher.Object);
            Mock <IFunctionsHostingConfiguration> conf = new Mock <IFunctionsHostingConfiguration>();

            conf.Setup(x => x.FunctionsWorkerDynamicConcurrencyEnabled).Returns(true);
            WorkerConcurrencyOptions options = new WorkerConcurrencyOptions();

            WorkerConcurrencyManager concurrancyManager = new WorkerConcurrencyManager(functionInvocationDispatcherFactory.Object, testEnvironment, Options.Create(options), conf.Object, _applicationLifetime, _loggerFactory);

            concurrancyManager.ActivationTimerInterval = TimeSpan.FromMilliseconds(100);
            await concurrancyManager.StartAsync(CancellationToken.None);

            await TestHelpers.Await(() => _loggerProvider.GetAllLogMessages().SingleOrDefault(x => x.FormattedMessage.StartsWith("Dynamic worker concurrency monitoring was started by activation timer.")) != null, timeout : 1000, pollingInterval : 100);

            conf.Setup(x => x.FunctionsWorkerDynamicConcurrencyEnabled).Returns(false);
            await TestHelpers.Await(() => _loggerProvider.GetAllLogMessages().SingleOrDefault(x => x.FormattedMessage.StartsWith("Dynamic worker concurrency monitoring is disabled after activation. Shutting down Functions Host.")) != null, timeout : 1000, pollingInterval : 100);
        }