Exemple #1
0
        public async Task PartitionPumpCreatesScopeForEventProcessing()
        {
            using ClientDiagnosticListener listener = new ClientDiagnosticListener(DiagnosticSourceName);
            var  processorCalledSource = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
            var  consumerMock          = new Mock <EventHubConsumer>();
            bool returnedItems         = false;

            consumerMock.Setup(c => c.ReceiveAsync(It.IsAny <int>(), It.IsAny <TimeSpan?>(), It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                if (returnedItems)
                {
                    throw new InvalidOperationException("Something bad happened");
                }

                returnedItems = true;
                return(Task.FromResult(
                           (IEnumerable <EventData>) new[]
                {
                    new EventData(Array.Empty <byte>())
                    {
                        Properties =
                        {
                            { "Diagnostic-Id", "id" }
                        }
                    },
                    new EventData(Array.Empty <byte>())
                    {
                        Properties =
                        {
                            { "Diagnostic-Id", "id2" }
                        }
                    }
                }));
            });

            var clientMock = new Mock <EventHubClient>();

            clientMock.Setup(c => c.CreateConsumer("cg", "pid", It.IsAny <EventPosition>(), It.IsAny <EventHubConsumerOptions>())).Returns(consumerMock.Object);

            var processorMock = new Mock <BasePartitionProcessor>();

            processorMock.Setup(p => p.InitializeAsync(It.IsAny <PartitionContext>())).Returns(Task.CompletedTask);
            processorMock.Setup(p => p.ProcessEventsAsync(It.IsAny <PartitionContext>(), It.IsAny <IEnumerable <EventData> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask)
            .Callback(() => processorCalledSource.SetResult(null));

            var manager = new PartitionPump(clientMock.Object, "cg", new PartitionContext("ns", "eh", "cg", "pid", "oid", new InMemoryPartitionManager()), processorMock.Object, new EventProcessorOptions());

            await manager.StartAsync();

            await processorCalledSource.Task;
            await manager.StopAsync(null);

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorProcessingActivityName));
            Assert.That(scope.Links, Has.One.EqualTo("id"));
            Assert.That(scope.Links, Has.One.EqualTo("id2"));
            Assert.That(scope.Activity.Tags, Has.One.EqualTo(new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ServerKind)), "The activities tag should be server.");
        }
Exemple #2
0
        public async Task PartitionPumpCreatesScopeForEventProcessing()
        {
            using ClientDiagnosticListener listener = new ClientDiagnosticListener(DiagnosticSourceName);
            var  processorCalledSource = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
            var  consumerMock          = new Mock <TransportConsumer>();
            bool returnedItems         = false;

            consumerMock.Setup(c => c.ReceiveAsync(It.IsAny <int>(), It.IsAny <TimeSpan?>(), It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                if (returnedItems)
                {
                    throw new InvalidOperationException("Something bad happened");
                }

                returnedItems = true;
                return(Task.FromResult(
                           (IEnumerable <EventData>) new[]
                {
                    new EventData(Array.Empty <byte>())
                    {
                        Properties =
                        {
                            { "Diagnostic-Id", "id" }
                        }
                    },
                    new EventData(Array.Empty <byte>())
                    {
                        Properties =
                        {
                            { "Diagnostic-Id", "id2" }
                        }
                    }
                }));
            });

            var connectionMock = new Mock <EventHubConnection>("namespace", "eventHubName", Mock.Of <TokenCredential>(), new EventHubConnectionOptions());

            connectionMock.Setup(c => c.CreateTransportConsumer("cg", "pid", It.IsAny <EventPosition>(), It.IsAny <EventHubConsumerClientOptions>())).Returns(consumerMock.Object);

            Func <EventProcessorEvent, Task> processEventAsync = processorEvent =>
            {
                processorCalledSource.SetResult(null);
                return(Task.CompletedTask);
            };

            var updateCheckpointMock = Mock.Of <Func <EventData, PartitionContext, Task> >();
            var manager = new PartitionPump(connectionMock.Object, "cg", new PartitionContext("eventHubName", "pid"), EventPosition.Earliest, processEventAsync, updateCheckpointMock, new EventProcessorClientOptions());

            await manager.StartAsync();

            await processorCalledSource.Task;

            // TODO: figure out why an exception is being thrown. The problem has always existed, but now the Pump won't swallow exceptions
            // and throws them back to the caller.

            try
            {
                await manager.StopAsync();
            }
            catch (InvalidOperationException) { }

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorProcessingActivityName));
            Assert.That(scope.Links, Has.One.EqualTo("id"));
            Assert.That(scope.Links, Has.One.EqualTo("id2"));
            Assert.That(scope.Activity.Tags, Has.One.EqualTo(new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ServerKind)), "The activities tag should be server.");
        }