public void ClearRespectsTheBatchLock() { var mockBatch = new MockTransportBatch(); var batch = new ServiceBusMessageBatch(mockBatch); var messageData = new ServiceBusMessage(new byte[] { 0x21 }); Assert.That(batch.TryAdd(new ServiceBusMessage(new byte[] { 0x21 })), Is.True, "The message should have been accepted before locking."); batch.Lock(); Assert.That(() => batch.Clear(), Throws.InstanceOf <InvalidOperationException>(), "The batch should not accept messages when locked."); batch.Unlock(); batch.Clear(); Assert.That(mockBatch.ClearInvoked, Is.True, "The batch should have been cleared after unlocking."); }
public async Task CanSendLargeMessageBatch(bool enableTracing) { await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: true, enableSession: true)) { TestDiagnosticListener listener = null; if (enableTracing) { listener = new TestDiagnosticListener(EntityScopeFactory.DiagnosticNamespace); } try { await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString); ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); await AddAndSendMessages(); batch.Clear(); Assert.AreEqual(0, batch.Count); Assert.AreEqual(0, batch.SizeInBytes); await AddAndSendMessages(); async Task AddAndSendMessages() { // service limits to 4500 messages but we have not added this to our client validation yet while (batch.Count < 4500 && batch.TryAddMessage( new ServiceBusMessage(new byte[50]) { MessageId = "new message ID that takes up some space", SessionId = "sessionId", PartitionKey = "sessionId", ApplicationProperties = { { "key", "value" } } })) { } if (batch.Count < 4500) { var diff = batch.MaxSizeInBytes - batch.SizeInBytes; // the difference in size from the max allowable size should be less than the size of 1 message Assert.IsTrue(diff < 220, diff.ToString()); } Assert.Greater(batch.Count, 0); await sender.SendMessagesAsync(batch); } } finally { listener?.Dispose(); } } }