Esempio n. 1
0
        public async Task EventHub_MultipleDispatch_IndependentMessages()
        {
            // send individual messages via EventHub client, process batch by host
            await using var ehClient = new EventHubProducerClient(EventHubsTestEnvironment.Instance.EventHubsConnectionString, _eventHubScope.EventHubName);

            var messages      = new EventData[5];
            var expectedLinks = new TestLink[messages.Length];

            for (int i = 0; i < messages.Length; i++)
            {
                var operationId = ActivityTraceId.CreateRandom().ToHexString();
                var spanId      = ActivitySpanId.CreateRandom().ToHexString();
                expectedLinks[i] = new TestLink
                {
                    operation_Id = operationId,
                    id           = spanId
                };

                messages[i] = new EventData(Encoding.UTF8.GetBytes(i.ToString()))
                {
                    Properties = { ["Diagnostic-Id"] = $"00-{operationId}-{spanId}-01" }
                };
            }

            await ehClient.SendAsync(messages);

            var(jobHost, host) = BuildHost <EventHubTestMultipleDispatchJobs>();
            using (host)
            {
                bool result = _eventWait.WaitOne(Timeout);
                Assert.True(result);
            }

            List <RequestTelemetry> requests = _channel.Telemetries.OfType <RequestTelemetry>().ToList();

            var ehTriggerRequests = requests.Where(r => r.Context.Operation.Name == nameof(EventHubTestMultipleDispatchJobs.ProcessMultipleEvents));

            List <TestLink> actualLinks = new List <TestLink>();

            foreach (var ehTriggerRequest in ehTriggerRequests)
            {
                ValidateEventHubRequest(
                    ehTriggerRequest,
                    true,
                    EventHubsTestEnvironment.Instance.FullyQualifiedNamespace,
                    _eventHubScope.EventHubName,
                    nameof(EventHubTestMultipleDispatchJobs.ProcessMultipleEvents),
                    null,
                    null);

                Assert.NotNull(ehTriggerRequest.Context.Operation.Id);
                Assert.Null(ehTriggerRequest.Context.Operation.ParentId);
                Assert.True(ehTriggerRequest.Properties.TryGetValue("_MS.links", out var linksStr));
                actualLinks.AddRange(JsonConvert.DeserializeObject <TestLink[]>(linksStr, jsonSettingThrowOnError));
            }

            Assert.AreEqual(expectedLinks.Length, actualLinks.Count);
            foreach (var link in actualLinks)
            {
                Assert.True(expectedLinks.Any(l => l.operation_Id == link.operation_Id && l.id == link.id));
            }
        }