Beispiel #1
0
        public void End_WithDisposedTimedScope_ShouldLogError()
        {
            FailOnErrors = false;

            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateDefaultTimedScope(timedScopeLoggerMock.Object, replyEventConfiguratorMock.Object, machineInformation, timedScopeStackManager))
            {
                Assert.True(scope.IsScopeActive, "Timer should be active.");

                scope.Dispose();

                Assert.False(scope.IsScopeActive, "Dispose should turn off timer.");

                scope.End();

                Assert.Equal(TraceErrors.Count(), 1);
                LoggedEvents.Clear();
            }
        }
Beispiel #2
0
        public void SuccessTimedScope_DoesntReplayLogs()
        {
            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();

            Correlation = new Correlation(new MemoryCorrelationHandler(), CallContextManagerInstance, MachineInformation);
            Correlation.CorrelationStart(new CorrelationData());

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(CallContextManagerInstance, machineInformation);
            CorrelationData         currentCorrelation     = Correlation.CurrentCorrelation;

            Assert.False(currentCorrelation.ShouldReplayUls);

            using (TimedScope scope = TimedScope.Start(currentCorrelation, MachineInformation, "TestScope", customLogger: timedScopeLoggerMock.Object,
                                                       replayEventConfigurator: replyEventConfiguratorMock.Object, timedScopeStackManager: timedScopeStackManager))
            {
                scope.Result = TimedScopeResult.Success;

                Mock <IReplayEventDisabledTimedScopes> disabledScopes = new Mock <IReplayEventDisabledTimedScopes>();
                disabledScopes.Setup(x => x.IsDisabled(scope.ScopeDefinition)).Returns(false);

                ReplayEventConfigurator configurator = new ReplayEventConfigurator(disabledScopes.Object, Correlation);
                configurator.ConfigureReplayEventsOnScopeEnd(scope);
            }

            Assert.False(currentCorrelation.ShouldReplayUls);
        }
Beispiel #3
0
        public void FailedScope_ResultAndFailureDescription_ShouldOutputValueInLogEvent()
        {
            FailOnErrors = false;

            UnitTestTimedScopeLogger        unitTestTimedScopeLogger   = new UnitTestTimedScopeLogger();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateTestCountersUnitTestTimedScope(machineInformation: machineInformation, scopeLogger: unitTestTimedScopeLogger,
                                                                                     replayEventConfigurator: replyEventConfiguratorMock.Object, timedScopeStackManager: timedScopeStackManager))
            {
                scope.Result             = TimedScopeResult.ExpectedError;
                scope.FailureDescription = UnitTestFailureDescription.ExampleDescription;
            }

            TimedScopeLogEvent scopeEvent = unitTestTimedScopeLogger.Events.SingleOrDefault();

            if (VerifyNotNullAndReturn(scopeEvent, "Scope end event should be logged"))
            {
                Assert.Equal(scopeEvent.Result, TimedScopeResult.ExpectedError);
                Assert.Equal(scopeEvent.FailureDescription, UnitTestFailureDescription.ExampleDescription.ToString());
            }
        }
Beispiel #4
0
        public void DefaultTimedScopeResult_LogsAsSystemError()
        {
            LoggedEvents.Clear();

            CorrelationData data = new CorrelationData();

            UnitTestTimedScopeLogger        unitTestTimedScopeLogger   = new UnitTestTimedScopeLogger();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope.Create(data, machineInformation, TestHooks.DefaultTimedScopeName, "description", unitTestTimedScopeLogger,
                                     replyEventConfiguratorMock.Object, timedScopeStackManager, default(TimedScopeResult)))
            {
            }

            TimedScopeLogEvent evt = unitTestTimedScopeLogger.SingleTimedScopeEvent(TestHooks.DefaultTimedScopeName);

            if (VerifyNotNullAndReturn(evt, "A scope event has been logged"))
            {
                Assert.Equal(TimedScopeResult.SystemError, evt.Result);
            }
        }
Beispiel #5
0
        public void AddLoggingValue_ShouldOutputValueInLogEvent()
        {
            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateTestCountersUnitTestTimedScope(machineInformation: machineInformation, scopeLogger: timedScopeLoggerMock.Object,
                                                                                     replayEventConfigurator: replyEventConfiguratorMock.Object, timedScopeStackManager: timedScopeStackManager))
            {
                scope.AddLoggingValue(TimedScopeDataKeys.Category, "MyCategory");
                scope.End();

                // There should be one 'Ending' transaction log call with formatted output
                foreach (LogEventArgs args in LoggedEvents)
                {
                    if (args.CategoryId == Categories.TimingGeneral)
                    {
                        if (args.FullMessage.Contains("Ending timed scope"))
                        {
                            Assert.Contains("Category:'MyCategory';", args.FullMessage, StringComparison.Ordinal);
                        }
                    }
                }
            }
        }
		public void FailedTimedScope_ShouldReplayLogs()
		{

			Mock<ITimedScopeLogger> timedScopeLoggerMock = new Mock<ITimedScopeLogger>();
			Mock<IReplayEventConfigurator> replyEventConfiguratorMock = new Mock<IReplayEventConfigurator>();
			Mock<ILogEventCache> mockCache = new Mock<ILogEventCache>();

			Correlation = new Correlation(new MemoryCorrelationHandler(), CallContextManagerInstance, MachineInformation);
			Correlation.CorrelationStart(new CorrelationData(mockCache.Object));

			IMachineInformation machineInformation = new UnitTestMachineInformation();
			ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(CallContextManagerInstance, machineInformation);
			CorrelationData currentCorrelation = Correlation.CurrentCorrelation;

			Assert.False(currentCorrelation.ShouldReplayUls, "Logs shouldn't be replayed");

			using (TimedScope scope = TestHooks.CreateDefaultTimedScope(
				timedScopeLoggerMock.Object,
				replyEventConfiguratorMock.Object,
				machineInformation,
				timedScopeStackManager,
				startScope: true))
			{
				scope.Result = TimedScopeResult.SystemError;

				Mock<IReplayEventDisabledTimedScopes> disabledScopes = new Mock<IReplayEventDisabledTimedScopes>();
				disabledScopes.Setup(x => x.IsDisabled(scope.ScopeDefinition)).Returns(false);

				ReplayEventConfigurator configurator = new ReplayEventConfigurator(disabledScopes.Object, Correlation);
				configurator.ConfigureReplayEventsOnScopeEnd(scope);
			}

			Assert.True(currentCorrelation.ShouldReplayUls, "Logs should be replayed");
		}
        public void GetTimedScopeStack_CallContextManager_Null_Value()
        {
            FailOnErrors = false;

            IMachineInformation machineInformation = new UnitTestMachineInformation();

            Assert.Throws <ArgumentNullException>(() =>
            {
                new TimedScopeStackManager(null, machineInformation);
            });
        }
Beispiel #8
0
        public void TestInitialize()
        {
            CallContextManagerInstance = CreateCallContextManager();
            CallContextManagerInstance.CallContextOverride = new HttpCallContext(useLogicalCallContext: true);

            MachineInformation = new UnitTestMachineInformation();

            HttpCallContext callContext = CallContextManagerInstance.CallContextHandler(MachineInformation) as HttpCallContext;

            callContext.ExistingCallContext();
            callContext.StartCallContext();
        }
        public void GetTimedScopeStack_ShouldReturnNullValue()
        {
            IMachineInformation        machineInformation = new UnitTestMachineInformation();
            Mock <ICallContextManager> callContextManager = new Mock <ICallContextManager>();
            ICallContext callContext = null;

            callContextManager.SetupGet(mock => mock.CallContextOverride).Returns(callContext);
            callContextManager.Setup(mock => mock.CallContextHandler(It.IsAny <IMachineInformation>())).Returns(callContext);

            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManager.Object, machineInformation);

            Assert.Null(timedScopeStackManager.Scopes);

            callContextManager.Verify(x => x.CallContextHandler(machineInformation), Times.Once);
        }
        public void GetTimedScopeStack_ShouldReturnValue()
        {
            IMachineInformation        machineInformation = new UnitTestMachineInformation();
            Mock <ICallContextManager> callContextManager = new Mock <ICallContextManager>();
            Mock <ICallContext>        callContext        = new Mock <ICallContext>();

            callContext.Setup(mock => mock.Data).Returns(new ConcurrentDictionary <string, object>(StringComparer.Ordinal));
            callContextManager.SetupGet(mock => mock.CallContextOverride).Returns(callContext.Object);
            callContextManager.Setup(mock => mock.CallContextHandler(It.IsAny <IMachineInformation>())).Returns(callContext.Object);

            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManager.Object, machineInformation);

            Assert.NotNull(timedScopeStackManager.Scopes);

            callContextManager.Verify(x => x.CallContextHandler(machineInformation), Times.Once);
            callContext.Verify(x => x.Data, Times.AtLeastOnce);
        }
Beispiel #11
0
        public void Create_ShouldConstructTimedScope_WithTimerInactive()
        {
            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateTestCountersUnitTestTimedScope(true, false, machineInformation, timedScopeLoggerMock.Object,
                                                                                     replyEventConfiguratorMock.Object, timedScopeStackManager))
            {
                Assert.False(scope.IsScopeActive, "Creating a scope should not start the timer.");

                Assert.True(scope.IsSuccessful.HasValue, "IsSuccessful should be set.");
                Assert.True(scope.IsSuccessful.Value, "IsSuccessful should be set to true.");
            }
        }
Beispiel #12
0
        public void AbortTimer_ShouldDisableTimerActive()
        {
            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateDefaultTimedScope(timedScopeLoggerMock.Object, replyEventConfiguratorMock.Object, machineInformation: machineInformation,
                                                                        timedScopeStackManager: timedScopeStackManager))
            {
                Assert.True(scope.IsScopeActive, "Default scope should have timer active.");

                scope.AbortTimer();
                Assert.False(scope.IsScopeActive, "Aborting timer should stop timer.");
            }
        }
Beispiel #13
0
        public void AddLoggingValue_WithNullKey_ShouldLogError()
        {
            FailOnErrors = false;

            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateDefaultTimedScope(timedScopeLoggerMock.Object, replyEventConfiguratorMock.Object, machineInformation, timedScopeStackManager))
            {
                scope.AddLoggingValue(null, "My Application.");

                Assert.Equal(TraceErrors.Count(), 1);
                LoggedEvents.Clear();
            }
        }
Beispiel #14
0
        public void Start_DisposedTimedScope_ShoudLogError()
        {
            FailOnErrors = false;

            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            TimedScope scope = TestHooks.CreateDefaultTimedScope(timedScopeLoggerMock.Object, replyEventConfiguratorMock.Object, machineInformation, timedScopeStackManager);

            scope.Dispose();

            scope.Start();

            Assert.Equal(TraceErrors.Count(), 1);
            LoggedEvents.Clear();
        }
Beispiel #15
0
        public void Scope_TimedScopeLogger_IsCalled()
        {
            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            TimedScope      scope;
            CorrelationData data = new CorrelationData();

            using (scope = TestHooks.CreateTimedScopeProvider(machineInformation, timedScopeLoggerMock.Object, replyEventConfiguratorMock.Object, timedScopeStackManager)
                           .Create(new TimedScopeDefinition("TestScope"), TimedScopeResult.SystemError))
            {
                timedScopeLoggerMock.Verify(x => x.LogScopeStart(scope), Times.Once);
                timedScopeLoggerMock.Verify(x => x.LogScopeEnd(scope, It.IsAny <CorrelationData>()), Times.Never);
            }

            timedScopeLoggerMock.Verify(x => x.LogScopeEnd(scope, It.IsAny <CorrelationData>()), Times.Once);
        }
Beispiel #16
0
        public void SucceededScope_Result_ShouldOutputValueInLogEvent()
        {
            UnitTestTimedScopeLogger        unitTestTimedScopeLogger   = new UnitTestTimedScopeLogger();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateTestCountersUnitTestTimedScope(machineInformation: machineInformation, scopeLogger: unitTestTimedScopeLogger,
                                                                                     replayEventConfigurator: replyEventConfiguratorMock.Object, timedScopeStackManager: timedScopeStackManager))
            {
                scope.Result = TimedScopeResult.Success;
            }

            TimedScopeLogEvent scopeEvent = unitTestTimedScopeLogger.Events.SingleOrDefault();

            if (VerifyNotNullAndReturn(scopeEvent, "Timed scope should be logged"))
            {
                Assert.Equal(scopeEvent.Result, TimedScopeResult.Success);
            }
        }
Beispiel #17
0
        public void NotSettingTimedScopeResult_ChangesToSystemError()
        {
            LoggedEvents.Clear();

            UnitTestTimedScopeLogger        unitTestTimedScopeLogger   = new UnitTestTimedScopeLogger();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateDefaultTimedScope(unitTestTimedScopeLogger, replyEventConfiguratorMock.Object, machineInformation, timedScopeStackManager))
            {
            }

            TimedScopeLogEvent evt = unitTestTimedScopeLogger.SingleTimedScopeEvent(TestHooks.DefaultTimedScopeName);

            if (VerifyNotNullAndReturn(evt, "A scope event has been logged"))
            {
                Assert.Equal(evt.Result, TimedScopeResult.SystemError);
            }
        }
Beispiel #18
0
        public void AbortTimer_ShouldDisableTimerActive_AndSetResultsToFalse()
        {
            Mock <ITimedScopeLogger>        timedScopeLoggerMock       = new Mock <ITimedScopeLogger>();
            Mock <IReplayEventConfigurator> replyEventConfiguratorMock = new Mock <IReplayEventConfigurator>();
            Mock <ICallContextManager>      callContextManagerMock     = new Mock <ICallContextManager>();

            IMachineInformation     machineInformation     = new UnitTestMachineInformation();
            ITimedScopeStackManager timedScopeStackManager = new TimedScopeStackManager(callContextManagerMock.Object, machineInformation);

            using (TimedScope scope = TestHooks.CreateDefaultTimedScope(scopeLogger: timedScopeLoggerMock.Object, replayEventConfigurator: replyEventConfiguratorMock.Object,
                                                                        machineInformation: machineInformation, timedScopeStackManager: timedScopeStackManager, startScope: false))
            {
                Assert.False(scope.IsScopeActive, "Default scope started without an active scope should have timer active.");

                scope.Start();

                Assert.True(scope.IsScopeActive, "Default scope should have timer active.");

                scope.AbortTimer(false);
                Assert.False(scope.IsScopeActive, "Aborting timer should stop timer.");
                Assert.True(scope.IsSuccessful.HasValue, "IsSuccessful should be set.");
                Assert.False(scope.IsSuccessful.Value, "IsSuccesful should be set to false.");
            }
        }