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")); }
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"))); }
public void RetryAndThrowWhenFail() { // given var traceBrokerInstrumentation = new TraceBrokerInstrumentation(); var cancelSource = new CancellationTokenSource(); var retryEventHandler = new RetryEventHandler("ThrowEvent", null, cancelSource.Token, traceBrokerInstrumentation); // when try { this.retryer.TryActionAsync <int>( () => { throw new WebException("Unauthorized", null, WebExceptionStatus.ConnectionClosed, new MockWebResponse(HttpStatusCode.Unauthorized)); }, retryEventHandler, true).ConfigureAwait(false); } catch (Exception ex) { Assert.AreEqual(ex.Message, "RequestTimeout"); } // then var events = traceBrokerInstrumentation.Events; Assert.AreEqual(events.Count, RetryCount); }
public void WaitAndRetry() { // given var traceBrokerInstrumentation = new TraceBrokerInstrumentation(); var cancelSource = new CancellationTokenSource(); var eventProperties = new Dictionary <string, string>() { { "VstsPlanUrl", "testUrl" }, { "ProjectId", "testProjectId" }, { "PlanId", "testPlanId" } }; var retryEventHandler = new RetryEventHandler("TestEvent", eventProperties, cancelSource.Token, traceBrokerInstrumentation); // when retryer.TryActionAsync <int>( () => { Exception ex = new Exception("Unauthorized"); throw new WebException("Unauthorized", ex, WebExceptionStatus.ConnectionClosed, new MockWebResponse(HttpStatusCode.Unauthorized)); }, retryEventHandler).ConfigureAwait(false); // then var events = traceBrokerInstrumentation.Events; Assert.AreEqual(events.Count, RetryCount); Assert.AreEqual(events[0], "TestEvent_Retry"); }
public async Task BasicExecuteInstrumentationTest() { // given var traceBrokerInstrumentation = new TraceBrokerInstrumentation(); // when await ProcessTestMessage(instrumentation : traceBrokerInstrumentation); // then Assert.AreEqual(3, traceBrokerInstrumentation.Events.Count); }
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); }
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()))); }
public void CheckShouldRetry() { // given var traceBrokerInstrumentation = new TraceBrokerInstrumentation(); var cancelSource = new CancellationTokenSource(); var retryEventHandler = new RetryEventHandler("TestEvent", null, cancelSource.Token, traceBrokerInstrumentation); retryEventHandler.ShouldRetry = (e, count) => (e is InvalidFilterCriteriaException && count < 1); // when retryer.TryActionAsync <int>( () => { throw new InvalidFilterCriteriaException("custom exception"); }, retryEventHandler).ConfigureAwait(false); // then var events = traceBrokerInstrumentation.Events; Assert.AreEqual(events.Count, 1); }
public void ReplaceDefaultTransientErrors() { // given var traceBrokerInstrumentation = new TraceBrokerInstrumentation(); var cancelSource = new CancellationTokenSource(); var trasientHttpCodes = new HashSet <HttpStatusCode>() { HttpStatusCode.ExpectationFailed }; var retryEventHandler = new RetryEventHandler("ReplaceDefaultEvent", null, cancelSource.Token, traceBrokerInstrumentation, trasientHttpCodes); // when retryer.TryActionAsync <int>( () => { throw new WebException("ExpectationFailed", null, WebExceptionStatus.ConnectionClosed, new MockWebResponse(HttpStatusCode.ExpectationFailed)); }, retryEventHandler).ConfigureAwait(false); // then var events = traceBrokerInstrumentation.Events; Assert.AreEqual(events.Count, RetryCount); }