Exemplo n.º 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.");
        }
Exemplo n.º 2
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnSend()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName  = "SomeName";
            var endpoint      = new Uri("amqp://endpoint");
            var transportMock = new Mock <TransportEventHubProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var producer = new EventHubProducer(transportMock.Object, endpoint, eventHubName, new EventHubProducerOptions(), Mock.Of <EventHubRetryPolicy>());

            var eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            await producer.SendAsync(eventData);

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.TypeAttribute, DiagnosticProperty.EventHubProducerType),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint.ToString()));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName);

            Assert.That(eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity.Tags, Has.One.EqualTo(new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.InternalKind)), "The activities tag should be internal.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
        }
Exemplo n.º 3
0
        public async Task EventHubProducerLinksSendScopeToMessageScopesOnSend()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticSourceName);

            var fakeConnection = new MockConnection("some.endpoint.com", "SomeName");
            var transportMock  = new Mock <TransportProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            await producer.SendAsync(new[]
            {
                new EventData(ReadOnlyMemory <byte> .Empty, new Dictionary <string, object> {
                    { DiagnosticProperty.DiagnosticIdAttribute, "id" }
                }),
                new EventData(ReadOnlyMemory <byte> .Empty, new Dictionary <string, object> {
                    { DiagnosticProperty.DiagnosticIdAttribute, "id2" }
                })
            });

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.Scopes.Single(s => s.Name == DiagnosticProperty.ProducerActivityName);

            var expectedLinks = new List <string>()
            {
                "id", "id2"
            };
            var links = sendScope.Links.ToList();

            Assert.That(links.Count, Is.EqualTo(expectedLinks.Count), "The amount of links should be the same as the amount of events that were sent.");
            Assert.That(links, Is.EquivalentTo(expectedLinks), "The links should be identical to the diagnostic ids in the events that were sent.");
        }
        public async Task EventHubProducerLinksSendScopeToMessageScopesOnBatchSend()
        {
            using var testListener = new ClientDiagnosticListener(EventDataInstrumentation.DiagnosticNamespace);

            var writtenEventsData  = 0;
            var batchTransportMock = new Mock <TransportEventBatch>();
            var fakeConnection     = new MockConnection("some.endpoint.com", "SomeName");
            var transportMock      = new Mock <TransportProducer>();

            batchTransportMock
            .Setup(m => m.TryAdd(It.IsAny <EventData>()))
            .Returns <EventData>(e =>
            {
                var hasSpace = writtenEventsData <= 1;
                if (hasSpace)
                {
                    ++writtenEventsData;
                }
                return(hasSpace);
            });

            batchTransportMock
            .Setup(m => m.Count)
            .Returns(2);

            transportMock
            .Setup(m => m.CreateBatchAsync(It.IsAny <CreateBatchOptions>(), It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <TransportEventBatch>(Task.FromResult(batchTransportMock.Object)));

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            var eventData1 = new EventData(new BinaryData(ReadOnlyMemory <byte> .Empty), new Dictionary <string, object> {
                { DiagnosticProperty.DiagnosticIdAttribute, "id" }
            });
            var eventData2 = new EventData(new BinaryData(ReadOnlyMemory <byte> .Empty), new Dictionary <string, object> {
                { DiagnosticProperty.DiagnosticIdAttribute, "id2" }
            });
            var eventData3 = new EventData(new BinaryData(ReadOnlyMemory <byte> .Empty), new Dictionary <string, object> {
                { DiagnosticProperty.DiagnosticIdAttribute, "id3" }
            });

            EventDataBatch batch = await producer.CreateBatchAsync();

            Assert.That(batch.TryAdd(eventData1), Is.True, "The first event should have been added to the batch.");
            Assert.That(batch.TryAdd(eventData2), Is.True, "The second event should have been added to the batch.");
            Assert.That(batch.TryAdd(eventData3), Is.False, "The third event should not have been added to the batch.");

            await producer.SendAsync(batch);

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.Scopes.Single(s => s.Name == DiagnosticProperty.ProducerActivityName);

            var expectedLinks = new List <string>()
            {
                "id", "id2"
            };
            var links = sendScope.Links.ToList();

            Assert.That(links.Count, Is.EqualTo(expectedLinks.Count), "The amount of links should be the same as the amount of events that were sent.");
            Assert.That(links, Is.EquivalentTo(expectedLinks), "The links should be identical to the diagnostic ids in the events that were sent.");
        }
        public async Task CheckpointManagerCreatesScope()
        {
            using ClientDiagnosticListener listener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName = "SomeName";
            var endpoint     = new Uri("amqp://some.endpoint.com/path");
            Func <EventHubConnection> fakeFactory = () => new MockConnection(endpoint, eventHubName);
            var context = new MockPartitionContext("partition");
            var data    = new MockEventData(new byte[0], sequenceNumber: 0, offset: 0);

            var storageManager = new Mock <PartitionManager>();
            var eventProcessor = new Mock <EventProcessorClient>(Mock.Of <BlobContainerClient>(), "cg", endpoint.Host, eventHubName, fakeFactory, null);

            storageManager
            .Setup(manager => manager.UpdateCheckpointAsync(It.IsAny <Checkpoint>()))
            .Returns(Task.CompletedTask);

            eventProcessor
            .Setup(processor => processor.CreateStorageManager(It.IsAny <BlobContainerClient>()))
            .Returns(storageManager.Object);

            await eventProcessor.Object.UpdateCheckpointAsync(data, context);

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorCheckpointActivityName));
        }
        public async Task UpdateCheckpointAsyncCreatesScope()
        {
            using var cancellationSource = new CancellationTokenSource();
            cancellationSource.CancelAfter(TimeSpan.FromSeconds(15));

            var completionSource = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            var mockContext      = new Mock <PartitionContext>("65");
            var mockLogger       = new Mock <EventProcessorClientEventSource>();
            var mockProcessor    = new Mock <EventProcessorClient>(Mock.Of <StorageManager>(), "cg", "host", "hub", Mock.Of <TokenCredential>(), null);

            mockProcessor
            .Protected()
            .Setup <EventHubConnection>("CreateConnection")
            .Returns(Mock.Of <EventHubConnection>());

            mockLogger
            .Setup(log => log.UpdateCheckpointComplete(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .Callback(() => completionSource.TrySetResult(true));

            mockProcessor.Object.Logger = mockLogger.Object;

            using var listener = new ClientDiagnosticListener(EventDataInstrumentation.DiagnosticNamespace);
            await mockProcessor.Object.UpdateCheckpointAsync(new MockEventData(new byte[0], sequenceNumber : 65, offset : 998), mockContext.Object, default);

            await Task.WhenAny(completionSource.Task, Task.Delay(Timeout.Infinite, cancellationSource.Token));

            Assert.That(cancellationSource.IsCancellationRequested, Is.False, "The cancellation token should not have been signaled.");

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorCheckpointActivityName));

            cancellationSource.Cancel();
        }
Exemplo n.º 7
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnBatchSend()
        {
            using var testListener = new ClientDiagnosticListener(EventDataInstrumentation.DiagnosticNamespace);
            var activity = new Activity("SomeActivity").Start();

            var eventCount         = 0;
            var eventHubName       = "SomeName";
            var endpoint           = "endpoint";
            var batchEvent         = default(EventData);
            var fakeConnection     = new MockConnection(endpoint, eventHubName);
            var batchTransportMock = new Mock <TransportEventBatch>();


            batchTransportMock
            .Setup(m => m.TryAdd(It.IsAny <EventData>()))
            .Callback <EventData>(addedEvent => batchEvent = addedEvent)
            .Returns(() =>
            {
                eventCount++;
                return(eventCount <= 1);
            });

            var transportMock = new Mock <TransportProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            transportMock
            .Setup(m => m.CreateBatchAsync(It.IsAny <CreateBatchOptions>(), It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <TransportEventBatch>(Task.FromResult(batchTransportMock.Object)));

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            var eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            var batch     = await producer.CreateBatchAsync();

            Assert.True(batch.TryAdd(eventData));

            await producer.SendAsync(batch);

            activity.Stop();

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ClientKind),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName,
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            Assert.That(batchEvent.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
            Assert.That(sendScope.Activity.ParentId, Is.EqualTo(activity.Id), "The send scope's parent identifier should match the activity in the active scope.");
            Assert.That(messageScope.Activity.ParentId, Is.EqualTo(activity.Id), "The message scope's parent identifier should match the activity in the active scope.");
        }
Exemplo n.º 8
0
        public async Task CheckpointManagerCreatesScope()
        {
            using ClientDiagnosticListener listener = new ClientDiagnosticListener(DiagnosticSourceName);
            var manager = new PartitionContext("namespace", "name", "group", "partition", "owner", new InMemoryPartitionManager());

            await manager.UpdateCheckpointAsync(0, 0);

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorCheckpointActivityName));
        }
Exemplo n.º 9
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnBatchSend()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName       = "SomeName";
            var endpoint           = new Uri("amqp://endpoint");
            var fakeConnection     = new MockConnection(endpoint, eventHubName);
            var eventCount         = 0;
            var batchTransportMock = new Mock <TransportEventBatch>();

            batchTransportMock
            .Setup(m => m.TryAdd(It.IsAny <EventData>()))
            .Returns(() =>
            {
                eventCount++;
                return(eventCount <= 3);
            });

            var transportMock = new Mock <TransportProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            transportMock
            .Setup(m => m.CreateBatchAsync(It.IsAny <CreateBatchOptions>(), It.IsAny <CancellationToken>()))
            .Returns(new ValueTask <TransportEventBatch>(Task.FromResult(batchTransportMock.Object)));

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            var            eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            EventDataBatch batch     = await producer.CreateBatchAsync();

            Assert.True(batch.TryAdd(eventData));

            await producer.SendAsync(batch);

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.TypeAttribute, DiagnosticProperty.EventHubProducerType),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint.ToString()));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName);

            Assert.That(eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
        }
Exemplo n.º 10
0
        public async Task CheckpointManagerCreatesScope()
        {
            using ClientDiagnosticListener listener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName   = "SomeName";
            var endpoint       = new Uri("amqp://some.endpoint.com/path");
            var fakeConnection = new MockConnection(endpoint, eventHubName);
            var context        = new PartitionContext(eventHubName, "partition");
            var data           = new EventData(new byte[0], sequenceNumber: 0, offset: 0);

            var processor = new EventProcessorClient("cg", new InMemoryPartitionManager(), fakeConnection, null);

            await processor.UpdateCheckpointAsync(data, context);

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorCheckpointActivityName));
        }
Exemplo n.º 11
0
        public async Task CheckpointManagerCreatesScope()
        {
            using ClientDiagnosticListener listener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName = "SomeName";
            var endpoint     = new Uri("amqp://some.endpoint.com/path");
            Func <EventHubConnection> fakeFactory = () => new MockConnection(endpoint, eventHubName);
            var context = new PartitionContext("partition");
            var data    = new EventData(new byte[0], sequenceNumber: 0, offset: 0);

            var processor = new EventProcessorClient(new MockCheckPointStorage(), "cg", endpoint.Host, eventHubName, fakeFactory, null);

            // TODO: find a way to call UpdateCheckpointAsync.

            await Task.CompletedTask;

            // await processor.UpdateCheckpointAsync(data, context);

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorCheckpointActivityName));
        }
Exemplo n.º 12
0
        public async Task EventHubProducerCreatesDiagnosticScopeOnSend()
        {
            using var testListener = new ClientDiagnosticListener(DiagnosticSourceName);
            var activity = new Activity("SomeActivity").Start();

            var eventHubName   = "SomeName";
            var endpoint       = "endpoint";
            var fakeConnection = new MockConnection(endpoint, eventHubName);
            var transportMock  = new Mock <TransportProducer>();

            transportMock
            .Setup(m => m.SendAsync(It.IsAny <IEnumerable <EventData> >(), It.IsAny <SendEventOptions>(), It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var producer = new EventHubProducerClient(fakeConnection, transportMock.Object);

            var eventData = new EventData(ReadOnlyMemory <byte> .Empty);
            await producer.SendAsync(eventData);

            activity.Stop();

            ClientDiagnosticListener.ProducedDiagnosticScope sendScope = testListener.AssertScope(DiagnosticProperty.ProducerActivityName,
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ClientKind),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.ServiceContextAttribute, DiagnosticProperty.EventHubsServiceContext),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                  new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            ClientDiagnosticListener.ProducedDiagnosticScope messageScope = testListener.AssertScope(DiagnosticProperty.EventActivityName,
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EventHubAttribute, eventHubName),
                                                                                                     new KeyValuePair <string, string>(DiagnosticProperty.EndpointAttribute, endpoint));

            Assert.That(eventData.Properties[DiagnosticProperty.DiagnosticIdAttribute], Is.EqualTo(messageScope.Activity.Id), "The diagnostics identifier should match.");
            Assert.That(messageScope.Activity.Tags, Has.One.EqualTo(new KeyValuePair <string, string>(DiagnosticProperty.KindAttribute, DiagnosticProperty.ProducerKind)), "The activities tag should be internal.");
            Assert.That(messageScope.Activity, Is.Not.SameAs(sendScope.Activity), "The activities should not be the same instance.");
            Assert.That(sendScope.Activity.ParentId, Is.EqualTo(activity.Id), "The send scope's parent identifier should match the activity in the active scope.");
            Assert.That(messageScope.Activity.ParentId, Is.EqualTo(activity.Id), "The message scope's parent identifier should match the activity in the active scope.");
        }
        public async Task UpdateCheckpointAsyncCreatesScope()
        {
            using ClientDiagnosticListener listener = new ClientDiagnosticListener(DiagnosticSourceName);

            var eventHubName = "SomeName";
            var endpoint     = new Uri("amqp://some.endpoint.com/path");
            Func <EventHubConnection> fakeFactory = () => new MockConnection(endpoint, eventHubName);
            var context = new MockPartitionContext("partition");
            var data    = new MockEventData(new byte[0], sequenceNumber: 0, offset: 0);

            var storageManager = new Mock <PartitionManager>();
            var eventProcessor = new Mock <EventProcessorClient>(Mock.Of <PartitionManager>(), "cg", endpoint.Host, eventHubName, fakeFactory, null);

            // UpdateCheckpointAsync does not invoke the handlers, but we are setting them here in case
            // this fact changes in the future.

            eventProcessor.Object.ProcessEventAsync += eventArgs => Task.CompletedTask;
            eventProcessor.Object.ProcessErrorAsync += eventArgs => Task.CompletedTask;

            await eventProcessor.Object.UpdateCheckpointAsync(data, context, default);

            ClientDiagnosticListener.ProducedDiagnosticScope scope = listener.Scopes.Single();
            Assert.That(scope.Name, Is.EqualTo(DiagnosticProperty.EventProcessorCheckpointActivityName));
        }
Exemplo n.º 14
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 <EventHubsRetryPolicy>(), It.IsAny <bool>(), It.IsAny <long?>(), It.IsAny <uint?>())).Returns(consumerMock.Object);

            Func <ProcessEventArgs, ValueTask> processEventAsync = eventArgs =>
            {
                processorCalledSource.SetResult(null);
                return(new ValueTask());
            };

            // TODO: partition pump type does not exist anymore. Figure out how to call RunPartitionProcessingAsync.

            await Task.CompletedTask;

            /*
             *
             * var manager = new PartitionPump(connectionMock.Object, "cg", new PartitionContext("eventHubName", "pid"), EventPosition.Earliest, processEventAsync, 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.");
        }
Exemplo n.º 15
0
        public void Intercept(IInvocation invocation)
        {
            var methodName = invocation.Method.Name;

            if (methodName.EndsWith("Async"))
            {
                Type declaringType = invocation.Method.DeclaringType;
                var  ns            = declaringType.Namespace;
                var  expectedName  = declaringType.Name + "." + methodName.Substring(0, methodName.Length - 5);
                using ClientDiagnosticListener diagnosticListener = new ClientDiagnosticListener(s => s.StartsWith("Azure."), asyncLocal: true);
                invocation.Proceed();

                bool expectFailure = false;
                bool skipChecks    = false;

                bool strict = !invocation.Method.GetCustomAttributes(true).Any(a => a.GetType().FullName == "Azure.Core.ForwardsClientCallsAttribute");
                if (invocation.Method.ReturnType.Name.Contains("Pageable") ||
                    invocation.Method.ReturnType.Name.Contains("IAsyncEnumerable"))
                {
                    return;
                }

                try
                {
                    object returnValue = invocation.ReturnValue;
                    if (returnValue is Task t)
                    {
                        t.GetAwaiter().GetResult();
                    }
                    else
                    {
                        // Await ValueTask
                        Type       returnType       = returnValue.GetType();
                        MethodInfo getAwaiterMethod = returnType.GetMethod("GetAwaiter", BindingFlags.Instance | BindingFlags.Public);
                        MethodInfo getResultMethod  = getAwaiterMethod.ReturnType.GetMethod("GetResult", BindingFlags.Instance | BindingFlags.Public);

                        getResultMethod.Invoke(
                            getAwaiterMethod.Invoke(returnValue, Array.Empty <object>()),
                            Array.Empty <object>());
                    }
                }
                catch (Exception ex)
                {
                    expectFailure = true;

                    if (ex is ArgumentException)
                    {
                        // Don't expect scope for argument validation failures
                        skipChecks = true;
                    }
                }
                finally
                {
                    // Remove subscribers before enumerating events.
                    diagnosticListener.Dispose();
                    if (!skipChecks)
                    {
                        if (strict)
                        {
                            ClientDiagnosticListener.ProducedDiagnosticScope e = diagnosticListener.Scopes.FirstOrDefault(e => e.Name == expectedName);

                            if (e == default)
                            {
                                throw new InvalidOperationException($"Expected diagnostic scope not created {expectedName} {Environment.NewLine}    created scopes {string.Join(", ", diagnosticListener.Scopes)} {Environment.NewLine}    You may have forgotten to set your operationId to {expectedName} in {methodName} or applied the Azure.Core.ForwardsClientCallsAttribute to {methodName}.");
                            }

                            if (!e.Activity.Tags.Any(tag => tag.Key == "az.namespace"))
                            {
                                throw new InvalidOperationException($"All diagnostic scopes should have 'az.namespace' attribute, make sure the assembly containing **ClientOptions type is marked with the AzureResourceProviderNamespace attribute specifying the appropriate provider. This attribute should be included in AssemblyInfo, and can be included by pulling in AzureResourceProviderNamespaceAttribute.cs using the AzureCoreSharedSources alias.");
                            }

                            if (expectFailure && !e.IsFailed)
                            {
                                throw new InvalidOperationException($"Expected scope {expectedName} to be marked as failed but it succeeded");
                            }
                        }
                        else
                        {
                            if (!diagnosticListener.Scopes.Any())
                            {
                                throw new InvalidOperationException($"Expected some diagnostic scopes to be created, found none");
                            }
                        }
                    }
                }
            }
            else
            {
                invocation.Proceed();
            }
        }