コード例 #1
0
        public async Task CreateTriggerMetrics_ReturnsExpectedResult()
        {
            EventHubsConnectionStringBuilder sb = new EventHubsConnectionStringBuilder(_eventHubConnectionString);
            string prefix = $"{sb.Endpoint.Host}/{_eventHubName.ToLower()}/{_consumerGroup}/checkpoint/0";

            var mockBlobReference = new Mock <BlobClient>(MockBehavior.Strict);
            var sequence          = mockBlobReference.SetupSequence(m => m.GetPropertiesAsync(IsAny <BlobRequestConditions>(), IsAny <CancellationToken>()));

            _mockBlobContainer
            .Setup(c => c.GetBlobClient(prefix))
            .Returns(mockBlobReference.Object);

            SetupBlobMock(sequence, 0, 0);

            var partitionInfo = new List <PartitionProperties>
            {
                new TestPartitionProperties(lastSequenceNumber: 0)
            };

            var metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.AreEqual(0, metrics.EventCount);
            Assert.AreEqual(1, metrics.PartitionCount);
            Assert.AreNotEqual(default(DateTime), metrics.Timestamp);

            // Partition got its first message (Offset == null, LastEnqueued == 0)
            SetupBlobMock(sequence, null, 0);

            metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.AreEqual(1, metrics.EventCount);
            Assert.AreEqual(1, metrics.PartitionCount);
            Assert.AreNotEqual(default(DateTime), metrics.Timestamp);

            // No instances assigned to process events on partition (Offset == null, LastEnqueued > 0)
            SetupBlobMock(sequence, null, 0);

            partitionInfo = new List <PartitionProperties>
            {
                new TestPartitionProperties(lastSequenceNumber: 5)
            };

            metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.AreEqual(6, metrics.EventCount);
            Assert.AreEqual(1, metrics.PartitionCount);
            Assert.AreNotEqual(default(DateTime), metrics.Timestamp);

            // Checkpointing is ahead of partition info (SequenceNumber > LastEnqueued)
            SetupBlobMock(sequence, 25, 11);

            partitionInfo = new List <PartitionProperties>
            {
                new TestPartitionProperties(lastSequenceNumber: 10)
            };

            metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.AreEqual(0, metrics.EventCount);
            Assert.AreEqual(1, metrics.PartitionCount);
            Assert.AreNotEqual(default(DateTime), metrics.Timestamp);
        }
        public async void CreateTriggerMetrics_ReturnsExpectedResult()
        {
            EventHubsConnectionStringBuilder sb = new EventHubsConnectionStringBuilder(_eventHubConnectionString);
            string prefix = $"{sb.Endpoint.Host}/{_eventHubName.ToLower()}/{_consumerGroup}/0";

            var mockBlobReference = new Mock <CloudBlockBlob>(MockBehavior.Strict, new Uri(_storageUri, $"{_eventHubContainerName}/{prefix}"));

            _mockBlobContainer
            .Setup(c => c.GetBlockBlobReference(prefix))
            .Returns(mockBlobReference.Object);

            // No messages processed, no messages in queue
            mockBlobReference
            .Setup(m => m.DownloadTextAsync())
            .Returns(Task.FromResult("{ offset: 0, sequencenumber: 0 }"));

            var partitionInfo = new List <EventHubPartitionRuntimeInformation>
            {
                new EventHubPartitionRuntimeInformation {
                    LastEnqueuedSequenceNumber = 0
                }
            };

            var metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.Equal(0, metrics.EventCount);
            Assert.Equal(1, metrics.PartitionCount);
            Assert.NotEqual(default(DateTime), metrics.Timestamp);

            // Partition got its first message (Offset == null, LastEnqueued == 0)
            mockBlobReference
            .Setup(m => m.DownloadTextAsync())
            .Returns(Task.FromResult("{ sequencenumber: 0 }"));

            metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.Equal(1, metrics.EventCount);
            Assert.Equal(1, metrics.PartitionCount);
            Assert.NotEqual(default(DateTime), metrics.Timestamp);

            // No instances assigned to process events on partition (Offset == null, LastEnqueued > 0)
            mockBlobReference
            .Setup(m => m.DownloadTextAsync())
            .Returns(Task.FromResult("{ sequencenumber: 0 }"));

            partitionInfo = new List <EventHubPartitionRuntimeInformation>
            {
                new EventHubPartitionRuntimeInformation {
                    LastEnqueuedSequenceNumber = 5
                }
            };

            metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.Equal(6, metrics.EventCount);
            Assert.Equal(1, metrics.PartitionCount);
            Assert.NotEqual(default(DateTime), metrics.Timestamp);

            // Checkpointing is ahead of partition info (SequenceNumber > LastEnqueued)
            mockBlobReference
            .Setup(m => m.DownloadTextAsync())
            .Returns(Task.FromResult("{ offset: 25, sequencenumber: 11 }"));

            partitionInfo = new List <EventHubPartitionRuntimeInformation>
            {
                new EventHubPartitionRuntimeInformation {
                    LastEnqueuedSequenceNumber = 10
                }
            };

            metrics = await _scaleMonitor.CreateTriggerMetrics(partitionInfo);

            Assert.Equal(0, metrics.EventCount);
            Assert.Equal(1, metrics.PartitionCount);
            Assert.NotEqual(default(DateTime), metrics.Timestamp);
        }