示例#1
0
        public async Task WhenUnableToCheckpointWithExceptionThenThrows()
        {
            // Arrange
            var context = MockPartitionContext.Create("0", () =>
            {
                throw new InvalidOperationException();
            });

            _writerMock
            .Setup(w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()))
            .Returns(() => TaskHelpers.CreateCompletedTask(true));

            await _processor.OpenAsync(context);

            // Act
            await AssertExt.ThrowsAsync <InvalidOperationException>(() => _processor.ProcessEventsAsync(context, new[]
            {
                CreateEventData((byte)'a', 100),
                CreateEventData((byte)'b', 200),
                CreateEventData((byte)'c', 300),
                CreateEventData((byte)'d', 400),
            }));

            // Assert
            _loggerMock.Verify(l => l.Warning(It.IsAny <Exception>(), It.IsAny <string>(), It.IsAny <object[]>()), Times.Never);
        }
示例#2
0
        public async Task WhenUnableToCheckpointWithStorageExceptionThenLogs()
        {
            // Arrange
            var context = MockPartitionContext.Create("0", () =>
            {
                throw new StorageException();
            });

            var processor =
                new ColdStorageProcessor(
                    n => _writerMock.Object,
                    Mock.Of <IColdStorageInstrumentationPublisher>(),
                    CancellationToken.None,
                    CircuitBreakerWarningLevel,
                    CircuitBreakerStallLevel,
                    CircuitBreakerStallInterval,
                    TimeSpan.FromSeconds(200),
                    "Test",
                    maxBlockSize: MaxBlockSize);

            _writerMock
            .Setup(w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()))
            .Returns(() => TaskHelpers.CreateCompletedTask(true));

            await processor.OpenAsync(context);

            // Act
            await processor.ProcessEventsAsync(context, new[]
            {
                CreateEventData((byte)'a', 100),
                CreateEventData((byte)'b', 200),
                CreateEventData((byte)'c', 300),
                CreateEventData((byte)'d', 400),
            });
        }
示例#3
0
        public async Task WhenClosingForLostLeaseWithEmptyBuffer_ThenDoesThrow()
        {
            var operationQueue = new List <string>();

            Func <Task> flagCheckpointOperation = () =>
            {
                operationQueue.Add(checkpoint);
                return(Task.FromResult(true));
            };

            IList <BlockData> blocks = null;
            Action <IReadOnlyCollection <BlockData>, CancellationToken> getBlocks = (d, ct) => blocks = d.ToList();

            var context = MockPartitionContext.Create("0", flagCheckpointOperation);
            await _processor.OpenAsync(context);

            _writerMock
            .Setup(w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Callback(getBlocks);

            await _processor.CloseAsync(context, CloseReason.LeaseLost);

            _writerMock.Verify(
                w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()),
                Times.Never());

            // Assert checkpoint is never called
            Assert.Empty(operationQueue);
        }
示例#4
0
        public async Task WhenReceivesNullEvents_ThenDoesNotFlushEmptyBuffer()
        {
            var operationQueue       = new List <string>();
            IList <BlockData> blocks = null;
            Action <IReadOnlyCollection <BlockData>, CancellationToken> flagWriteOperation = (d, ct) =>
            {
                operationQueue.Add(write);
                blocks = d.ToList();
            };

            Func <Task> flagCheckpointOperation = () =>
            {
                operationQueue.Add(checkpoint);
                return(Task.FromResult(true));
            };

            var context = MockPartitionContext.Create("0", flagCheckpointOperation);
            await _processor.OpenAsync(context);

            _writerMock
            .Setup(w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Callback(flagWriteOperation);

            await _processor.ProcessEventsAsync(context, null);

            _writerMock.Verify(
                w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()),
                Times.Never());

            // Assert checkpoint is never called
            Assert.Empty(operationQueue);
        }
示例#5
0
        public async Task WhenIncomingMessagesFillABlock_ThenWritesFilledBlock()
        {
            // Arrange
            var operationQueue       = new List <string>();
            IList <BlockData> blocks = null;
            Action <IReadOnlyCollection <BlockData>, CancellationToken> flagWriteOperation = (d, ct) =>
            {
                operationQueue.Add(write);
                blocks = d.ToList();
            };

            Func <Task> flagCheckpointOperation = () =>
            {
                operationQueue.Add(checkpoint);
                return(Task.FromResult(true));
            };

            var context = MockPartitionContext.Create("0", flagCheckpointOperation);
            await _processor.OpenAsync(context);

            _writerMock
            .Setup(w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(true))
            .Callback(flagWriteOperation);

            // Act
            await _processor.ProcessEventsAsync(
                context,
                new[]
            {
                CreateEventData((byte)'a', 100),
                CreateEventData((byte)'b', 200),
                CreateEventData((byte)'c', 300),
                CreateEventData((byte)'d', 400),
            });

            Assert.NotNull(blocks);
            Assert.Equal(1, blocks.Count);

            var serializedFrame = Encoding.UTF8.GetString(blocks[0].Frame, 0, blocks[0].FrameLength);
            var lines           = serializedFrame.Split(new[] { ColdStorageProcessor.EventDelimiter }, StringSplitOptions.RemoveEmptyEntries);

            Assert.Equal(3, lines.Length);

            Assert.Equal(new string('a', 100), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[0])).Payload);
            Assert.Equal(new string('b', 200), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[1])).Payload);
            Assert.Equal(new string('c', 300), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[2])).Payload);

            // Assert checkpoint happens after write
            Assert.Equal(write, operationQueue.FirstOrDefault());
            Assert.Equal(checkpoint, operationQueue.LastOrDefault());
        }
示例#6
0
        public async Task WhenWritingFullBlockFails_ThenDoesNotThrow()
        {
            // Arrange
            var operationQueue = new List <string>();

            Func <Task> flagCheckpointOperation = () =>
            {
                operationQueue.Add(checkpoint);
                return(Task.FromResult(true));
            };

            IList <BlockData> blocks = null;
            Action <IReadOnlyCollection <BlockData>, CancellationToken> getBlocks = (d, ct) => blocks = d.ToList();

            var context = MockPartitionContext.Create("0", flagCheckpointOperation);
            await _processor.OpenAsync(context);

            _writerMock
            .Setup(w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()))
            .Returns(() => TaskHelpers.CreateCompletedTask(false))
            .Callback(getBlocks);

            // Act
            await _processor.ProcessEventsAsync(
                context,
                new[]
            {
                CreateEventData((byte)'a', 100),
                CreateEventData((byte)'b', 200),
                CreateEventData((byte)'c', 300),
                CreateEventData((byte)'d', 400),
            });

            // Assert
            Assert.NotNull(blocks);
            Assert.Equal(1, blocks.Count);
            Assert.False(operationQueue.Contains(checkpoint));
        }
        public void DoesNotThrowWhenCheckpointFailsWithLeaseLostExceptionTypes()
        {
            testDomain.DoCallBack(() =>
            {
                // Arrange
                var mockLogger = new Mock <ILogger>();
                LoggerFactory.Register(Mock.Of <ILogFactory>(f => f.Create(It.IsAny <string>()) == mockLogger.Object));
                var mockCircuitBreaker = new MockCircuitBreaker();
                var mockResolver       = new MockMessageHandlerResolver();

                var processor = new EventProcessor(mockResolver, mockCircuitBreaker, MaxConcurrency, EventHubName, Mock.Of <IDispatcherInstrumentationPublisher>());
                Func <Task> faultedCheckpoint = () =>
                {
                    throw new LeaseLostException();
                };

                var context = MockPartitionContext.Create(PartitionId, faultedCheckpoint);
                var events  = new[] { new EventData() };

                // Act & Assert
                processor.ProcessEventsAsync(context, events).Wait();
                mockLogger.Verify(l => l.Warning(It.IsAny <Exception>(), It.IsAny <string>(), It.IsAny <object[]>()), Times.Once);
            });
        }
示例#8
0
        public async Task WhenWritingFullBlockFails_ThenReattemptsOnNextFilledBlock()
        {
            // Arrange
            var         operationQueue          = new List <string>();
            Func <Task> flagCheckpointOperation = () =>
            {
                operationQueue.Add(checkpoint);
                return(Task.FromResult(true));
            };

            IList <BlockData> blocks = null;
            Action <IReadOnlyCollection <BlockData>, CancellationToken> getBlocks = (d, ct) => blocks = d.ToList();

            var context = MockPartitionContext.Create("0", flagCheckpointOperation);
            await _processor.OpenAsync(context);

            _writerMock
            .Setup(w => w.WriteAsync(It.IsAny <IReadOnlyCollection <BlockData> >(), It.IsAny <CancellationToken>()))
            .Returns(() => TaskHelpers.CreateCompletedTask(false))
            .Callback(getBlocks);

            // Act
            await _processor.ProcessEventsAsync(
                context,
                new[]
            {
                CreateEventData((byte)'a', 100),
                CreateEventData((byte)'b', 200),
                CreateEventData((byte)'c', 300),
                CreateEventData((byte)'d', 400),
            });

            Assert.NotNull(blocks);
            Assert.Equal(1, blocks.Count);
            Assert.False(operationQueue.Contains(checkpoint));

            await _processor.ProcessEventsAsync(
                context,
                new[]
            {
                CreateEventData((byte)'e', 300),
                CreateEventData((byte)'f', 500),
            });

            Assert.NotNull(blocks);
            Assert.Equal(2, blocks.Count);

            var serializedFrame = Encoding.UTF8.GetString(blocks[0].Frame, 0, blocks[0].FrameLength);
            var lines           = serializedFrame.Split(new[] { ColdStorageProcessor.EventDelimiter }, StringSplitOptions.RemoveEmptyEntries);

            Assert.Equal(3, lines.Length);

            Assert.Equal(new string('a', 100), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[0])).Payload);
            Assert.Equal(new string('b', 200), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[1])).Payload);
            Assert.Equal(new string('c', 300), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[2])).Payload);

            serializedFrame = Encoding.UTF8.GetString(blocks[1].Frame, 0, blocks[1].FrameLength);
            lines           = serializedFrame.Split(new[] { ColdStorageProcessor.EventDelimiter }, StringSplitOptions.RemoveEmptyEntries);

            Assert.Equal(2, lines.Length);

            Assert.Equal(new string('d', 400), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[0])).Payload);
            Assert.Equal(new string('e', 300), (JsonConvert.DeserializeObject <ColdStorageEvent>(lines[1])).Payload);
        }