Ejemplo n.º 1
0
        public async Task <IList <EventData> > GetMessagesFromAllPartitions(DateTime startTime, int maxPerPartition = 10, int waitTimeSecs = 5)
        {
            var messages = new List <EventData>();
            EventHubRuntimeInformation rtInfo = await this.eventHubClient.GetRuntimeInformationAsync();

            foreach (string partition in rtInfo.PartitionIds)
            {
                PartitionReceiver partitionReceiver = this.eventHubClient.CreateReceiver(
                    PartitionReceiver.DefaultConsumerGroupName,
                    partition,
                    startTime);

                // Retry a few times to make sure we get all expected messages.
                for (int i = 0; i < 3; i++)
                {
                    IEnumerable <EventData> events = await partitionReceiver.ReceiveAsync(maxPerPartition, TimeSpan.FromSeconds(waitTimeSecs));

                    if (events != null)
                    {
                        messages.AddRange(events);
                    }

                    if (i < 3)
                    {
                        await Task.Delay(TimeSpan.FromSeconds(5));
                    }
                }

                await partitionReceiver.CloseAsync();
            }

            return(messages);
        }
        public static void CreateListenerPalAndReceiveMessages()
        {
            var builder = new EventHubsConnectionStringBuilder(Configuration.IoTHub.EventHubString)
            {
                EntityPath = Configuration.IoTHub.EventHubCompatibleName
            };

            EventHubClient eventHubClient          = EventHubClient.CreateFromConnectionString(builder.ToString());
            var            eventRuntimeInformation = eventHubClient.GetRuntimeInformationAsync().Result;
            var            eventHubPartitionsCount = eventRuntimeInformation.PartitionCount;
            string         consumerGroupName       = Configuration.IoTHub.EventHubConsumerGroup;

            foreach (string partitionId in eventRuntimeInformation.PartitionIds)
            {
                try
                {
                    PartitionReceiver receiver = eventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, DateTime.Now.AddMinutes(-LookbackTimeInMinutes));
                    s_log.WriteLine($"EventHub receiver created for partition {partitionId}, listening from {LookbackTimeInMinutes}");

                    new Task(async() =>
                    {
                        while (true)
                        {
                            IEnumerable <EventData> eventDatas = await receiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(OperationTimeoutInSeconds)).ConfigureAwait(false);
                            ProcessEventData(eventDatas);
                        }
                    }).Start();
                }
                catch (EventHubsException ex)
                {
                    s_log.WriteLine($"{nameof(EventHubTestListener)}.{nameof(CreateListenerPalAndReceiveMessages)}: Cannot create receiver for partitionID {partitionId}: {ex}");
                }
            }
        }
Ejemplo n.º 3
0
        public async Task ReceiveFromInvalidPartition()
        {
            var ehClient = EventHubClient.CreateFromConnectionString(TestUtility.EventHubsConnectionString);
            PartitionReceiver receiver = null;

            try
            {
                // Some invalid partition values. These will fail on the service side.
                var invalidPartitions = new List <string>()
                {
                    "XYZ", "-1", "1000", "-"
                };
                foreach (var invalidPartitionId in invalidPartitions)
                {
                    await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
                    {
                        TestUtility.Log($"Receiving from invalid partition {invalidPartitionId}");
                        receiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, invalidPartitionId, EventPosition.FromStart());
                        await receiver.ReceiveAsync(1);
                    });

                    await receiver.CloseAsync();
                }

                // Some invalid partition values. These will fail on the client side.
                invalidPartitions = new List <string>()
                {
                    " ", null, ""
                };
                foreach (var invalidPartitionId in invalidPartitions)
                {
                    await Assert.ThrowsAsync <InvalidOperationException>(async() =>
                    {
                        TestUtility.Log($"Receiving from invalid partition {invalidPartitionId}");
                        receiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, invalidPartitionId, EventPosition.FromStart());
                        await receiver.ReceiveAsync(1);
                    });

                    await receiver.CloseAsync();
                }
            }
            finally
            {
                await ehClient.CloseAsync();
            }
        }
Ejemplo n.º 4
0
        private async Task ReceiverPumpAsync(PartitionReceiver receiver, Func <IReadOnlyCollection <QueueMessage>, Task> onMessage, int maxBatchSize, CancellationToken cancellationToken)
        {
            while (true)
            {
                try
                {
                    IEnumerable <EventData> events = await receiver.ReceiveAsync(maxBatchSize, _waitTime);

                    if (events != null && !cancellationToken.IsCancellationRequested)
                    {
                        List <QueueMessage> qms = events.Select(ed => Converter.ToQueueMessage(ed, receiver.PartitionId)).ToList();
                        await onMessage(qms);

                        QueueMessage lastMessage = qms.LastOrDefault();

                        //save state
                        if (lastMessage != null)
                        {
                            const string sequenceNumberPropertyName = "x-opt-sequence-number";

                            if (lastMessage.Properties.TryGetValue(sequenceNumberPropertyName, out string sequenceNumber))
                            {
                                long?sequenceNumberLong = null;
                                if (long.TryParse(sequenceNumber, out long seqenceNumberNonNullable))
                                {
                                    sequenceNumberLong = seqenceNumberNonNullable;
                                }

                                await _state.SetPartitionStateAsync(receiver.PartitionId, sequenceNumberLong);
                            }
                        }
                    }

                    if (cancellationToken.IsCancellationRequested)
                    {
                        await receiver.CloseAsync();

                        return;
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine("failed with message: '{0}', clearing partition state.", ex);

                    await _state.SetPartitionStateAsync(receiver.PartitionId, EventPosition.FromStart().SequenceNumber);
                }
                catch (OperationCanceledException)
                {
                    return;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("receiver stopped: {0}", ex);

                    return;
                }
            }
        }
Ejemplo n.º 5
0
        public void ReceiveAsyncValidatesTheMaximumWaitTime(int timeSpanDelta)
        {
            var transportConsumer = new ObservableTransportConsumerMock();
            var receiver          = new PartitionReceiver("group", "0", "hub", true, TimeSpan.Zero, transportConsumer);
            var expectedWaitTime  = TimeSpan.FromMilliseconds(timeSpanDelta);

            using var cancellation = new CancellationTokenSource();
            Assert.That(async() => await receiver.ReceiveAsync(32, expectedWaitTime, cancellation.Token), Throws.InstanceOf <ArgumentException>());
        }
Ejemplo n.º 6
0
        static async Task Receive(int number)
        {
            var sms = await _receiver.ReceiveAsync(number);

            foreach (EventData s in sms)
            {
                Console.WriteLine($"Event Read from ReceiveAsync: { FromByteArray<SmsContent>(s.Body.ToArray()) } - Partition key : {s.SystemProperties.PartitionKey}");
            }
        }
        async Task SendReceiveNonexistentEntity()
        {
            // Rebuild connection string with a nonexistent entity.
            var csb = new EventHubsConnectionStringBuilder(this.connectionString);

            csb.EntityPath = Guid.NewGuid().ToString();
            var ehClient = EventHubClient.CreateFromConnectionString(csb.ToString());

            // Try sending.
            PartitionSender sender = null;
            await Assert.ThrowsAsync <MessagingEntityNotFoundException>(async() =>
            {
                Log("Sending an event to nonexistent entity.");
                sender = ehClient.CreatePartitionSender("0");
                await sender.SendAsync(new EventData(Encoding.UTF8.GetBytes("this send should fail.")));
                throw new InvalidOperationException("Send should have failed");
            });

            await sender.CloseAsync();

            // Try receiving.
            PartitionReceiver receiver = null;
            await Assert.ThrowsAsync <MessagingEntityNotFoundException>(async() =>
            {
                Log("Receiving from nonexistent entity.");
                receiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", PartitionReceiver.StartOfStream);
                await receiver.ReceiveAsync(1);
                throw new InvalidOperationException("Receive should have failed");
            });

            await receiver.CloseAsync();

            // Try receiving on an nonexistent consumer group.
            ehClient = EventHubClient.CreateFromConnectionString(this.connectionString);
            await Assert.ThrowsAsync <MessagingEntityNotFoundException>(async() =>
            {
                Log("Receiving from nonexistent consumer group.");
                receiver = ehClient.CreateReceiver(Guid.NewGuid().ToString(), "0", PartitionReceiver.StartOfStream);
                await receiver.ReceiveAsync(1);
                throw new InvalidOperationException("Receive should have failed");
            });

            await receiver.CloseAsync();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Method created to connect and process the Topic/Subscription in the azure.
        /// </summary>
        /// <returns></returns>
        private void ProcessHub()
        {
            if (_topics.Count != 1)
            {
                throw new LightException($"EventHub implementation must have one Entity Model related!");
            }

            var e = _topics.GetEnumerator();

            e.MoveNext();
            var topic = e.Current;

            var connectionStringBuilder = new EventHubsConnectionStringBuilder(GetConnection(topic))
            {
                EntityPath = topic.Value.TopicName
            };

            EventHubClient          client         = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            PartitionReceiver       receiver       = client.CreateReceiver(topic.Value.Subscription, "0", EventPosition.FromStart(), null);
            IEnumerable <EventData> receivedEvents = receiver.ReceiveAsync(topic.Value.TakeQuantity).Result;

            try
            {
                while (true)
                {
                    if (receivedEvents != null)
                    {
                        foreach (EventData receivedEvent in receivedEvents)
                        {
                            LightWorker.InvokeProcess(topic.Key, receivedEvent.Body.ToArray());
                        }
                    }
                    receivedEvents = receiver.ReceiveAsync(topic.Value.TakeQuantity).Result;
                }
            }
            catch (Exception exception)
            {
                Exception moreInfo = new Exception($"Exception reading topic={topic.Value.TopicName} with subscription={topic.Value.Subscription} from event hub. See inner exception for details. Message={exception.Message}", exception);

                //Use the class instead of interface because tracking exceptions directly is not supposed to be done outside AMAW (i.e. by the business code)
                ((LightTelemetry)WorkBench.Telemetry).TrackException(moreInfo);
            }
        }
Ejemplo n.º 9
0
        // Send and receive given event on given partition.
        protected async Task <EventData> SendAndReceiveEventAsync(string partitionId, EventData sendEvent, EventHubClient client)
        {
            PartitionSender   partitionSender   = client.CreatePartitionSender(partitionId);
            PartitionReceiver partitionReceiver = client.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, EventPosition.FromEnqueuedTime(DateTime.UtcNow.AddMinutes(-10)));

            EventData receivedEvent = null;

            try
            {
                string uniqueEventId = Guid.NewGuid().ToString();
                TestUtility.Log($"Sending event to Partition {partitionId} with custom property EventId {uniqueEventId}");
                sendEvent.Properties["EventId"] = uniqueEventId;
                await partitionSender.SendAsync(sendEvent);

                bool expectedEventReceived = false;
                do
                {
                    IEnumerable <EventData> eventDatas = await partitionReceiver.ReceiveAsync(10);

                    if (eventDatas == null)
                    {
                        break;
                    }

                    TestUtility.Log($"Received a batch of {eventDatas.Count()} events:");
                    foreach (var eventData in eventDatas)
                    {
                        object objectValue;

                        if (eventData.Properties != null && eventData.Properties.TryGetValue("EventId", out objectValue))
                        {
                            TestUtility.Log($"Received message with EventId {objectValue}");
                            string receivedId = objectValue.ToString();
                            if (receivedId == uniqueEventId)
                            {
                                TestUtility.Log("Success");
                                receivedEvent         = eventData;
                                expectedEventReceived = true;
                                break;
                            }
                        }
                    }
                }while (!expectedEventReceived);

                Assert.True(expectedEventReceived, $"Did not receive expected event with EventId {uniqueEventId}");
            }
            finally
            {
                await Task.WhenAll(
                    partitionReceiver.CloseAsync(),
                    partitionSender.CloseAsync());
            }

            return(receivedEvent);
        }
Ejemplo n.º 10
0
        async Task PartitionReceiverReceive()
        {
            Log("Receiving Events via PartitionReceiver.ReceiveAsync");
            const string      partitionId       = "1";
            PartitionSender   partitionSender   = this.EventHubClient.CreatePartitionSender(partitionId);
            PartitionReceiver partitionReceiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, DateTime.UtcNow.AddMinutes(-10));

            try
            {
                string uniqueEventId = Guid.NewGuid().ToString();
                Log($"Sending an event to Partition {partitionId} with custom property EventId {uniqueEventId}");
                var sendEvent = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));
                sendEvent.Properties = new Dictionary <string, object> {
                    ["EventId"] = uniqueEventId
                };
                await partitionSender.SendAsync(sendEvent);

                bool expectedEventReceived = false;
                do
                {
                    IEnumerable <EventData> eventDatas = await partitionReceiver.ReceiveAsync(10);

                    if (eventDatas == null)
                    {
                        break;
                    }

                    Log($"Received a batch of {eventDatas.Count()} events:");
                    foreach (var eventData in eventDatas)
                    {
                        object objectValue;
                        if (eventData.Properties != null && eventData.Properties.TryGetValue("EventId", out objectValue))
                        {
                            Log($"Received message with EventId {objectValue}");
                            string receivedId = objectValue.ToString();
                            if (receivedId == uniqueEventId)
                            {
                                Log("Success");
                                expectedEventReceived = true;
                                break;
                            }
                        }
                    }
                }while (!expectedEventReceived);

                Assert.True(expectedEventReceived, $"Did not receive expected event with EventId {uniqueEventId}");
            }
            finally
            {
                await Task.WhenAll(
                    partitionReceiver.CloseAsync(),
                    partitionSender.CloseAsync());
            }
        }
Ejemplo n.º 11
0
        async Task ReceiveFromInvalidPartition()
        {
            PartitionReceiver receiver = null;

            // Some invalid partition values. These will fail on the service side.
            var invalidPartitions = new List <string>()
            {
                "XYZ", "-1", "1000", "-"
            };

            foreach (var invalidPartitionId in invalidPartitions)
            {
                await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
                {
                    TestUtility.Log($"Receiving from invalid partition {invalidPartitionId}");
                    receiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, invalidPartitionId, EventPosition.FromStart());
                    await receiver.ReceiveAsync(1);
                    throw new InvalidOperationException("Receive call should have failed");
                });

                await receiver.CloseAsync();
            }

            // Some invalid partition values. These will fail on the client side.
            invalidPartitions = new List <string>()
            {
                " ", null, ""
            };
            foreach (var invalidPartitionId in invalidPartitions)
            {
                await Assert.ThrowsAsync <InvalidOperationException>(async() =>
                {
                    TestUtility.Log($"Receiving from invalid partition {invalidPartitionId}");
                    receiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, invalidPartitionId, EventPosition.FromStart());
                    await receiver.ReceiveAsync(1);
                    throw new InvalidOperationException("Receive call should have failed");
                });

                await receiver.CloseAsync();
            }
        }
        public async Task UseSharedAccessSignature()
        {
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var csb           = new EventHubsConnectionStringBuilder(connectionString);
                var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(csb.SasKeyName, csb.SasKey);
                var token         = await tokenProvider.GetTokenAsync(csb.Endpoint.ToString(), TimeSpan.FromSeconds(120));

                var sas = token.TokenValue.ToString();

                // Update connection string builder to use shared access signature instead.
                csb.SasKey                = "";
                csb.SasKeyName            = "";
                csb.SharedAccessSignature = sas;

                // Create new client with updated connection string.
                var ehClient = EventHubClient.CreateFromConnectionString(csb.ToString());

                // Send one event
                TestUtility.Log($"Sending one message.");
                var ehSender  = ehClient.CreatePartitionSender("0");
                var eventData = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));
                await ehSender.SendAsync(eventData);

                // Receive event.
                PartitionReceiver ehReceiver = null;
                try
                {
                    TestUtility.Log($"Receiving one message.");
                    ehReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromStart());
                    var msg = await ehReceiver.ReceiveAsync(1);

                    Assert.True(msg != null, "Failed to receive message.");
                }
                finally
                {
                    await ehReceiver?.CloseAsync();
                }

                // Get EH runtime information.
                TestUtility.Log($"Getting Event Hub runtime information.");
                var ehInfo = await ehClient.GetRuntimeInformationAsync();

                Assert.True(ehInfo != null, "Failed to get runtime information.");

                // Get EH partition runtime information.
                TestUtility.Log($"Getting Event Hub partition '0' runtime information.");
                var partitionInfo = await ehClient.GetPartitionRuntimeInformationAsync("0");

                Assert.True(ehInfo != null, "Failed to get runtime partition information.");
            }
        }
Ejemplo n.º 13
0
        protected async Task ReceiveMessagesFromDeviceAsync(PartitionReceiver receiver)
        {
            while (true)
            {
                var eventData = await receiver.ReceiveAsync(5, new TimeSpan(0, 0, 5));  //Timeout for receiving messages = 30 seconds

                int count = 0;
                if (eventData != null)
                {
                    if (eventData != null)
                    {
                        foreach (var item in eventData)
                        {
                            count++;

                            if (item.SystemProperties.EnqueuedTimeUtc >= lastRowValues[receiver.PartitionId].EnqueuedTimeUtc && Convert.ToInt64(item.SystemProperties.Offset) > lastRowValues[receiver.PartitionId].EnqueuedOffset)
                            {
                                if (item.Properties.ContainsKey("MessageType") && item.Properties.ContainsKey("UnicodeEncoding"))
                                {
                                    if (this.MessageType.Equals(item.Properties["MessageType"]))
                                    {
                                        //Assert ? item.Properties["UnicodeEncoding"] == "UTF8"
                                        string data = UnicodeEncoding.UTF8.GetString(item.Body.Array);
                                        var    jsonDeserializerSettings = new JsonSerializerSettings()
                                        {
                                            DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind
                                        };
                                        List <Dictionary <string, object> > results = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(data, jsonDeserializerSettings);

                                        foreach (var r in results)
                                        {
                                            RowRead row = new RowRead();
                                            row.Values = r;
                                            receivedRows.Add(row);
                                        }
                                    }
                                }

                                LastReceivedMessage lastReceivedMessage;
                                lastReceivedMessage.EnqueuedTimeUtc = item.SystemProperties.EnqueuedTimeUtc;
                                lastReceivedMessage.EnqueuedOffset  = Convert.ToInt64(item.SystemProperties.Offset);
                                lastRowValues[receiver.PartitionId] = lastReceivedMessage;
                            }
                        }
                    }
                }
                if (count == 0)
                {
                    break;
                }
            }
            await Task.CompletedTask;
        }
        // Sends single message to given partition and returns it after receiving.
        async Task <EventData> SendAndReceiveSingleEvent(string partitionId)
        {
            var eDataToSend = new EventData(new byte[1]);

            // Stamp this message so we can recognize it when received.
            var stampValue = Guid.NewGuid().ToString();
            var sendEvent  = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));

            eDataToSend.Properties = new Dictionary <string, object>
            {
                { "stamp", stampValue }
            };
            PartitionSender partitionSender = this.EventHubClient.CreatePartitionSender(partitionId);

            Log($"Sending single event to partition {partitionId} with stamp {stampValue}");
            await partitionSender.SendAsync(eDataToSend);

            Log($"Receiving all messages from partition {partitionId}");
            PartitionReceiver receiver = null;

            try
            {
                receiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName,
                                                              partitionId, PartitionReceiver.StartOfStream);
                while (true)
                {
                    var receivedEvents = await receiver.ReceiveAsync(100);

                    if (receivedEvents == null || receivedEvents.Count() == 0)
                    {
                        throw new Exception("Not able to receive stamped message!");
                    }

                    Log($"Received {receivedEvents.Count()} event(s) in batch where last event is sent on {receivedEvents.Last().SystemProperties.EnqueuedTimeUtc}");

                    // Continue until we locate stamped message.
                    foreach (var receivedEvent in receivedEvents)
                    {
                        if (receivedEvent.Properties != null &&
                            receivedEvent.Properties.ContainsKey("stamp") &&
                            receivedEvent.Properties["stamp"].ToString() == eDataToSend.Properties["stamp"].ToString())
                        {
                            return(receivedEvent);
                        }
                    }
                }
            }
            finally
            {
                await receiver.CloseAsync();
            }
        }
        //// This function create a device with x509 cert and send a message to the iothub on the transport specified.
        //// It then verifies the message is received at the eventHubClient.
        internal async Task SendSingleMessageX509(Client.TransportType transport)
        {
            // TODO: Update Jenkins Config
            string endpoint = Configuration.IoTHub.EventHubString;

            if (endpoint.IsNullOrWhiteSpace())
            {
                return;
            }

            Tuple <string, string> deviceInfo = TestUtil.CreateDeviceWithX509(DevicePrefix, hostName, registryManager);

            EventHubClient    eventHubClient;
            PartitionReceiver eventHubReceiver = await CreateEventHubReceiver(deviceInfo.Item1).ConfigureAwait(false);

            X509Certificate2 cert = Configuration.IoTHub.GetCertificateWithPrivateKey();

            var auth         = new DeviceAuthenticationWithX509Certificate(deviceInfo.Item1, cert);
            var deviceClient = DeviceClient.Create(deviceInfo.Item2, auth, transport);

            try
            {
                await deviceClient.OpenAsync().ConfigureAwait(false);

                string         payload;
                string         p1Value;
                Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value);
                await deviceClient.SendEventAsync(testMessage).ConfigureAwait(false);

                bool      isReceived = false;
                Stopwatch sw         = new Stopwatch();
                sw.Start();
                while (!isReceived && sw.Elapsed.Minutes < 1)
                {
                    var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)).ConfigureAwait(false);

                    isReceived = VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value);
                }

                sw.Stop();

                Assert.IsTrue(isReceived, "Message is not received.");
            }
            finally
            {
                await deviceClient.CloseAsync().ConfigureAwait(false);

                await eventHubReceiver.CloseAsync().ConfigureAwait(false);

                await TestUtil.RemoveDeviceAsync(deviceInfo.Item1, registryManager).ConfigureAwait(false);
            }
        }
        internal async Task SendMessageThrottledForHttp()
        {
            // TODO: Update Jenkins Config
            if (Configuration.IoTHub.EventHubString.IsNullOrWhiteSpace())
            {
                return;
            }
            await sequentialTestSemaphore.WaitAsync();

            Tuple <string, string> deviceInfo = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager);

            EventHubClient    eventHubClient;
            PartitionReceiver eventHubReceiver = await CreateEventHubReceiver(deviceInfo.Item1);

            var deviceClient = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, Client.TransportType.Http1);

            try
            {
                deviceClient.OperationTimeoutInMilliseconds = (uint)TestUtil.ShortRetryInMilliSec;
                await deviceClient.OpenAsync();

                string         payload, p1Value;
                Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value);
                await deviceClient.SendEventAsync(testMessage);

                bool      isReceived = false;
                Stopwatch sw         = new Stopwatch();
                sw.Start();
                while (!isReceived && sw.Elapsed.Minutes < 1)
                {
                    var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5));

                    isReceived = VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value);
                }
                sw.Stop();

                // Implementation of error injection of throttling on http is that it will throttle the
                // fault injection message itself only.  The duration of fault has no effect on http throttle.
                // Client is supposed to retry sending the throttling fault message until operation timeout.
                await deviceClient.SendEventAsync(TestUtil.ComposeErrorInjectionProperties(TestUtil.FaultType_Throttle,
                                                                                           TestUtil.FaultCloseReason_Boom, TestUtil.DefaultDelayInSec, TestUtil.DefaultDurationInSec));
            }
            finally
            {
                await deviceClient.CloseAsync();

                await eventHubReceiver.CloseAsync();

                sequentialTestSemaphore.Release(1);
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        ///   Executes the performance test scenario asynchronously.
        /// </summary>
        ///
        /// <param name="cancellationToken">The token used to signal when cancellation is requested.</param>
        ///
        public async override Task RunAsync(CancellationToken cancellationToken)
        {
            // Read the requested number of events.

            var remainingEvents = Options.Count;

            while ((!cancellationToken.IsCancellationRequested) && (remainingEvents > 0))
            {
                remainingEvents -= (await _receiver.ReceiveAsync(remainingEvents).ConfigureAwait(false)).Count();
            }

            // If iteration stopped due to cancellation, ensure that the expected exception is thrown.

            cancellationToken.ThrowIfCancellationRequested();
        }
Ejemplo n.º 18
0
        public async Task ReceiveAsyncInvokesTheTransportConsumer()
        {
            var defaultMaximumReceiveWaitTime = TimeSpan.FromMilliseconds(8);
            var transportConsumer             = new ObservableTransportConsumerMock();
            var receiver             = new PartitionReceiver("group", "0", "hub", true, defaultMaximumReceiveWaitTime, transportConsumer);
            var expectedMessageCount = 45;

            using var cancellation = new CancellationTokenSource();
            await receiver.ReceiveAsync(expectedMessageCount, null, cancellation.Token);

            (var actualMessageCount, TimeSpan? actualWaitTime) = transportConsumer.ReceiveCalledWith;

            Assert.That(actualMessageCount, Is.EqualTo(expectedMessageCount), "The message counts should match.");
            Assert.That(actualWaitTime, Is.EqualTo(defaultMaximumReceiveWaitTime), "The wait time should match.");
        }
        public async Task UseITokenProviderWithAad()
        {
            var appAuthority = "";
            var aadAppId     = "";
            var aadAppSecret = "";

            AzureActiveDirectoryTokenProvider.AuthenticationCallback authCallback =
                async(audience, authority, state) =>
            {
                var authContext = new AuthenticationContext(authority);
                var cc          = new ClientCredential(aadAppId, aadAppSecret);
                var authResult  = await authContext.AcquireTokenAsync(audience, cc);

                return(authResult.AccessToken);
            };

            var tokenProvider = TokenProvider.CreateAzureActiveDirectoryTokenProvider(authCallback, appAuthority);

            // Create new client with updated connection string.
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var csb      = new EventHubsConnectionStringBuilder(connectionString);
                var ehClient = EventHubClient.CreateWithTokenProvider(csb.Endpoint, csb.EntityPath, tokenProvider);

                // Send one event
                TestUtility.Log($"Sending one message.");
                var ehSender  = ehClient.CreatePartitionSender("0");
                var eventData = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));
                await ehSender.SendAsync(eventData);

                // Receive event.
                PartitionReceiver ehReceiver = null;
                try
                {
                    TestUtility.Log($"Receiving one message.");
                    ehReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromStart());
                    var msg = await ehReceiver.ReceiveAsync(1);

                    Assert.True(msg != null, "Failed to receive message.");
                }
                finally
                {
                    await ehReceiver?.CloseAsync();
                }
            }
        }
Ejemplo n.º 20
0
        private static void receive(PartitionReceiver partitionReceiver, string requestId)
        {
            try
            {
                while (true)
                {
                    var receiveTask = partitionReceiver.ReceiveAsync(1, TimeSpan.FromSeconds(10));
                    receiveTask.Wait();
                    IEnumerable <EventData> receivedEvents = receiveTask.Result;

                    if (receivedEvents != null)
                    {
                        foreach (EventData eventData in receivedEvents)
                        {
                            if (eventData != null)
                            {
                                if (eventData.Properties != null && eventData.Properties.ContainsKey(COMMAND_REQUEST_ID_PROPERTY_NAME))
                                {
                                    string payload = Encoding.UTF8.GetString(eventData.Body);
                                    Console.WriteLine("Received an update on the async command:");
                                    Console.WriteLine();
                                    foreach (string propertyKey in eventData.Properties.Keys)
                                    {
                                        Console.WriteLine("    " + propertyKey + ":" + eventData.Properties[propertyKey]);
                                    }
                                    Console.WriteLine();
                                    Console.WriteLine("    " + "Update Payload: ");
                                    Console.WriteLine("        " + payload);
                                    Console.WriteLine();

                                    if (payload.Contains("100%"))
                                    {
                                        Console.WriteLine("Async command has finished, enter any key to finish\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (ThreadInterruptedException e)
            {
                //Thread was aborted, so allow it to end
                partitionReceiver.Close();
            }
        }
Ejemplo n.º 21
0
        async Task PartitionReceiverReceiveBatch()
        {
            const int MaxBatchSize = 5;

            TestUtility.Log("Receiving Events via PartitionReceiver.ReceiveAsync(BatchSize)");
            const string      partitionId       = "0";
            PartitionSender   partitionSender   = this.EventHubClient.CreatePartitionSender(partitionId);
            PartitionReceiver partitionReceiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, DateTime.UtcNow.AddMinutes(-10));

            try
            {
                int eventCount = 20;
                TestUtility.Log($"Sending {eventCount} events to Partition {partitionId}");
                var sendEvents = new List <EventData>(eventCount);
                for (int i = 0; i < eventCount; i++)
                {
                    sendEvents.Add(new EventData(Encoding.UTF8.GetBytes($"Hello EventHub! Message {i}")));
                }
                await partitionSender.SendAsync(sendEvents);

                int maxReceivedBatchSize = 0;
                while (true)
                {
                    IEnumerable <EventData> partition1Events = await partitionReceiver.ReceiveAsync(MaxBatchSize);

                    int receivedEventCount = partition1Events != null?partition1Events.Count() : 0;

                    TestUtility.Log($"Received {receivedEventCount} event(s)");

                    if (partition1Events == null)
                    {
                        break;
                    }

                    maxReceivedBatchSize = Math.Max(maxReceivedBatchSize, receivedEventCount);
                }

                Assert.True(maxReceivedBatchSize == MaxBatchSize, $"A max batch size of {MaxBatchSize} events was not honored! Actual {maxReceivedBatchSize}.");
            }
            finally
            {
                await partitionReceiver.CloseAsync();

                await partitionSender.CloseAsync();
            }
        }
        private async Task ValidateDisabledBehavior(PartitionReceiver partitionReceiver)
        {
            Assert.True(partitionReceiver.ReceiverRuntimeMetricEnabled == false, "ReceiverRuntimeMetricEnabled == true");

            // Receive a message and validate RuntimeInfo isn't set.
            var msg = await partitionReceiver.ReceiveAsync(1);

            Assert.True(msg != null, "Failed to receive a message.");
            Assert.True(partitionReceiver.RuntimeInfo.LastEnqueuedOffset == null,
                        $"FAILED partitionReceiver.RuntimeInfo.LastEnqueuedOffset == {partitionReceiver.RuntimeInfo.LastEnqueuedOffset}");
            Assert.True(partitionReceiver.RuntimeInfo.LastEnqueuedTimeUtc == DateTime.MinValue,
                        $"FAILED partitionReceiver.RuntimeInfo.LastEnqueuedTimeUtc == {partitionReceiver.RuntimeInfo.LastEnqueuedTimeUtc}");
            Assert.True(partitionReceiver.RuntimeInfo.LastSequenceNumber == -1,
                        $"FAILED partitionReceiver.RuntimeInfo.LastSequenceNumber == {partitionReceiver.RuntimeInfo.LastSequenceNumber}");
            Assert.True(partitionReceiver.RuntimeInfo.RetrievalTime == DateTime.MinValue,
                        $"FAILED partitionReceiver.RuntimeInfo.RetrievalTime == {partitionReceiver.RuntimeInfo.RetrievalTime}");
        }
        public async Task PrepareAzureEventHubResource_NewNamespace_NoError()
        {
            var            message        = "Hello";
            EventHubClient eventHubClient = await _eventHubResource.GetEventHubClientAsync("testEventHub");

            await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));

            PartitionReceiver receiver = await _eventHubResource.GetEventHubReceiverAsync("testEventHub");

            IEnumerable <EventData> events = await receiver.ReceiveAsync(1);

            EventData eventData = events.FirstOrDefault();

            Assert.NotNull(eventData);
            var result = Encoding.UTF8.GetString(eventData.Body);

            Assert.Equal(message, result);
        }
Ejemplo n.º 24
0
        //// This function create a device with x509 cert and send a message to the iothub on the transport specified.
        //// It then verifies the message is received at the eventHubClient.
        internal async Task SendSingleMessageX509(Client.TransportType transport)
        {
            TestDevice testDevice = await TestDevice.GetTestDeviceAsync(DevicePrefix, TestDeviceType.X509).ConfigureAwait(false);

            PartitionReceiver eventHubReceiver = await CreateEventHubReceiver(testDevice.Id).ConfigureAwait(false);

            X509Certificate2 cert = Configuration.IoTHub.GetCertificateWithPrivateKey();

            var auth         = new DeviceAuthenticationWithX509Certificate(testDevice.Id, cert);
            var deviceClient = DeviceClient.Create(testDevice.IoTHubHostName, auth, transport);

            try
            {
                await deviceClient.OpenAsync().ConfigureAwait(false);

                string         payload;
                string         p1Value;
                Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value);
                await deviceClient.SendEventAsync(testMessage).ConfigureAwait(false);

                bool      isReceived = false;
                Stopwatch sw         = new Stopwatch();
                sw.Start();
                while (!isReceived && sw.Elapsed.Minutes < 1)
                {
                    var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(5)).ConfigureAwait(false);

                    isReceived = VerifyTestMessage(events, testDevice.Id, payload, p1Value);
                }

                sw.Stop();

                Assert.IsTrue(isReceived, "Message is not received.");
            }
            finally
            {
                await deviceClient.CloseAsync().ConfigureAwait(false);

                await eventHubReceiver.CloseAsync().ConfigureAwait(false);

                _log.WriteLine($"Waiting for fault injection interval to finish {FaultInjection.DefaultDelayInSec}s.");
                await Task.Delay(FaultInjection.DefaultDurationInSec * 1000).ConfigureAwait(false);
            }
        }
        // Receives all messages on the given receiver.
        async Task <List <EventData> > ReceiveAllMessages(PartitionReceiver receiver)
        {
            List <EventData> messages = new List <EventData>();

            while (true)
            {
                var receivedEvents = await receiver.ReceiveAsync(100);

                if (receivedEvents == null)
                {
                    // There is no more events to receive.
                    break;
                }

                messages.AddRange(receivedEvents);
            }

            return(messages);
        }
        private static async Task <IReadOnlyList <EventData> > ReceiveAll(PartitionReceiver receiver)
        {
            var messages = new List <EventData>();
            var waitTime = TimeSpan.FromMilliseconds(1000);

            while (true)
            {
                IEnumerable <EventData> received = await receiver.ReceiveAsync(10, waitTime);

                if (received == null)
                {
                    break;
                }

                messages.AddRange(received);
            }

            return(messages);
        }
        public async Task UseITokenProviderWithSas()
        {
            // Generate SAS token provider.
            var csb           = new EventHubsConnectionStringBuilder(TestUtility.EventHubsConnectionString);
            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(csb.SasKeyName, csb.SasKey);

            // Create new client with updated connection string.
            var ehClient = EventHubClient.CreateWithTokenProvider(csb.Endpoint, csb.EntityPath, tokenProvider);

            // Send one event
            TestUtility.Log($"Sending one message.");
            var ehSender  = ehClient.CreatePartitionSender("0");
            var eventData = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));
            await ehSender.SendAsync(eventData);

            // Receive event.
            PartitionReceiver ehReceiver = null;

            try
            {
                TestUtility.Log($"Receiving one message.");
                ehReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromStart());
                var msg = await ehReceiver.ReceiveAsync(1);

                Assert.True(msg != null, "Failed to receive message.");
            }
            finally
            {
                await ehReceiver?.CloseAsync();
            }

            // Get EH runtime information.
            TestUtility.Log($"Getting Event Hub runtime information.");
            var ehInfo = await ehClient.GetRuntimeInformationAsync();

            Assert.True(ehInfo != null, "Failed to get runtime information.");

            // Get EH partition runtime information.
            TestUtility.Log($"Getting Event Hub partition '0' runtime information.");
            var partitionInfo = await ehClient.GetPartitionRuntimeInformationAsync("0");

            Assert.True(ehInfo != null, "Failed to get runtime partition information.");
        }
        internal async Task SendSingleMessage(Client.TransportType transport)
        {
            // TODO: Update Jenkins Config
            if (Configuration.IoTHub.EventHubString.IsNullOrWhiteSpace())
            {
                return;
            }
            Tuple <string, string> deviceInfo       = TestUtil.CreateDevice(DevicePrefix, hostName, registryManager);
            PartitionReceiver      eventHubReceiver = await CreateEventHubReceiver(deviceInfo.Item1);

            var deviceClient = DeviceClient.CreateFromConnectionString(deviceInfo.Item2, transport);

            try
            {
                await deviceClient.OpenAsync();

                string         payload;
                string         p1Value;
                Client.Message testMessage = ComposeD2CTestMessage(out payload, out p1Value);
                await deviceClient.SendEventAsync(testMessage);

                bool      isReceived = false;
                Stopwatch sw         = new Stopwatch();
                sw.Start();
                while (!isReceived && sw.Elapsed.Minutes < 1)
                {
                    var events = await eventHubReceiver.ReceiveAsync(int.MaxValue, TimeSpan.FromSeconds(30));

                    isReceived = VerifyTestMessage(events, deviceInfo.Item1, payload, p1Value);
                }
                sw.Stop();

                Assert.IsTrue(isReceived, "Message is not received.");
            }
            finally
            {
                await deviceClient.CloseAsync();

                await eventHubReceiver.CloseAsync();

                await TestUtil.RemoveDeviceAsync(deviceInfo.Item1, registryManager);
            }
        }
Ejemplo n.º 29
0
        public async Task UseITokenProviderWithAad()
        {
            var tenantId     = "";
            var aadAppId     = "";
            var aadAppSecret = "";

            if (string.IsNullOrEmpty(tenantId))
            {
                TestUtility.Log($"Skipping test during scheduled runs.");
                return;
            }

            var authContext   = new AuthenticationContext($"https://login.windows.net/{tenantId}");
            var cc            = new ClientCredential(aadAppId, aadAppSecret);
            var tokenProvider = TokenProvider.CreateAadTokenProvider(authContext, cc);

            // Create new client with updated connection string.
            var csb      = new EventHubsConnectionStringBuilder(TestUtility.EventHubsConnectionString);
            var ehClient = EventHubClient.Create(csb.Endpoint, csb.EntityPath, tokenProvider);

            // Send one event
            TestUtility.Log($"Sending one message.");
            var ehSender  = ehClient.CreatePartitionSender("0");
            var eventData = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));
            await ehSender.SendAsync(eventData);

            // Receive event.
            PartitionReceiver ehReceiver = null;

            try
            {
                TestUtility.Log($"Receiving one message.");
                ehReceiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, "0", EventPosition.FromStart());
                var msg = await ehReceiver.ReceiveAsync(1);

                Assert.True(msg != null, "Failed to receive message.");
            }
            finally
            {
                await ehReceiver?.CloseAsync();
            }
        }
Ejemplo n.º 30
0
        public async Task ReadFromSpecificPartition()
        {
            await using var scope = await EventHubScope.CreateAsync(1);

            #region Snippet:EventHubs_Migrate_T1_ReadFromSpecificPartition
#if SNIPPET
            var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
            var eventHubName     = "<< NAME OF THE EVENT HUB >>";
            var consumerGroup    = "<< NAME OF THE EVENT HUB CONSUMER GROUP >>";
#else
            var connectionString = TestUtility.EventHubsConnectionString;
            var eventHubName     = scope.EventHubName;
            var consumerGroup    = PartitionReceiver.DefaultConsumerGroupName;
#endif

            var builder = new EventHubsConnectionStringBuilder(connectionString);
            builder.EntityPath = eventHubName;

            EventHubClient    client   = EventHubClient.CreateFromConnectionString(builder.ToString());
            PartitionReceiver receiver = default;

            try
            {
                string firstPartition = (await client.GetRuntimeInformationAsync()).PartitionIds.First();
                receiver = client.CreateReceiver(consumerGroup, firstPartition, EventPosition.FromStart());

                IEnumerable <EventData> events = await receiver.ReceiveAsync(50);

                foreach (var eventData in events)
                {
                    Debug.WriteLine($"Read event of length { eventData.Body.Count } from { firstPartition }");
                }
            }
            finally
            {
                receiver?.Close();
                client.Close();
            }

            #endregion
        }