private IFunctionInstance CreateFunctionInstance(Guid id)
        {
            var method     = GetType().GetMethod(nameof(TestFunction), BindingFlags.NonPublic | BindingFlags.Static);
            var descriptor = FunctionIndexer.FromMethod(method);

            return(new FunctionInstance(id, null, new ExecutionReason(), null, null, descriptor));
        }
예제 #2
0
        private void RunOnFunctionTimeoutTest(bool isDebugging, string expectedMessage)
        {
            System.Timers.Timer timer = new System.Timers.Timer(TimeSpan.FromMinutes(1).TotalMilliseconds);
            timer.Start();

            Assert.True(timer.Enabled);
            Assert.False(_cancellationTokenSource.IsCancellationRequested);

            MethodInfo       method    = typeof(Functions).GetMethod("MethodLevel", BindingFlags.Static | BindingFlags.Public);
            TimeoutAttribute attribute = method.GetCustomAttribute <TimeoutAttribute>();

            _descriptor = FunctionIndexer.FromMethod(method);
            Guid instanceId            = Guid.Parse("B2D1DD72-80E2-412B-A22E-3B4558F378B4");
            bool timeoutWhileDebugging = false;

            TestLogger logger = new TestLogger("Tests.FunctionExecutor");

            FunctionExecutor.OnFunctionTimeout(timer, _descriptor, instanceId, attribute.Timeout, timeoutWhileDebugging, logger, _cancellationTokenSource, () => isDebugging);

            Assert.False(timer.Enabled);
            Assert.NotEqual(isDebugging, _cancellationTokenSource.IsCancellationRequested);

            string message = string.Format("Timeout value of 00:01:00 exceeded by function 'Functions.MethodLevel' (Id: 'b2d1dd72-80e2-412b-a22e-3b4558f378b4'). {0}", expectedMessage);

            // verify ILogger
            LogMessage log = logger.LogMessages.Single();

            Assert.Equal(LogLevel.Error, log.Level);
            Assert.Equal(message, log.FormattedMessage);
        }
예제 #3
0
 public void FormatLockId_ReturnsExpectedValue(SingletonScope scope, string scopeId, string expectedLockId)
 {
     MethodInfo methodInfo = this.GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.NonPublic);
     var descriptor = FunctionIndexer.FromMethod(methodInfo);
     string actualLockId = SingletonManager.FormatLockId(descriptor, scope, "TestHostId", scopeId);
     Assert.Equal(expectedLockId, actualLockId);
 }
예제 #4
0
        public void GetListenerSingletonOrNull_ReturnsListenerClassSingleton()
        {
            MethodInfo method = this.GetType().GetMethod("TestJob", BindingFlags.Static | BindingFlags.NonPublic);
            var descriptor = FunctionIndexer.FromMethod(method);

            SingletonAttribute attribute = SingletonManager.GetListenerSingletonOrNull(typeof(TestListener), descriptor);
            Assert.Equal("Listener", attribute.ScopeId);
        }
예제 #5
0
        public void GetListenerSingletonOrNull_ThrowsOnMultiple()
        {
            MethodInfo method = this.GetType().GetMethod("TestJob_MultipleListenerSingletons", BindingFlags.Static | BindingFlags.NonPublic);
            var descriptor = FunctionIndexer.FromMethod(method);

            NotSupportedException exception = Assert.Throws<NotSupportedException>(() =>
            {
                SingletonManager.GetListenerSingletonOrNull(typeof(TestListener), descriptor);
            });
            Assert.Equal("Only one SingletonAttribute using mode 'Listener' is allowed.", exception.Message);
        }
예제 #6
0
        public void StartFunctionTimeout_NoCancellationTokenParameter_ThrowOnTimeoutFalse_ReturnsNull()
        {
            MethodInfo method = typeof(Functions).GetMethod("NoCancellationTokenParameter", BindingFlags.Static | BindingFlags.Public);

            _descriptor = FunctionIndexer.FromMethod(method);

            TimeoutAttribute attribute = typeof(Functions).GetCustomAttribute <TimeoutAttribute>();

            attribute.ThrowOnTimeout = false;

            System.Timers.Timer timer = FunctionExecutor.StartFunctionTimeout(_mockFunctionInstance.Object, attribute, _cancellationTokenSource, _traceWriter, null);

            Assert.Null(timer);

            _mockFunctionInstance.VerifyAll();
        }
예제 #7
0
        public void StartFunctionTimeout_ClassLevelTimeout_CreatesExpectedTimer()
        {
            MethodInfo method = typeof(Functions).GetMethod("ClassLevel", BindingFlags.Static | BindingFlags.Public);

            _descriptor = FunctionIndexer.FromMethod(method);

            // we need to set up the Id so that when the timer fires it doesn't throw, but since this is Strict, we need to access it first.
            _mockFunctionInstance.SetupGet(p => p.Id).Returns(Guid.Empty);
            Assert.NotNull(_mockFunctionInstance.Object.Id);

            TimeoutAttribute attribute = typeof(Functions).GetCustomAttribute <TimeoutAttribute>();

            System.Timers.Timer timer = FunctionExecutor.StartFunctionTimeout(_mockFunctionInstance.Object, attribute, _cancellationTokenSource, _traceWriter, null);

            Assert.True(timer.Enabled);
            Assert.Equal(attribute.Timeout.TotalMilliseconds, timer.Interval);

            _mockFunctionInstance.VerifyAll();
        }
예제 #8
0
        public async Task InvokeAsync_Stop_Timeout_Throw()
        {
            Mock <IFunctionInvoker> mockInvoker = new Mock <IFunctionInvoker>();

            mockInvoker.Setup(i => i.InvokeAsync(It.IsAny <object>(), It.IsAny <object[]>()))
            .Returns(async() =>
            {
                bool exit   = false;
                Task ignore = Task.Delay(5000).ContinueWith((ct) => exit = true);
                while (!exit)
                {
                    await Task.Delay(500);
                }
                return(null);
            });

            // setup the instance details for the exception message
            MethodInfo method = typeof(Functions).GetMethod("ClassLevel", BindingFlags.Static | BindingFlags.Public);

            _descriptor = FunctionIndexer.FromMethod(method);
            _mockFunctionInstance.SetupGet(p => p.Id).Returns(Guid.Empty);

            var timeoutSource  = new CancellationTokenSource();
            var shutdownSource = new CancellationTokenSource();

            object[] parameters     = new object[] { shutdownSource.Token };
            bool     throwOnTimeout = true;

            TimeSpan timeoutInterval = TimeSpan.FromMilliseconds(1000);

            shutdownSource.CancelAfter(500);
            timeoutSource.CancelAfter(timeoutInterval);
            var ex = await Assert.ThrowsAsync <FunctionTimeoutException>(() => FunctionExecutor.InvokeAsync(mockInvoker.Object, NewArgs(parameters), timeoutSource, shutdownSource,
                                                                                                            throwOnTimeout, timeoutInterval, _mockFunctionInstance.Object));

            var expectedMessage = string.Format("Timeout value of {0} was exceeded by function: {1}", timeoutInterval, _mockFunctionInstance.Object.FunctionDescriptor.ShortName);

            Assert.Equal(expectedMessage, ex.Message);
        }