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 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 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(); } }
/// <summary> /// Performs the tasks needed to clean up the environment for the test scenario. /// When multiple instances are run in parallel, the cleanup will take place once, /// after the execution of all test instances. /// </summary> /// public async override Task GlobalCleanupAsync() { await s_sender.CloseAsync().ConfigureAwait(false); await s_client.CloseAsync().ConfigureAwait(false); await s_scope.DisposeAsync().ConfigureAwait(false); await base.GlobalCleanupAsync().ConfigureAwait(false); }
// 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); }
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(); } } }
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()); } }
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(); } }
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(); } }
/// <inheritdoc /> protected override async Task <BoolResult> ShutdownCoreAsync(OperationContext context) { SuspendProcessing(context).ThrowIfFailure(); if (_partitionSender != null) { await _partitionSender.CloseAsync(); } if (_eventHubClient != null) { await _eventHubClient.CloseAsync(); } return(await base.ShutdownCoreAsync(context)); }
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(); } }
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(); } }
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(); }
/// <inheritdoc /> protected override async Task <BoolResult> ShutdownCoreAsync(OperationContext context) { // Need to dispose nagle queue first to ensure last batch is processed before buffers are disposed var result = await base.ShutdownCoreAsync(context); _partitionSender?.CloseAsync(); _eventHubClient?.CloseAsync(); if (_eventProcessingBlocks != null) { foreach (var eventProcessingBlock in _eventProcessingBlocks) { eventProcessingBlock.Complete(); await eventProcessingBlock.Completion; } } UnregisterEventProcessorIfNecessary(); return(result); }
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(); } } }
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(); } }