Esempio n. 1
0
        static void Main(string[] args)
        {
            var consoleLogger = new ConsoleLogger();
            var authorisationServiceWithDelay = new AuthorisationServiceWithDelay();
            var executionCountDecorator =
                new LogExecutionCountDecorator<IAuthorisationService>
                    (authorisationServiceWithDelay, consoleLogger, new TescoClock(), 10);
            var service =
                new LogAverageExecutionTimeDecorator<IAuthorisationService>
                    (executionCountDecorator, new TescoStopwatch(), consoleLogger, new TescoClock());

            for (int x = 0; x < 100; x++) //100 is a random number large enough to watch the "logging" in progress
            {
                service.Authorise(new AuthorisationRequest());
                Thread.Sleep(100);
            }

            Console.WriteLine("Example complete. Press any key to continue...");
            Console.ReadLine();
        }
        public void Authorise_GivenARunOf3Timings_LogsAverageExecutionTime(int timing1, 
            int timing2, int timing3, int expectedAverage)
        {
            //arrange
            var request = new AuthorisationRequest();
            var expectedMessage = string.Format(_logMessageFormat, _applicationNow.ToString("o"), expectedAverage, "3");
            _decorator = new LogAverageExecutionTimeDecorator<IAuthorisationService>(_decoratee,
                                                                                     _stopwatch,
                                                                                     _logger, _clock, 3);

            //act
            _stopwatch.Stub(s => s.ElapsedTime).Return(new TimeSpan(0, 0, 0, timing1)).Repeat.Once();
            _decorator.Authorise(request);
            _stopwatch.Stub(s => s.ElapsedTime).Return(new TimeSpan(0, 0, 0, timing2)).Repeat.Once();
            _decorator.Authorise(request);
            _stopwatch.Stub(s => s.ElapsedTime).Return(new TimeSpan(0, 0, 0, timing3)).Repeat.Once();
            _decorator.Authorise(request);

            //assert
            _logger.AssertWasCalled(d => d.Log(expectedMessage));
        }
 public void SetUp()
 {
     LogAverageExecutionTimeDecoratorForTest<IAuthorisationService>.ResetRequestCounter();
     _decoratee = MockRepository.GenerateMock<IAuthorisationService>();
     _stopwatch = MockRepository.GenerateMock<ITescoStopwatch>();
     _logger = MockRepository.GenerateMock<ILogger>();
     _clock = MockRepository.GenerateMock<IClock>();
     _clock.Stub(c => c.ApplicationNow).Return(_applicationNow);
     _decorator = new LogAverageExecutionTimeDecorator<IAuthorisationService>(_decoratee, _stopwatch, _logger, _clock, 1);
 }
        public void Authorise_SampleSizeSupplied_CallsLoggerCorrectNumberOfTimes(int numberOfRequests,
                                                                                int sampleSize,
                                                                                int expectedNumberOfCallsToLogger)
        {
            //arrange
            _decorator = new LogAverageExecutionTimeDecorator<IAuthorisationService>(_decoratee,
                                                                                     _stopwatch,
                                                                                     _logger,
                                                                                     _clock,
                                                                                     sampleSize);
            var request = new AuthorisationRequest();

            //act
            for (int x = 0; x < numberOfRequests; x++)
            {
                _decorator.Authorise(request);
            }

            //assert
            _logger.AssertWasCalled(l => l.Log(Arg<string>.Is.Anything),
                                    options => options.Repeat.Times(expectedNumberOfCallsToLogger));
        }
        public void Authorise_MultipleThreads_LogsExpectedNumberOfTimes()
        {
            //arrange
            var request = new AuthorisationRequest();
            var expectedMessage = string.Format(_logMessageFormat, _applicationNow.ToString("o"), 1, 100);
            _stopwatch.Stub(s => s.ElapsedTime).Return(new TimeSpan(0, 0, 0, 0, 1));
            _decorator = new LogAverageExecutionTimeDecorator<IAuthorisationService>(_decoratee,
                                                                                     _stopwatch,
                                                                                     _logger, _clock);

            var t1 = new Thread(() =>
            {
                for (int x = 0; x <= MaxSampleSize; x++)
                {
                    _decorator.Authorise(request);
                }
            });

            var t2 = new Thread(() =>
            {
                for (int x = 0; x <= MaxSampleSize; x++)
                {
                    _decorator.Authorise(request);
                }
            });

            var t3 = new Thread(() =>
            {
                for (int x = 0; x <= MaxSampleSize; x++)
                {
                    _decorator.Authorise(request);
                }
            });

            //act
            t1.Start();
            t2.Start();
            t3.Start();
            t1.Join();
            t2.Join();
            t3.Join();

            //assert
            _logger.AssertWasCalled(l => l.Log(expectedMessage),
                                    options => options.Repeat.Times(60));
        }
        public void Authorise_InvokedOverSampleSize_ContinuesLogging()
        {
            //arrange
            var request = new AuthorisationRequest();
            var expectedMessage = string.Format(_logMessageFormat, _applicationNow.ToString("o"), 1, MaxSampleSize);
            _stopwatch.Stub(s => s.ElapsedTime).Return(new TimeSpan(0, 0, 0, 0, 1));
            _decorator = new LogAverageExecutionTimeDecorator<IAuthorisationService>(_decoratee,
                _stopwatch, _logger, _clock, MaxSampleSize);

            //act
            for (int x = 0; x <= MaxSampleSize*3; x++)
            {
                _decorator.Authorise(request);
            }

            //assert
            _logger.AssertWasCalled(l => l.Log(expectedMessage),
                                    options => options.Repeat.Times(3));
        }