Beispiel #1
0
        public async Task ExecuteSkipsDeletedBuildTest()
        {
            // given - a message with a handler that fails
            var mockMessage = CreateMockMessage(CreateValidTestVstsMessage());

            var mockVstsHandler = new MockVstsHandler()
            {
                MockExecuteFunc =
                    msg =>
                {
                    Assert.Fail("should not be called");
                    throw new NotImplementedException("should not be called");
                },
            };

            var mockBuildClient = new MockBuildClient()
            {
                ReturnNullBuild = true
            };
            var mockMessageListener = new MockServiceBusQueueMessageListener();

            // when
            await ProcessTestMessage(mockMessage, mockMessageListener, new MockTaskClient(), mockVstsHandler, mockBuildClient : mockBuildClient);

            // then - logs are written to existing timeline
            Assert.IsTrue(mockMessageListener.IsCompleted);
        }
        public async Task ExecuteExceptionIsInstrumentedTest()
        {
            // given
            var handler = new MockVstsHandler()
            {
                MockExecuteFunc = (msg) => { throw new NotSupportedException(); }
            };
            var traceBrokerInstrumentation = new TraceBrokerInstrumentation();
            var testVstsMessage            = new TestVstsMessage()
            {
                RequestType = RequestType.Execute
            };

            // when
            var instrumentedHandler = new HandlerWithInstrumentation <TestVstsMessage>(traceBrokerInstrumentation, handler);

            Exception actualEx = null;

            try
            {
                await instrumentedHandler.Execute(testVstsMessage, default(CancellationToken));
            }
            catch (NotSupportedException ex)
            {
                actualEx = ex;
            }

            // then
            Assert.IsNotNull(actualEx);
            Assert.AreEqual(1, traceBrokerInstrumentation.Events.Count(x => x.Equals(testVstsMessage.RequestType.ToString())));
            Assert.AreEqual(1, traceBrokerInstrumentation.Events.Count(x => x.Equals(HandlerWithInstrumentation <TestVstsMessage> .HandlerExecuteFailedEventName)));
            Assert.AreEqual(1, traceBrokerInstrumentation.Events.Count(x => x.Equals("NotSupportedException")));
        }
Beispiel #3
0
        public async Task TimelineIsPreservedAndRetryIsIncrementedOnAbandonTest()
        {
            // given - a message with a handler that fails
            var mockMessage = CreateMockMessage(CreateValidTestVstsMessage());

            mockMessage.SetProperty(VstsMessageConstants.TaskLogIdPropertyName, 456);
            var maxRetryAttempts = 10;
            var currRetry        = 5;

            mockMessage.SetProperty(VstsMessageConstants.RetryAttemptPropertyName, currRetry);
            var mockTaskClient = new MockTaskClient();

            var executeCount    = 0;
            var mockVstsHandler = new MockVstsHandler()
            {
                MockExecuteFunc =
                    msg =>
                {
                    executeCount++;
                    throw new NotImplementedException("handler throws expected ex");
                },
            };
            var mockMessageListener = new MockServiceBusQueueMessageListener();

            // when
            await ProcessTestMessage(mockMessage, mockMessageListener, mockTaskClient, mockVstsHandler, maxRetryAttempts : maxRetryAttempts);

            // then - logs are written to existing timeline
            Assert.IsTrue(mockMessageListener.IsAbandoned);
            Assert.AreEqual(0, mockTaskClient.TimelineRecordsUpdated.Count);
            // Assert.AreEqual("456", mockMessage.UpdatedProperties[VstsMessageConstants.TaskLogIdPropertyName]);
            // Assert.AreEqual((currRetry + 1).ToString(), mockMessage.UpdatedProperties[VstsMessageConstants.RetryAttemptPropertyName]);
            Assert.AreEqual(1, executeCount);
        }
        public async Task MultipleEventsInstrumentedTest()
        {
            // given
            var handler = new MockVstsHandler()
            {
                MockExecuteFunc = (msg) => Task.FromResult(new VstsScheduleResult())
            };
            var traceBrokerInstrumentation = new TraceBrokerInstrumentation();
            var testVstsMessage            = new TestVstsMessage()
            {
                RequestType = RequestType.Execute
            };
            var events = new List <string>();

            // when
            events.Add(string.Format("[{0}] INFO: {1}: {2}", DateTime.UtcNow.ToString("o"), testVstsMessage.RequestType.ToString(), "Processing request"));
            var result = await handler.Execute(testVstsMessage, default(CancellationToken));

            events.Add(string.Format("[{0}] INFO: {1}: {2}: {3}", DateTime.UtcNow.ToString("o"), testVstsMessage.RequestType.ToString(), "Processed request", result.Message));
            var logStream = GenerateStreamFromStringList(events);
            await traceBrokerInstrumentation.HandleEventList(logStream, eventProperties : null, cancellationToken : default(CancellationToken));

            // then
            var requestType = testVstsMessage.RequestType.ToString();
            var logMessages = traceBrokerInstrumentation.Events.First(x => x.Contains(requestType));

            Assert.IsNotNull(logMessages);
            Assert.IsTrue(logMessages.IndexOf(requestType) >= 0);
            Assert.IsTrue(logMessages.Contains("Processing request"));
            Assert.IsTrue(logMessages.Contains("Processed request"));
        }
Beispiel #5
0
        public async Task ExecuteGoldenPathTestForBuild()
        {
            // given
            var testVstsMessage = CreateValidTestVstsMessage();

            testVstsMessage.BuildProperties.BuildName = "tacos";
            testVstsMessage.TestProperty         = "burrito";
            testVstsMessage.CompleteSychronously = false;

            var mockMessage    = CreateMockMessage(testVstsMessage);
            var mockTaskClient = new MockTaskClient();
            var executeCalled  = false;
            var handler        = new MockVstsHandler
            {
                MockExecuteFunc = (vstsMessage) =>
                {
                    Assert.IsNotNull(vstsMessage);
                    Assert.AreEqual("tacos", vstsMessage.BuildProperties.BuildName);
                    Assert.AreEqual("burrito", vstsMessage.TestProperty);
                    executeCalled = true;
                    return(Task.FromResult(new VstsScheduleResult()
                    {
                        Message = "(test) execution started", ScheduledId = "someId", ScheduleFailed = false
                    }));
                }
            };
            var mockMessageListener = new MockServiceBusQueueMessageListener();

            // when
            await ProcessTestMessage(mockMessage, mockMessageListener, mockTaskClient, handler);

            // then - executed successfully
            Assert.IsTrue(executeCalled);
            Assert.IsTrue(mockMessageListener.IsCompleted);

            // then - job assigned was called
            Assert.AreEqual(1, mockTaskClient.EventsReceived.Count);
            var assignedEvent = mockTaskClient.EventsReceived[0] as JobAssignedEvent;

            Assert.IsNotNull(assignedEvent);
            Assert.AreEqual(testVstsMessage.JobId, assignedEvent.JobId);

            // then - timeline was created
            Assert.AreEqual(1, mockTaskClient.TimelineRecordsUpdated.Count);
            Assert.AreEqual(string.Format("someTimeline_{0}", testVstsMessage.JobId), mockTaskClient.TimelineRecordsUpdated[0].Name);

            // then - logs were written
            Assert.AreEqual(2, mockTaskClient.LogLines.Count);
        }
Beispiel #6
0
        public async Task HandlerCancelExceptionTest()
        {
            // given
            var handler = new MockVstsHandler {
                MockCancelFunc = (vstsMessage) => { throw new NotSupportedException("die"); }
            };
            var traceBrokerInstrumentation = new TraceBrokerInstrumentation();
            var mockMessage         = CreateMockMessage(CreateValidTestVstsMessage());
            var mockMessageListener = new MockServiceBusQueueMessageListener();

            // when
            await ProcessTestMessage(handler : handler, mockMessageListener : mockMessageListener, instrumentation : traceBrokerInstrumentation, mockServiceBusMessage : mockMessage);

            // then
            Assert.IsTrue(mockMessageListener.IsAbandoned);
        }
Beispiel #7
0
        public async Task MessageIsDeadLetteredAndAbandonsBuildOnMaxRetryTest()
        {
            // given - a message with a handler that fails
            var mockMessage = CreateMockMessage(CreateValidTestVstsMessage());

            mockMessage.SetProperty(VstsMessageConstants.TaskLogIdPropertyName, 456);
            var maxRetryAttempts = 10;
            var currRetry        = 10;

            mockMessage.SetProperty(VstsMessageConstants.RetryAttemptPropertyName, currRetry);
            var mockTaskClient = new MockTaskClient();

            var executeCount    = 0;
            var mockVstsHandler = new MockVstsHandler()
            {
                MockExecuteFunc =
                    msg =>
                {
                    executeCount++;
                    throw new NotImplementedException("handler throws expected ex");
                },
            };
            var mockMessageListener = new MockServiceBusQueueMessageListener();

            // when
            await ProcessTestMessage(mockMessage, mockMessageListener, mockTaskClient, mockVstsHandler, maxRetryAttempts : maxRetryAttempts);

            // then - logs are written to existing timeline
            Assert.IsTrue(mockMessageListener.IsDeadLettered);
            Assert.AreEqual(0, mockTaskClient.TimelineRecordsUpdated.Count);
            // Assert.AreEqual("456", mockMessage.UpdatedProperties[VstsMessageConstants.TaskLogIdPropertyName]);
            // Assert.AreEqual((currRetry + 1).ToString(), mockMessage.UpdatedProperties[VstsMessageConstants.RetryAttemptPropertyName]);
            // Assert.AreEqual("NotImplementedException", mockMessage.UpdatedProperties[VstsMessageConstants.ErrorTypePropertyName]);
            Assert.AreEqual(1, executeCount);

            // then - build was failed
            Assert.AreEqual(2, mockTaskClient.EventsReceived.Count);
            var assignedEvent = mockTaskClient.EventsReceived[0] as JobAssignedEvent;

            Assert.IsNotNull(assignedEvent);
            var completedEvent = mockTaskClient.EventsReceived[1] as JobCompletedEvent;

            Assert.IsNotNull(completedEvent);
            Assert.AreEqual(TaskResult.Abandoned, completedEvent.Result);
        }
Beispiel #8
0
        public async Task ExecuteScheduleFailedGoldenPathTest()
        {
            // given
            var testVstsMessage = CreateValidTestVstsMessage();

            testVstsMessage.CompleteSychronously = true;
            var mockMessage         = CreateMockMessage(testVstsMessage);
            var mockTaskClient      = new MockTaskClient();
            var mockReportingHelper = new MockVstsReportingHelper(testVstsMessage);
            var executeCalled       = false;
            var handler             = new MockVstsHandler
            {
                MockExecuteFunc = (vstsMessage) =>
                {
                    executeCalled = true;
                    return(Task.FromResult(new VstsScheduleResult()
                    {
                        Message = "some error message", ScheduleFailed = true
                    }));
                }
            };

            // when
            await ProcessTestMessage(mockMessage, null, mockTaskClient, handler, mockReportingHelper);

            // then - executed successfully
            Assert.IsTrue(executeCalled);

            // then - job assigned & job completed was called
            Assert.AreEqual(1, mockTaskClient.EventsReceived.Count);
            var assignedEvent = mockTaskClient.EventsReceived[0] as JobAssignedEvent;

            Assert.IsNotNull(assignedEvent);

            Assert.AreEqual(2, mockReportingHelper.JobStatusReceived.Count);
            var startedMessage = mockReportingHelper.JobStatusReceived[0];

            Assert.AreEqual(MockVstsReportingHelper.JobStatusEnum.Started, startedMessage);
            var completedMessage = mockReportingHelper.JobStatusReceived[1];

            Assert.AreEqual(MockVstsReportingHelper.JobStatusEnum.Completed, completedMessage);
            Assert.AreEqual(testVstsMessage.JobId, mockReportingHelper.VstsMessage.JobId);
            Assert.IsFalse(mockReportingHelper.JobStatusSuccess);
        }
        public async Task ExecuteIsInstrumentedTest()
        {
            // given
            var handler = new MockVstsHandler()
            {
                MockExecuteFunc = (msg) => Task.FromResult(new VstsScheduleResult())
            };
            var traceBrokerInstrumentation = new TraceBrokerInstrumentation();
            var testVstsMessage            = new TestVstsMessage()
            {
                RequestType = RequestType.Execute
            };

            // when
            var instrumentedHandler = new HandlerWithInstrumentation <TestVstsMessage>(traceBrokerInstrumentation, handler);
            await instrumentedHandler.Execute(testVstsMessage, default(CancellationToken));

            // then
            Assert.AreEqual(2, traceBrokerInstrumentation.Events.Count(x => x.Equals(testVstsMessage.RequestType.ToString())));
        }
Beispiel #10
0
        public async Task CancelGoldenPathTest()
        {
            // given - a cancel message
            var testVstsMessage = CreateValidTestVstsMessage();

            testVstsMessage.RequestType = RequestType.Cancel;
            var mockMessage    = CreateMockMessage(testVstsMessage);
            var mockTaskClient = new MockTaskClient();

            var cancelCalled = false;
            var handler      = new MockVstsHandler
            {
                MockCancelFunc = (vstsMessage) =>
                {
                    Assert.IsNotNull(vstsMessage);
                    cancelCalled = true;
                    return(Task.FromResult("(test) cancel reqeusted!"));
                }
            };
            var mockMessageListener = new MockServiceBusQueueMessageListener();

            // when
            await ProcessTestMessage(mockMessage, mockMessageListener, mockTaskClient, handler);

            // then - cancelled successfully
            Assert.IsTrue(cancelCalled);
            Assert.IsTrue(mockMessageListener.IsCompleted);

            // then - job assigned is not called
            Assert.AreEqual(0, mockTaskClient.EventsReceived.Count);

            // then - timeline was created
            Assert.AreEqual(1, mockTaskClient.TimelineRecordsUpdated.Count);
            Assert.AreEqual(string.Format("someTimeline_{0}", testVstsMessage.JobId), mockTaskClient.TimelineRecordsUpdated[0].Name);

            // then - logs were written
            Assert.AreEqual(2, mockTaskClient.LogLines.Count);
        }
Beispiel #11
0
        private static async Task ProcessTestMessage(MockServiceBusMessage mockServiceBusMessage = null, MockServiceBusQueueMessageListener mockMessageListener = null, MockTaskClient mockTaskClient = null, MockVstsHandler handler = null, MockVstsReportingHelper mockReportingHelper = null, IBrokerInstrumentation instrumentation = null, int maxRetryAttempts = 1, IBuildClient mockBuildClient = null, IGitClient mockGitClient = null)
        {
            mockServiceBusMessage = mockServiceBusMessage ?? CreateMockMessage(CreateValidTestVstsMessage());
            mockTaskClient        = mockTaskClient ?? new MockTaskClient();
            mockBuildClient       = mockBuildClient ?? new MockBuildClient()
            {
                MockBuild = new Build()
                {
                    Status = BuildStatus.InProgress
                }
            };
            mockReportingHelper = mockReportingHelper ?? new MockVstsReportingHelper(new TestVstsMessage());
            var mockReleaseClient = new MockReleaseClient()
            {
                MockRelease = new Release()
                {
                    Status = ReleaseStatus.Active
                }
            };

            handler = handler ?? new MockVstsHandler {
                MockExecuteFunc = (vstsMessage) => Task.FromResult(new VstsScheduleResult()
                {
                    Message = "(test) mock execute requested", ScheduledId = "someId", ScheduleFailed = false
                })
            };
            instrumentation = instrumentation ?? new TraceBrokerInstrumentation();
            var settings = new ServiceBusQueueMessageHandlerSettings {
                MaxRetryAttempts = maxRetryAttempts, TimeLineNamePrefix = "someTimeline", WorkerName = "someWorker"
            };

            mockMessageListener = mockMessageListener ?? new MockServiceBusQueueMessageListener();
            var schedulingBroker = new ServiceBusQueueMessageHandler <TestVstsMessage>(queueClient: mockMessageListener, baseInstrumentation: instrumentation, scheduleHandler: handler, settings: settings);

            schedulingBroker.CreateTaskClient          = (uri, creds, instrumentationHandler, skipRaisePlanEvents) => mockTaskClient;
            schedulingBroker.CreateBuildClient         = (uri, creds) => mockBuildClient;
            schedulingBroker.CreateReleaseClient       = (uri, creds) => mockReleaseClient;
            schedulingBroker.CreateVstsReportingHelper = (vstsMessage, inst, props) => mockReportingHelper;
            var cancelSource = new CancellationTokenSource();
            await schedulingBroker.ReceiveAsync(mockServiceBusMessage, cancelSource.Token);
        }
        private static async Task ProcessTestMessage(MockServiceBusMessage mockServiceBusMessage = null, MockServiceBusQueueMessageListener mockMessageListener = null, MockTaskClient mockTaskClient = null, MockVstsHandler handler = null, MockJobStatusReportingHelper mockReportingHelper = null, ILogger logger = null, int maxRetryAttempts = 1, IBuildClient mockBuildClient = null)
        {
            mockServiceBusMessage = mockServiceBusMessage ?? CreateMockMessage(CreateValidTestVstsMessage());
            mockTaskClient        = mockTaskClient ?? new MockTaskClient();
            mockBuildClient       = mockBuildClient ?? new MockBuildClient()
            {
                MockBuild = new Build()
                {
                    Status = BuildStatus.InProgress
                }
            };
            mockReportingHelper = mockReportingHelper ?? new MockJobStatusReportingHelper(new TestVstsMessage());
            var mockReleaseClient = new MockReleaseClient()
            {
                MockRelease = new Release()
                {
                    Status = ReleaseStatus.Active
                }
            };

            handler = handler ?? new MockVstsHandler {
                MockExecuteFunc = (vstsMessage) => Task.FromResult(new VstsScheduleResult()
                {
                    Message = "(test) mock execute requested", ScheduledId = "someId", ScheduleFailed = false
                })
            };
            logger = logger ?? new TraceLogger();
            var settings = new ServiceBusQueueMessageHandlerSettings {
                MaxRetryAttempts = maxRetryAttempts, TimeLineNamePrefix = "someTimeline", WorkerName = "someWorker"
            };

            mockMessageListener = mockMessageListener ?? new MockServiceBusQueueMessageListener();
            var schedulingBroker = new TestableServiceBusQueueMessageHandler(mockMessageListener, handler, settings, logger, mockTaskClient, mockBuildClient, mockReportingHelper, mockReleaseClient);
            var cancelSource     = new CancellationTokenSource();
            await schedulingBroker.ReceiveAsync(mockServiceBusMessage, cancelSource.Token);
        }