예제 #1
0
        /// <inheritdoc />
        protected override async Task <BoolResult> StartupCoreAsync(OperationContext context)
        {
            Tracer.Info(context, $"Initializing Event Hub-based content location event store with epoch '{_configuration.Epoch}'.");

            var baseInitializeResult = await base.StartupCoreAsync(context);

            if (!baseInitializeResult)
            {
                return(baseInitializeResult);
            }

            var connectionStringBuilder =
                new EventHubsConnectionStringBuilder(_configuration.EventHubConnectionString)
            {
                EntityPath = _configuration.EventHubName,
            };

            _currentEventProcessor = new Processor(context, this);

            // Retry behavior in the Azure Event Hubs Client Library is controlled by the RetryPolicy property on the EventHubClient class.
            // The default policy retries with exponential backoff when Azure Event Hub returns a transient EventHubsException or an OperationCanceledException.
            _eventHubClient  = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            _partitionSender = _eventHubClient.CreatePartitionSender(PartitionId);

            return(BoolResult.Success);
        }
예제 #2
0
 public RequestSender(uint processId, EventHubsConnections connections,
                      ILogger logger, DataContractSerializer payloadSerializer, FunctionsHostConfiguration configuration)
     : base(processId, connections, logger, payloadSerializer, configuration)
 {
     this.processId = processId;
     doorbell       = connections.GetDoorbellSender(processId);
 }
예제 #3
0
        async Task NonexistentEntity()
        {
            // Rebuild connection string with a nonexistent entity.
            var csb = new EventHubsConnectionStringBuilder(TestUtility.EventHubsConnectionString);

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

            // GetRuntimeInformationAsync on a nonexistent entity.
            await Assert.ThrowsAsync <MessagingEntityNotFoundException>(async() =>
            {
                TestUtility.Log("Getting entity information from a nonexistent entity.");
                await ehClient.GetRuntimeInformationAsync();
                throw new InvalidOperationException("GetRuntimeInformation call should have failed");
            });

            // GetPartitionRuntimeInformationAsync on a nonexistent entity.
            await Assert.ThrowsAsync <MessagingEntityNotFoundException>(async() =>
            {
                TestUtility.Log("Getting partition information from a nonexistent entity.");
                await ehClient.GetPartitionRuntimeInformationAsync("0");
                throw new InvalidOperationException("GetPartitionRuntimeInformation call should have failed");
            });

            // Try sending.
            PartitionSender sender = null;
            await Assert.ThrowsAsync <MessagingEntityNotFoundException>(async() =>
            {
                TestUtility.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 call should have failed");
            });

            await sender.CloseAsync();

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

            await receiver.CloseAsync();

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

            await receiver.CloseAsync();
        }
        async Task GetEventHubPartitionRuntimeInformation()
        {
            var cbs = new EventHubsConnectionStringBuilder(EventHubsConnectionString);

            Log("Getting EventHubPartitionRuntimeInformation on each partition in parallel");
            var tasks = this.PartitionIds.Select(async(pid) =>
            {
                // Send some messages so we can have meaningful data returned from service call.
                PartitionSender partitionSender = this.EventHubClient.CreatePartitionSender(pid);
                Log($"Sending single event to partition {pid}");
                var eDataToSend = new EventData(new byte[1]);
                await partitionSender.SendAsync(eDataToSend);

                Log($"Getting partition runtime information on partition {pid}");
                var p = await this.EventHubClient.GetPartitionRuntimeInformationAsync(pid);
                Log($"Path:{p.Path} PartitionId:{p.PartitionId} BeginSequenceNumber:{p.BeginSequenceNumber} LastEnqueuedOffset:{p.LastEnqueuedOffset} LastEnqueuedTimeUtc:{p.LastEnqueuedTimeUtc} LastEnqueuedSequenceNumber:{p.LastEnqueuedSequenceNumber}");

                // Validations.
                Assert.True(p.Path == cbs.EntityPath, $"Returned path {p.Path} is different than {cbs.EntityPath}");
                Assert.True(p.PartitionId == pid, $"Returned partition id {p.PartitionId} is different than {pid}");
                Assert.True(p.LastEnqueuedOffset != null, "Returned LastEnqueuedOffset is null");
                Assert.True(p.LastEnqueuedTimeUtc != null, "Returned LastEnqueuedTimeUtc is null");

                // Validate returned data regarding recently sent event.
                // Account 60 seconds of max clock skew.
                Assert.True(p.LastEnqueuedOffset != "-1", $"Returned LastEnqueuedOffset is {p.LastEnqueuedOffset}");
                Assert.True(p.BeginSequenceNumber >= 0, $"Returned BeginSequenceNumber is {p.BeginSequenceNumber}");
                Assert.True(p.LastEnqueuedSequenceNumber >= 0, $"Returned LastEnqueuedSequenceNumber is {p.LastEnqueuedSequenceNumber}");
                Assert.True(p.LastEnqueuedTimeUtc >= DateTime.UtcNow.AddSeconds(-60), $"Returned LastEnqueuedTimeUtc is {p.LastEnqueuedTimeUtc}");
            });

            await Task.WhenAll(tasks);
        }
예제 #5
0
 public RemoteSender(uint processId, uint destination, EventHubsConnections connections,
                     ILogger logger, DataContractSerializer payloadSerializer, FunctionsHostConfiguration configuration, DateTime deploymentTimestamp)
     : base(destination, connections, logger, payloadSerializer, configuration)
 {
     this.processId           = processId;
     this.deploymentTimestamp = deploymentTimestamp;
     doorbell = connections.GetDoorbellSender(destination);
 }
예제 #6
0
        async Task PartitionReceiverSetReceiveHandler()
        {
            Log("Receiving Events via PartitionReceiver.SetReceiveHandler()");
            string            partitionId       = "1";
            PartitionReceiver partitionReceiver = this.EventHubClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, partitionId, DateTime.UtcNow.AddMinutes(-10));
            PartitionSender   partitionSender   = this.EventHubClient.CreatePartitionSender(partitionId);

            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);

                EventWaitHandle dataReceivedEvent = new EventWaitHandle(false, EventResetMode.ManualReset);
                var             handler           = new TestPartitionReceiveHandler();
                handler.ErrorReceived  += (s, e) => Log($"TestPartitionReceiveHandler.ProcessError {e.GetType().Name}: {e.Message}");
                handler.EventsReceived += (s, eventDatas) =>
                {
                    int count = eventDatas != null?eventDatas.Count() : 0;

                    Log($"Received {count} event(s):");

                    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");
                                dataReceivedEvent.Set();
                                break;
                            }
                        }
                    }
                };

                partitionReceiver.SetReceiveHandler(handler);

                if (!dataReceivedEvent.WaitOne(TimeSpan.FromSeconds(20)))
                {
                    throw new InvalidOperationException("Data Received Event was not signaled.");
                }
            }
            finally
            {
                await partitionSender.CloseAsync();

                await partitionReceiver.CloseAsync();
            }
        }
        readonly MemoryStream stream = new MemoryStream(); // reused for all packets

        public EventHubsSender(TransportAbstraction.IHost host, byte[] taskHubGuid, PartitionSender sender, EventHubsTraceHelper traceHelper)
            : base(nameof(EventHubsSender <T>), false, 2000, CancellationToken.None)
        {
            this.host              = host;
            this.taskHubGuid       = taskHubGuid;
            this.sender            = sender;
            this.traceHelper       = traceHelper;
            this.eventHubName      = this.sender.EventHubClient.EventHubName;
            this.eventHubPartition = this.sender.PartitionId;
        }
예제 #8
0
 public BatchSender(uint destination, EventHubsConnections connections, ILogger logger,
                    DataContractSerializer payloadSerializer, FunctionsHostConfiguration configuration, PartitionSender sender)
 {
     this.destination       = destination;
     this.connections       = connections;
     this.payloadSerializer = payloadSerializer;
     this.configuration     = configuration;
     this.logger            = new LoggerWrapper(logger, $" [sender{destination:d3}] ");
     this.sender            = sender;
 }
예제 #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);
        }
예제 #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());
            }
        }
        public async Task GetEventHubPartitionRuntimeInformation()
        {
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var csb        = new EventHubsConnectionStringBuilder(connectionString);
                var ehClient   = EventHubClient.CreateFromConnectionString(csb.ToString());
                var partitions = await this.GetPartitionsAsync(ehClient);

                try
                {
                    TestUtility.Log("Getting EventHubPartitionRuntimeInformation on each partition in parallel");
                    var tasks = partitions.Select(async(pid) =>
                    {
                        // Send some messages so we can have meaningful data returned from service call.
                        PartitionSender partitionSender = ehClient.CreatePartitionSender(pid);

                        try
                        {
                            TestUtility.Log($"Sending single event to partition {pid}");
                            var eDataToSend = new EventData(new byte[1]);
                            await partitionSender.SendAsync(eDataToSend);

                            TestUtility.Log($"Getting partition runtime information on partition {pid}");
                            var partition = await ehClient.GetPartitionRuntimeInformationAsync(pid);
                            TestUtility.Log($"Path:{partition.Path} PartitionId:{partition.PartitionId} BeginSequenceNumber:{partition.BeginSequenceNumber} LastEnqueuedOffset:{partition.LastEnqueuedOffset} LastEnqueuedTimeUtc:{partition.LastEnqueuedTimeUtc} LastEnqueuedSequenceNumber:{partition.LastEnqueuedSequenceNumber}");

                            // Validations.
                            Assert.True(partition.Path == csb.EntityPath, $"Returned path {partition.Path} is different than {csb.EntityPath}");
                            Assert.True(partition.PartitionId == pid, $"Returned partition id {partition.PartitionId} is different than {pid}");
                            Assert.True(partition.LastEnqueuedOffset != null, "Returned LastEnqueuedOffset is null");
                            Assert.True(partition.LastEnqueuedTimeUtc != null, "Returned LastEnqueuedTimeUtc is null");

                            // Validate returned data regarding recently sent event.
                            // Account 60 seconds of max clock skew.
                            Assert.True(partition.LastEnqueuedOffset != "-1", $"Returned LastEnqueuedOffset is {partition.LastEnqueuedOffset}");
                            Assert.True(partition.BeginSequenceNumber >= 0, $"Returned BeginSequenceNumber is {partition.BeginSequenceNumber}");
                            Assert.True(partition.LastEnqueuedSequenceNumber >= 0, $"Returned LastEnqueuedSequenceNumber is {partition.LastEnqueuedSequenceNumber}");
                            Assert.True(partition.LastEnqueuedTimeUtc >= DateTime.UtcNow.AddSeconds(-60), $"Returned LastEnqueuedTimeUtc is {partition.LastEnqueuedTimeUtc}");
                        }
                        finally
                        {
                            await partitionSender.CloseAsync();
                        }
                    });

                    await Task.WhenAll(tasks);
                }
                finally
                {
                    await ehClient.CloseAsync();
                }
            }
        }
        // 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();
            }
        }
        public async Task SendToInvalidPartition()
        {
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var             connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var             ehClient         = EventHubClient.CreateFromConnectionString(connectionString);
                PartitionSender sender           = null;

                try
                {
                    // Some invalid partition values.
                    var invalidPartitions = new List <string>()
                    {
                        "XYZ", "-1", "1000", "-"
                    };

                    foreach (var invalidPartitionId in invalidPartitions)
                    {
                        await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
                        {
                            TestUtility.Log($"Sending to invalid partition {invalidPartitionId}");
                            sender = ehClient.CreatePartitionSender(invalidPartitionId);
                            await sender.SendAsync(new EventData(new byte[1]));
                        });

                        await sender.CloseAsync();
                    }

                    // Some other invalid partition values. These will fail on the client side.
                    invalidPartitions = new List <string>()
                    {
                        "", " ", null
                    };
                    foreach (var invalidPartitionId in invalidPartitions)
                    {
                        await Assert.ThrowsAsync <ArgumentNullException>(async() =>
                        {
                            TestUtility.Log($"Sending to invalid partition {invalidPartitionId}");
                            sender = ehClient.CreatePartitionSender(invalidPartitionId);
                            await sender.SendAsync(new EventData(new byte[1]));
                        });

                        await sender.CloseAsync();
                    }
                }
                finally
                {
                    await ehClient.CloseAsync();
                }
            }
        }
        async Task PartitionSenderSend()
        {
            Log("Sending single Event via PartitionSender.SendAsync(EventData)");
            PartitionSender partitionSender1 = this.EventHubClient.CreatePartitionSender("1");

            try
            {
                var eventData = new EventData(Encoding.UTF8.GetBytes("Hello again EventHub Partition 1!"));
                await partitionSender1.SendAsync(eventData);
            }
            finally
            {
                await partitionSender1.CloseAsync();
            }
        }
        /// <summary>
        ///   Performs the tasks needed to initialize and set up the environment for the test scenario.
        ///   When multiple instances are run in parallel, the setup will take place once, prior to the
        ///   execution of the first test instance.
        /// </summary>
        ///
        public async override Task GlobalSetupAsync()
        {
            await base.GlobalSetupAsync().ConfigureAwait(false);

            s_scope = await EventHubScope.CreateAsync(4).ConfigureAwait(false);

            s_client    = EventHubClient.CreateFromConnectionString(TestUtility.BuildEventHubsConnectionString(s_scope.EventHubName));
            s_eventBody = EventGenerator.CreateRandomBody(Options.Size);

            var partition = (await s_client.GetRuntimeInformationAsync().ConfigureAwait(false)).PartitionIds[0];

            s_sender = s_client.CreatePartitionSender(partition);

            // Publish an empty event to force the connection and link to be established.

            await s_sender.SendAsync(new[] { new EventData(Array.Empty <byte>()) }).ConfigureAwait(false);
        }
        async Task PartitionSenderSendBatch()
        {
            Log("Sending single Event via PartitionSender.SendAsync(IEnumerable<EventData>)");
            PartitionSender partitionSender1 = this.EventHubClient.CreatePartitionSender("1");

            try
            {
                var eventData1 = new EventData(Encoding.UTF8.GetBytes("Hello EventHub!"));
                var eventData2 = new EventData(Encoding.UTF8.GetBytes("This is another message in the batch!"));
                eventData2.Properties["ContosoEventType"] = "some value here";
                await partitionSender1.SendAsync(new[] { eventData1, eventData2 });
            }
            finally
            {
                await partitionSender1.CloseAsync();
            }
        }
예제 #17
0
        public async Task PublishToSpecificPartition()
        {
            await using var scope = await EventHubScope.CreateAsync(1);

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

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

            EventHubClient  client = EventHubClient.CreateFromConnectionString(builder.ToString());
            PartitionSender sender = default;

            try
            {
                using var eventBatch = client.CreateBatch();

                for (var index = 0; index < 5; ++index)
                {
                    var eventData = new EventData(Encoding.UTF8.GetBytes($"Event #{ index }"));

                    if (!eventBatch.TryAdd(eventData))
                    {
                        throw new Exception($"The event at { index } could not be added");
                    }
                }

                string firstPartition = (await client.GetRuntimeInformationAsync()).PartitionIds.First();
                sender = client.CreatePartitionSender(firstPartition);

                await sender.SendAsync(eventBatch);
            }
            finally
            {
                sender?.Close();
                client.Close();
            }

            #endregion
        }
예제 #18
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();
            }
        }
예제 #19
0
        /// <inheritdoc />
        protected override async Task <BoolResult> StartupCoreAsync(OperationContext context)
        {
            await base.StartupCoreAsync(context).ThrowIfFailure();

            var connectionStringBuilder =
                new EventHubsConnectionStringBuilder(_configuration.EventHubConnectionString)
            {
                EntityPath = _configuration.EventHubName,
            };

            // Retry behavior in the Azure Event Hubs Client Library is controlled by the RetryPolicy property on the EventHubClient class.
            // The default policy retries with exponential backoff when Azure Event Hub returns a transient EventHubsException or an OperationCanceledException.
            _eventHubClient  = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
            _partitionSender = _eventHubClient.CreatePartitionSender(PartitionId);

            return(BoolResult.Success);
        }
        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();
        }
예제 #21
0
        async Task SendToInvalidPartition()
        {
            PartitionSender sender = null;

            // Some invalid partition values.
            var invalidPartitions = new List <string>()
            {
                "XYZ", "-1", "1000", "-"
            };

            foreach (var invalidPartitionId in invalidPartitions)
            {
                await Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
                {
                    TestUtility.Log($"Sending to invalid partition {invalidPartitionId}");
                    sender = this.EventHubClient.CreatePartitionSender(invalidPartitionId);
                    await sender.SendAsync(new EventData(new byte[1]));
                    throw new InvalidOperationException("Send call should have failed");
                });

                await sender.CloseAsync();
            }

            // Some other invalid partition values. These will fail on the client side.
            invalidPartitions = new List <string>()
            {
                "", " ", null
            };
            foreach (var invalidPartitionId in invalidPartitions)
            {
                await Assert.ThrowsAsync <ArgumentException>(async() =>
                {
                    TestUtility.Log($"Sending to invalid partition {invalidPartitionId}");
                    sender = this.EventHubClient.CreatePartitionSender(invalidPartitionId);
                    await sender.SendAsync(new EventData(new byte[1]));
                    throw new InvalidOperationException("Send call should have failed");
                });

                await sender.CloseAsync();
            }
        }
예제 #22
0
        public async Task SendAndReceive()
        {
            string targetPartitionId = "0";

            TestUtility.Log("Creating Event Hub client");
            await using (var scope = await EventHubScope.CreateAsync(1))
            {
                var connectionString = TestUtility.BuildEventHubsConnectionString(scope.EventHubName);
                var ehClient         = EventHubClient.CreateFromConnectionString(GetWebSocketConnectionString(connectionString));

                PartitionSender sender = null;
                try
                {
                    // Send single message
                    TestUtility.Log("Sending single event");
                    sender = ehClient.CreatePartitionSender(targetPartitionId);
                    var eventData = new EventData(Encoding.UTF8.GetBytes("This event will be transported via web-sockets"));
                    await sender.SendAsync(eventData);
                }
                finally
                {
                    await sender?.CloseAsync();
                }

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

                    Assert.True(msg != null, $"Failed to receive single event from partition {targetPartitionId}");
                }
                finally
                {
                    await receiver?.CloseAsync();
                }
            }
        }
예제 #23
0
        static async Task Main(string[] args)
        {
            // Creates an EventHubsConnectionStringBuilder object from the connection string, and sets the EntityPath.
            // Typically, the connection string should have the entity path in it, but this simple scenario
            // uses the connection string from the namespace.
            var connectionStringBuilder = new EventHubsConnectionStringBuilder(EventHubConnectionString)
            {
                EntityPath = EventHubName
            };

            eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());

            // Specify a specific partition Id to send the messages to.
            partitionSender = eventHubClient.CreatePartitionSender("2");

            await SendEventsToEventHub(100);

            await eventHubClient.CloseAsync();

            Console.WriteLine("Press ENTER to exit.");
            Console.ReadLine();
        }
예제 #24
0
        async Task SendAndReceive()
        {
            string targetPartitionId = "0";

            // Create new client with updated connection string.
            TestUtility.Log("Creating Event Hub client");
            var ehClient = EventHubClient.CreateFromConnectionString(webSocketConnString);

            PartitionSender sender = null;

            try
            {
                // Send single message
                TestUtility.Log("Sending single event");
                sender = ehClient.CreatePartitionSender(targetPartitionId);
                var eventData = new EventData(Encoding.UTF8.GetBytes("This event will be transported via web-sockets"));
                await sender.SendAsync(eventData);
            }
            finally
            {
                await sender?.CloseAsync();
            }

            PartitionReceiver receiver = null;

            try
            {
                // Receive single message.
                TestUtility.Log("Receiving single event");
                receiver = ehClient.CreateReceiver(PartitionReceiver.DefaultConsumerGroupName, targetPartitionId, PartitionReceiver.StartOfStream);
                var msg = await receiver.ReceiveAsync(1);

                Assert.True(msg != null, $"Failed to receive single event from partition {targetPartitionId}");
            }
            finally
            {
                await receiver?.CloseAsync();
            }
        }
        /// <summary>
        ///   Performs the tasks needed to initialize and set up the environment for the test scenario.
        ///   When multiple instances are run in parallel, the setup will take place once, prior to the
        ///   execution of the first test instance.
        /// </summary>
        ///
        public async override Task GlobalSetupAsync()
        {
            await base.GlobalSetupAsync().ConfigureAwait(false);

            s_scope = await EventHubScope.CreateAsync(4).ConfigureAwait(false);

            s_client    = EventHubClient.CreateFromConnectionString(TestUtility.BuildEventHubsConnectionString(s_scope.EventHubName));
            s_eventBody = EventGenerator.CreateRandomBody(Options.Size);

            var partition = (await s_client.GetRuntimeInformationAsync().ConfigureAwait(false)).PartitionIds[0];

            s_sender = s_client.CreatePartitionSender(partition);

            // Publish an empty event to force the connection and link to be established.

            using var batch = s_sender.CreateBatch();

            if (!batch.TryAdd(new EventData(Array.Empty <byte>())))
            {
                throw new InvalidOperationException("The empty event could not be added to the batch during global setup.");
            }

            await s_sender.SendAsync(batch).ConfigureAwait(false);
        }
예제 #26
0
 public DefaultPartitionSender(PartitionSender sender)
 {
     _sender = sender;
 }
        private static SenderInfo CreatePartitionSender(double n, StreamOptions x, string id, PartitionSender sender)
        {
            var si = new SenderInfo()
            {
                PartitionId = id,
            };

            si.Thread = new Thread(() =>
            {
                Console.WriteLine($"Thread started to send to {id}, sending {n} events...");

                var epoch     = DateTime.UtcNow;
                var sessionId = Guid.NewGuid();
                var deviceId  = Guid.NewGuid();

                EventDataBatch batch = null;
                while (n >= 0)
                {
                    if (batch == null)
                    {
                        batch = sender.CreateBatch(new BatchOptions());
                    }

                    var e = GenerateEvent(epoch, si.SendCount, sessionId, deviceId);

                    if (!batch.TryAdd(e))
                    {
                        // flush
                        sender.SendAsync(batch).Wait();

                        batch = null;

                        // and go to continue, because we need to resend the event that failed
                        continue;
                    }

                    // looks like that went through, yay
                    epoch = epoch.AddMilliseconds(x.Interval);

                    Thread.Sleep(x.Pause);
                    n--;
                    si.SendCount++;
                }

                if (batch != null)
                {
                    sender.SendAsync(batch).Wait();
                }

                si.Event.Set();
            });

            si.Thread.Start();

            return(si);
        }