public void Run_WhenCanceled_ExpectExits()
        {
            // Arrange
            var mockBuffer           = new Mock <IBuffer>();
            var mockStopwatchFactory = new Mock <IStopwatchFactory>();
            var mockDelayCalculator  = new Mock <IDelayCalculator>();

            var scheduleSettings = new ScheduleSettings
            {
                ThrottleTime = TimeSpan.FromMilliseconds(10)
            };

            var pumpProcessor = new PumpProcessor(
                this.LoggerFactory,
                mockBuffer.Object,
                mockStopwatchFactory.Object,
                mockDelayCalculator.Object,
                scheduleSettings);

            var cancellationTokenSource = new CancellationTokenSource();

            cancellationTokenSource.Cancel();

            // Act
            var stopwatch = Stopwatch.StartNew();

            pumpProcessor.Run(cancellationTokenSource.Token);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            mockBuffer.Verify(r => r.ProcessBufferAsync(It.IsAny <CancellationToken>()), Times.Never());
        }
        public void Run_WhenDataProcessed_ExpectBatchSubmittedEvent()
        {
            // Arrange
            var mockBuffer           = new Mock <IBuffer>();
            var mockStopwatchFactory = new Mock <IStopwatchFactory>();
            var mockStopwatch        = new Mock <IStopwatch>();
            var mockDelayCalculator  = new Mock <IDelayCalculator>();

            mockStopwatchFactory.Setup(r => r.StartNew()).Returns(mockStopwatch.Object);

            mockDelayCalculator.Setup(r => r.GetNextDelay(It.IsAny <TimeSpan>(), It.IsAny <TimeSpan>()))
            .Returns(TimeSpan.FromMilliseconds(100));

            mockBuffer.Setup(r => r.ProcessBufferAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(new BufferProcessResponse
            {
                Processed = 10,
                Remaining = 10
            });

            var scheduleSettings = new ScheduleSettings
            {
                ThrottleTime = TimeSpan.FromMilliseconds(10)
            };

            var pumpProcessor = new PumpProcessor(
                this.LoggerFactory,
                mockBuffer.Object,
                mockStopwatchFactory.Object,
                mockDelayCalculator.Object,
                scheduleSettings);

            // Act
            var stopwatch = Stopwatch.StartNew();

            pumpProcessor.Run(CancellationToken.None);
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            var manualResetEventSlim = new ManualResetEventSlim(false);

            pumpProcessor.BatchSubmitted += (sender, i) => manualResetEventSlim.Set();

            var wasTriggered = manualResetEventSlim.Wait(TimeSpan.FromMilliseconds(100));

            Assert.That(wasTriggered, Is.True);

            mockBuffer.Verify(r => r.ProcessBufferAsync(It.IsAny <CancellationToken>()), Times.AtLeastOnce());
        }
Exemple #3
0
        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <param name="queueConfiguration">The queue configuration.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        /// <returns>
        /// The <see cref="IQueue" />
        /// </returns>
        public static IQueue Create(
            QueueConfiguration queueConfiguration,
            ILoggerFactory loggerFactory = null)
        {
            var logger = loggerFactory ?? new LoggerFactory();

            var queueEndpoint = new QueueEndpoint
            {
                ConnectionString = queueConfiguration.QueueConnectionString,
                QueueName        = queueConfiguration.QueueName
            };

            var serializerSettings = new SerializerSettings
            {
                SerializerType = queueConfiguration.SerializerType
            };

            var batchSettings = new BatchSettings
            {
                MaxQueueMessagesPerSchedule = queueConfiguration.MaxQueueMessagesPerSchedule,
                MaxMessagesPerQueueMessage  = queueConfiguration.MaxMessagesPerQueueMessage
            };

            var scheduleSettings = new ScheduleSettings
            {
                ThrottleTime = queueConfiguration.ThrottleTime
            };

            var queueWrapper           = new QueueWrapper(queueEndpoint);
            var messageSerializer      = new MessageSerializer(serializerSettings);
            var queueMessageSerializer = new QueueMessageSerializer(batchSettings, messageSerializer);

            var buffer = new Buffer(logger, queueWrapper, queueMessageSerializer);

            var stopwatchFactory = new StopwatchFactory();
            var delayCalculator  = new DelayCalculator();
            var pumpProcessor    = new PumpProcessor(
                logger,
                buffer,
                stopwatchFactory,
                delayCalculator,
                scheduleSettings);

            var queuePump = new QueuePump(buffer, pumpProcessor);

            return(new Queue(queuePump));
        }
        public async Task Run_WhenRun_ExpectBufferProcessed()
        {
            // Arrange
            var mockBuffer           = new Mock <IBuffer>();
            var mockStopwatchFactory = new Mock <IStopwatchFactory>();
            var mockStopwatch        = new Mock <IStopwatch>();
            var mockDelayCalculator  = new Mock <IDelayCalculator>();

            mockStopwatchFactory.Setup(r => r.StartNew()).Returns(mockStopwatch.Object);

            mockBuffer.Setup(r => r.ProcessBufferAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync(new BufferProcessResponse());

            mockDelayCalculator.Setup(r => r.GetNextDelay(It.IsAny <TimeSpan>(), It.IsAny <TimeSpan>()))
            .Returns(TimeSpan.FromMilliseconds(100));

            var scheduleSettings = new ScheduleSettings
            {
                ThrottleTime = TimeSpan.FromMilliseconds(10)
            };

            var pumpProcessor = new PumpProcessor(
                this.LoggerFactory,
                mockBuffer.Object,
                mockStopwatchFactory.Object,
                mockDelayCalculator.Object,
                scheduleSettings);

            // Act
            var stopwatch = Stopwatch.StartNew();

            Assert.DoesNotThrow(() => pumpProcessor.Run(CancellationToken.None));
            stopwatch.Stop();

            // Assert
            this.WriteTimeElapsed(stopwatch);

            await Task.Delay(200).ConfigureAwait(false);

            mockBuffer.Verify(r => r.ProcessBufferAsync(It.IsAny <CancellationToken>()), Times.AtLeastOnce());
        }