Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit b70ddf6

Browse files
authored
Allow sending empty collection by skipping the actual send (#642)
* Allow sending empty collection by skipping the actual send * Rename test * Improve test and add a message to help in case result is flaky * Remove unnecessary check covered by foreach
1 parent a4e4abb commit b70ddf6

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Microsoft.Azure.ServiceBus/Core/MessageSender.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ public async Task SendAsync(IList<Message> messageList)
237237
this.ThrowIfClosed();
238238

239239
var count = MessageSender.ValidateMessages(messageList);
240+
if (count <= 0)
241+
{
242+
return;
243+
}
244+
240245
MessagingEventSource.Log.MessageSendStart(this.ClientId, count);
241246

242247
bool isDiagnosticSourceEnabled = ServiceBusDiagnosticSource.IsEnabled();

test/Microsoft.Azure.ServiceBus.UnitTests/SenderReceiverTests.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public async Task ClientThrowsObjectDisposedExceptionWhenUserCloseConnectionAndW
376376

377377
var recivedMessage = await receiver.ReceiveAsync().ConfigureAwait(false);
378378
Assert.True(Encoding.UTF8.GetString(recivedMessage.Body) == Encoding.UTF8.GetString(messageBody));
379-
379+
380380
var connection = sender.ServiceBusConnection;
381381
Assert.Throws<ObjectDisposedException>(() => new MessageSender(connection, TestConstants.PartitionedQueueName));
382382
}
@@ -413,7 +413,7 @@ public async Task SendMesageCloseConnectionCreateAnotherConnectionSendAgainMessa
413413

414414
messageBody = Encoding.UTF8.GetBytes("Message 2");
415415
message = new Message(messageBody);
416-
await sender.SendAsync(message);
416+
await sender.SendAsync(message);
417417

418418
recivedMessage = await receiver.ReceiveAsync().ConfigureAwait(false);
419419
Assert.True(Encoding.UTF8.GetString(recivedMessage.Body) == Encoding.UTF8.GetString(messageBody));
@@ -459,5 +459,27 @@ public async Task ClientsUseGlobalConnectionCloseFirstClientSecoundClientShouldS
459459
await receiver.CloseAsync().ConfigureAwait(false);
460460
}
461461
}
462+
463+
[Theory]
464+
[InlineData(TestConstants.NonPartitionedQueueName)]
465+
[DisplayTestMethodName]
466+
async Task MessageSenderShouldNotThrowWhenSendingEmptyCollection(string queueName)
467+
{
468+
var sender = new MessageSender(TestUtility.NamespaceConnectionString, queueName);
469+
var receiver = new MessageReceiver(TestUtility.NamespaceConnectionString, queueName, ReceiveMode.ReceiveAndDelete);
470+
471+
try
472+
{
473+
await sender.SendAsync(new List<Message>());
474+
var message = await receiver.ReceiveAsync(TimeSpan.FromSeconds(3));
475+
Assert.True(message == null, "Expected not to find any messages, but a message was received.");
476+
}
477+
finally
478+
{
479+
await sender.CloseAsync();
480+
await receiver.CloseAsync();
481+
}
482+
}
483+
462484
}
463485
}

0 commit comments

Comments
 (0)