From 4b95a71046002141f503c68f7163e205e80c1764 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Wed, 9 Feb 2022 23:01:54 +0100 Subject: [PATCH 01/10] Remove the LINQ allocations for all outgoing messages that are batched --- .../src/Amqp/AmqpMessageBatch.cs | 6 +-- .../src/Amqp/AmqpMessageConverter.cs | 50 +++++++++++-------- .../src/Amqp/AmqpSender.cs | 5 +- .../src/Core/TransportMessageBatch.cs | 2 +- .../src/Primitives/ServiceBusModelFactory.cs | 2 +- .../src/Sender/ServiceBusMessageBatch.cs | 2 +- .../src/Sender/ServiceBusSender.cs | 2 +- .../tests/Amqp/AmqpMessageBatchTests.cs | 20 ++++---- .../Diagnostics/DiagnosticScopeLiveTests.cs | 2 +- .../tests/Diagnostics/EventSourceLiveTests.cs | 4 +- .../tests/Message/MessageBatchTests.cs | 2 +- .../tests/Receiver/ReceiverLiveTests.cs | 24 ++++----- .../Receiver/SessionReceiverLiveTests.cs | 20 ++++---- .../Sender/ServiceBusMessageBatchTests.cs | 14 +++--- 14 files changed, 81 insertions(+), 74 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs index 82458f782338d..47366a2684e0f 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs @@ -102,7 +102,7 @@ public override bool TryAddMessage(ServiceBusMessage message) { // Initialize the size by reserving space for the batch envelope taking into account the properties from the first // message which will be used to populate properties on the batch envelope. - amqpMessage = AmqpMessageConverter.BatchSBMessagesAsAmqpMessage(new ServiceBusMessage[] { message }, forceBatch: true); + amqpMessage = AmqpMessageConverter.BatchSBMessagesAsAmqpMessage(message, forceBatch: true); } else { @@ -154,14 +154,14 @@ public override void Clear() /// /// The set of messages as an enumerable of the requested type. /// - public override IEnumerable AsEnumerable() + public override IReadOnlyCollection AsReadOnly() { if (typeof(T) != typeof(ServiceBusMessage)) { throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedTransportEventType, typeof(T).Name)); } - return (IEnumerable)BatchMessages; + return (IReadOnlyCollection) BatchMessages; } /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs index 7a001436aa6cd..02ea3d4613f63 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs @@ -30,7 +30,14 @@ internal static class AmqpMessageConverter /// The size, in bytes, to use as a buffer for stream operations. private const int StreamBufferSizeInBytes = 512; - public static AmqpMessage BatchSBMessagesAsAmqpMessage(IEnumerable source, bool forceBatch = false) + public static AmqpMessage BatchSBMessagesAsAmqpMessage(ServiceBusMessage source, bool forceBatch = false) + { + Argument.AssertNotNull(source, nameof(source)); + var batchMessages = new List(1) { SBMessageToAmqpMessage(source) }; + return BuildAmqpBatchFromMessages(batchMessages, source, forceBatch); + } + + public static AmqpMessage BatchSBMessagesAsAmqpMessage(IReadOnlyCollection source, bool forceBatch = false) { Argument.AssertNotNull(source, nameof(source)); return BuildAmqpBatchFromMessage(source, forceBatch); @@ -46,25 +53,27 @@ public static AmqpMessage BatchSBMessagesAsAmqpMessage(IEnumerableThe batch containing the source messages. /// - private static AmqpMessage BuildAmqpBatchFromMessage(IEnumerable source, bool forceBatch) + private static AmqpMessage BuildAmqpBatchFromMessage(IReadOnlyCollection source, bool forceBatch) { AmqpMessage firstAmqpMessage = null; ServiceBusMessage firstMessage = null; - return BuildAmqpBatchFromMessages( - source.Select(sbMessage => + var batchMessages = new List(source.Count); + foreach (ServiceBusMessage sbMessage in source) + { + if (firstAmqpMessage == null) { - if (firstAmqpMessage == null) - { - firstAmqpMessage = SBMessageToAmqpMessage(sbMessage); - firstMessage = sbMessage; - return firstAmqpMessage; - } - else - { - return SBMessageToAmqpMessage(sbMessage); - } - }).ToList(), firstMessage, forceBatch); + firstAmqpMessage = SBMessageToAmqpMessage(sbMessage); + firstMessage = sbMessage; + batchMessages.Add(firstAmqpMessage); + } + else + { + batchMessages.Add(SBMessageToAmqpMessage(sbMessage)); + } + } + + return BuildAmqpBatchFromMessages(batchMessages, firstMessage, forceBatch); } /// @@ -78,7 +87,7 @@ private static AmqpMessage BuildAmqpBatchFromMessage(IEnumerableThe batch containing the source messages. /// private static AmqpMessage BuildAmqpBatchFromMessages( - IList batchMessages, + List batchMessages, ServiceBusMessage firstMessage, bool forceBatch) { @@ -90,13 +99,14 @@ private static AmqpMessage BuildAmqpBatchFromMessages( } else { - batchEnvelope = AmqpMessage.Create(batchMessages.Select(message => + var data = new List(batchMessages.Count); + foreach (var message in batchMessages) { message.Batchable = true; using var messageStream = message.ToStream(); - return new Data { Value = ReadStreamToArraySegment(messageStream) }; - })); - + data.Add(new Data { Value = ReadStreamToArraySegment(messageStream) }); + } + batchEnvelope = AmqpMessage.Create(data); batchEnvelope.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs index f7f2f3655678f..380f6a9566af5 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs @@ -217,7 +217,7 @@ await sender.SendBatchInternalAsync( timeout, token).ConfigureAwait(false); }, - (this, messageBatch.AsEnumerable()), + (this, messageBatch.AsReadOnly()), _connectionScope, cancellationToken).ConfigureAwait(false); } @@ -231,11 +231,10 @@ await sender.SendBatchInternalAsync( /// An optional instance to signal the request to cancel the operation. /// internal virtual async Task SendBatchInternalAsync( - IEnumerable messages, + IReadOnlyCollection messages, TimeSpan timeout, CancellationToken cancellationToken) { - var stopWatch = ValueStopwatch.StartNew(); var link = default(SendingAmqpLink); try diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs index 69c3410ef6556..eb232392bbd2c 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs @@ -61,7 +61,7 @@ internal abstract class TransportMessageBatch : IDisposable /// /// The set of messages as an enumerable of the requested type. /// - public abstract IEnumerable AsEnumerable(); + public abstract IReadOnlyCollection AsReadOnly(); /// /// Performs the task needed to clean up resources used by the . diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs index 892398deb2ff5..929e73f5a7979 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs @@ -504,7 +504,7 @@ public override bool TryAddMessage(ServiceBusMessage message) /// /// The set of events as an enumerable of the requested type. /// - public override IEnumerable AsEnumerable() => (IEnumerable)_backingStore; + public override IReadOnlyCollection AsReadOnly() => new List((IEnumerable)_backingStore); /// /// Performs the task needed to clean up resources used by the . diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs index e108fec8485a9..844d5bcca3522 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs @@ -140,7 +140,7 @@ internal void Clear() /// /// The set of messages as an enumerable of the requested type. /// - internal IEnumerable AsEnumerable() => _innerBatch.AsEnumerable(); + internal IReadOnlyCollection AsReadOnly() => _innerBatch.AsReadOnly(); /// /// Locks the batch to prevent new messages from being added while a service diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs index d40906b6f43f8..0dd9a94c2f68e 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs @@ -325,7 +325,7 @@ public virtual async Task SendMessagesAsync( cancellationToken.ThrowIfCancellationRequested(); Logger.SendMessageStart(Identifier, messageBatch.Count); using DiagnosticScope scope = CreateDiagnosticScope( - messageBatch.AsEnumerable(), + messageBatch.AsReadOnly(), DiagnosticProperty.SendActivityName); scope.Start(); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs index ef41cdab4c11b..a60b3adf5cb5d 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs @@ -166,26 +166,26 @@ public void TryAddSetsTheCount() } /// - /// Verifies functionality of the + /// Verifies functionality of the /// method. /// /// [Test] - public void AsEnumerableValidatesTheTypeParameter() + public void AsReadOnlyValidatesTheTypeParameter() { var options = new CreateMessageBatchOptions { MaxSizeInBytes = 5000 }; var batch = new AmqpMessageBatch(options); - Assert.That(() => batch.AsEnumerable(), Throws.InstanceOf()); + Assert.That(() => batch.AsReadOnly(), Throws.InstanceOf()); } /// - /// Verifies functionality of the + /// Verifies functionality of the /// method. /// /// [Test] - public void AsEnumerableReturnsTheMessages() + public void AsReadOnlyReturnsTheMessages() { var maximumSize = 5000; var options = new CreateMessageBatchOptions { MaxSizeInBytes = maximumSize }; @@ -199,15 +199,13 @@ public void AsEnumerableReturnsTheMessages() batch.TryAddMessage(batchMessages[index]); } - IEnumerable batchEnumerable = batch.AsEnumerable(); - Assert.That(batchEnumerable, Is.Not.Null, "The batch enumerable should have been populated."); - - var batchEnumerableList = batchEnumerable.ToList(); - Assert.That(batchEnumerableList.Count, Is.EqualTo(batch.Count), "The wrong number of messages was in the enumerable."); + var batchReadOnly = batch.AsReadOnly(); + Assert.That(batchReadOnly, Is.Not.Null, "The batch enumerable should have been populated."); + Assert.That(batchReadOnly.Count, Is.EqualTo(batch.Count), "The wrong number of messages was in the enumerable."); for (var index = 0; index < batchMessages.Length; ++index) { - Assert.That(batchEnumerableList.Contains(batchMessages[index]), $"The message at index: { index } was not in the enumerable."); + Assert.That(batchReadOnly.Contains(batchMessages[index]), $"The message at index: { index } was not in the enumerable."); } } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs index d4004199b86be..7d893c87edd7c 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs @@ -161,7 +161,7 @@ public async Task SenderReceiverActivities(bool useSessions) batch.TryAddMessage(ServiceBusTestUtilities.GetMessage(sessionId)); } await sender.SendMessagesAsync(batch); - AssertSendActivities(useSessions, sender, batch.AsEnumerable()); + AssertSendActivities(useSessions, sender, batch.AsReadOnly()); }; } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs index 0ef08f0ea3796..b23a9fe816cbd 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs @@ -48,7 +48,7 @@ public async Task LogsEvents() _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchStartEvent, e => e.Payload.Contains(sender.Identifier)); _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchCompleteEvent, e => e.Payload.Contains(sender.Identifier)); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); _listener.SingleEventById(ServiceBusEventSource.CreateSendLinkStartEvent, e => e.Payload.Contains(sender.Identifier)); @@ -163,7 +163,7 @@ public async Task LogsSessionEvents() _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchStartEvent, e => e.Payload.Contains(sender.Identifier)); _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchCompleteEvent, e => e.Payload.Contains(sender.Identifier)); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsReadOnly(); await sender.SendMessagesAsync(batch); _listener.SingleEventById(ServiceBusEventSource.CreateSendLinkStartEvent, e => e.Payload.Contains(sender.Identifier)); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs index 44e6f4d50d21d..208204fa2bf66 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs @@ -31,7 +31,7 @@ public void EventDataBatchRespectsTheTryAddCallback() Assert.That(() => batch.TryAddMessage(new ServiceBusMessage(new BinaryData("Too many"))), Is.False, "The batch is full; a second attempt to add a new event should not succeed."); Assert.That(store.Count, Is.EqualTo(eventLimit), "The batch should be at its limit after the failed TryAdd attempts."); - Assert.That(batch.AsEnumerable(), Is.EquivalentTo(store), "The batch enumerable should reflect the events in the backing store."); + Assert.That(batch.AsReadOnly(), Is.EquivalentTo(store), "The batch enumerable should reflect the events in the backing store."); } /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs index 8eead15624ee5..56bf46a03adaa 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs @@ -25,7 +25,7 @@ public async Task PeekUsingConnectionStringWithSharedKey() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsEnumerable(); + IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -85,7 +85,7 @@ public async Task PeekUsingConnectionStringWithSas() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsEnumerable(); + IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -141,7 +141,7 @@ public async Task ReceiveMessagesWhenQueueEmpty() var messageCount = 2; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); var receiver = client.CreateReceiver(scope.QueueName); @@ -173,7 +173,7 @@ public async Task CancellingDoesNotLoseMessages(bool prefetch) var messageCount = 10; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); var receiver = client.CreateReceiver( scope.QueueName, @@ -244,7 +244,7 @@ public async Task ReceiveMessagesInPeekLockMode() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -281,7 +281,7 @@ public async Task CompleteMessages() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -317,7 +317,7 @@ public async Task ServerBusyRespected() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -391,7 +391,7 @@ public async Task AbandonMessages() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -500,7 +500,7 @@ public async Task DeferMessagesList() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -555,7 +555,7 @@ public async Task DeferMessagesArray() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -604,7 +604,7 @@ public async Task DeferMessagesEnumerable() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -688,7 +688,7 @@ public async Task ReceiveMessagesInReceiveAndDeleteMode() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs index cfafb234d9390..030adb8c841a8 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs @@ -30,7 +30,7 @@ public async Task PeekSession(long? sequenceNumber) // send the messages using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt, sessionId, sessionId) - .AsEnumerable(); + .AsReadOnly(); await sender.SendMessagesAsync(batch); Dictionary sentMessageIdToMsg = new Dictionary(); @@ -218,7 +218,7 @@ public async Task ReceiveMessagesWhenQueueEmpty() var sessionId = "sessionId1"; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); ServiceBusReceiver receiver = await client.AcceptNextSessionAsync( @@ -259,7 +259,7 @@ public async Task ReceiveMessagesInPeekLockMode() var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -301,7 +301,7 @@ public async Task ReceiveMessagesInReceiveAndDeleteMode() var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -348,7 +348,7 @@ public async Task CompleteMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -389,7 +389,7 @@ public async Task AbandonMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -446,7 +446,7 @@ public async Task DeadLetterMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -511,7 +511,7 @@ public async Task DeadLetterMessagesSubscription(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); var topicName = scope.TopicName; @@ -587,7 +587,7 @@ public async Task DeferMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -955,7 +955,7 @@ public async Task CancellingDoesNotLoseSessionMessages(bool prefetch) var messageCount = 10; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsEnumerable(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsReadOnly(); await sender.SendMessagesAsync(batch); var receiver = await client.AcceptSessionAsync( scope.QueueName, diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs index dc280c191f774..a1da726b552d2 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs @@ -83,20 +83,20 @@ public void TryAddIsDelegatedToTheTransportClient() } /// - /// Verifies property accessors for the + /// Verifies property accessors for the /// method. /// /// [Test] - public void AsEnumerableIsDelegatedToTheTransportClient() + public void AsReadOnlyIsDelegatedToTheTransportClient() { var mockBatch = new MockTransportBatch(); var mockScope = new EntityScopeFactory("mock", "mock"); var batch = new ServiceBusMessageBatch(mockBatch, mockScope); - batch.AsEnumerable(); - Assert.That(mockBatch.AsEnumerableCalledWith, Is.EqualTo(typeof(string)), "The enumerable should delegated the requested type parameter."); + batch.AsReadOnly(); + Assert.That(mockBatch.AsReadOnlyCalledWith, Is.EqualTo(typeof(string)), "The enumerable should delegated the requested type parameter."); } /// @@ -211,7 +211,7 @@ private class MockTransportBatch : TransportMessageBatch { public bool DisposeInvoked = false; public bool ClearInvoked = false; - public Type AsEnumerableCalledWith = null; + public Type AsReadOnlyCalledWith = null; public ServiceBusMessage TryAddCalledWith = null; public override long MaxSizeInBytes { get; } = 200; @@ -226,9 +226,9 @@ public override bool TryAddMessage(ServiceBusMessage message) return true; } - public override IEnumerable AsEnumerable() + public override IReadOnlyCollection AsReadOnly() { - AsEnumerableCalledWith = typeof(T); + AsReadOnlyCalledWith = typeof(T); return default; } } From bece3ea2b0ace5c8fd2729c1a76663b964faa00b Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Wed, 9 Feb 2022 23:02:15 +0100 Subject: [PATCH 02/10] Array.Empty for my eyes --- .../tests/Amqp/AmqpMessageBatchTests.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs index a60b3adf5cb5d..614dce0b3b0bf 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs @@ -76,7 +76,7 @@ public void TryAddValidatesNotDisposed() var batch = new AmqpMessageBatch(new CreateMessageBatchOptions { MaxSizeInBytes = 25 }); batch.Dispose(); - Assert.That(() => batch.TryAddMessage(new ServiceBusMessage(new byte[0])), Throws.InstanceOf()); + Assert.That(() => batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Throws.InstanceOf()); } /// @@ -107,7 +107,7 @@ public void TryAddAcceptsAMessageSmallerThanTheMaximumSize() var batch = new AmqpMessageBatch(options); - Assert.That(batch.TryAddMessage(new ServiceBusMessage(new byte[0])), Is.True); + Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Is.True); } /// @@ -159,7 +159,7 @@ public void TryAddSetsTheCount() for (var index = 0; index < messages.Length; ++index) { - Assert.That(batch.TryAddMessage(new ServiceBusMessage(new byte[0])), Is.True, $"The addition for index: { index } should fit and be accepted."); + Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Is.True, $"The addition for index: { index } should fit and be accepted."); } Assert.That(batch.Count, Is.EqualTo(messages.Length), "The count should have been set when the batch was updated."); @@ -195,7 +195,7 @@ public void AsReadOnlyReturnsTheMessages() for (var index = 0; index < batchMessages.Length; ++index) { - batchMessages[index] = new ServiceBusMessage(new byte[0]); + batchMessages[index] = new ServiceBusMessage(Array.Empty()); batch.TryAddMessage(batchMessages[index]); } @@ -231,7 +231,7 @@ public void ClearClearsTheCount() for (var index = 0; index < messages.Length; ++index) { - Assert.That(batch.TryAddMessage(new ServiceBusMessage(new byte[0])), Is.True, $"The addition for index: { index } should fit and be accepted."); + Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Is.True, $"The addition for index: { index } should fit and be accepted."); } Assert.That(batch.Count, Is.EqualTo(messages.Length), "The count should have been set when the batch was updated."); @@ -262,7 +262,7 @@ public void ClearClearsTheSize() for (var index = 0; index < messages.Length; ++index) { - Assert.That(batch.TryAddMessage(new ServiceBusMessage(new byte[0])), Is.True, $"The addition for index: { index } should fit and be accepted."); + Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Is.True, $"The addition for index: { index } should fit and be accepted."); } Assert.That(batch.SizeInBytes, Is.GreaterThan(0), "The size should have been set when the batch was updated."); @@ -293,7 +293,7 @@ public void DisposeClearsTheCount() for (var index = 0; index < messages.Length; ++index) { - Assert.That(batch.TryAddMessage(new ServiceBusMessage(new byte[0])), Is.True, $"The addition for index: { index } should fit and be accepted."); + Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Is.True, $"The addition for index: { index } should fit and be accepted."); } Assert.That(batch.Count, Is.EqualTo(messages.Length), "The count should have been set when the batch was updated."); @@ -324,7 +324,7 @@ public void DisposeClearsTheSize() for (var index = 0; index < messages.Length; ++index) { - Assert.That(batch.TryAddMessage(new ServiceBusMessage(new byte[0])), Is.True, $"The addition for index: { index } should fit and be accepted."); + Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty())), Is.True, $"The addition for index: { index } should fit and be accepted."); } Assert.That(batch.SizeInBytes, Is.GreaterThan(0), "The size should have been set when the batch was updated."); From 680fccd84deaf6b5612f33313c06a2554aa515f5 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Thu, 10 Feb 2022 11:31:31 +0100 Subject: [PATCH 03/10] Mock the transport batch properly --- .../tests/Diagnostics/EventSourceTests.cs | 4 ++++ .../Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs index 5ebf3fc647518..38ed7a0c782c0 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs @@ -128,6 +128,10 @@ public async Task SendBatchOfMessagesLogsEvents() .Setup(transport => transport.Count) .Returns(3); + mockTransportBatch + .Setup(transport => transport.AsReadOnly()) + .Returns(new List()); + mockTransportSender.Setup( sender => sender.CreateMessageBatchAsync( It.IsAny(), diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs index 8fef13d966ead..73aa1a3637e20 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs @@ -160,6 +160,10 @@ public async Task SendBatchManagesLockingTheBatch() .Setup(transport => transport.Count) .Returns(1); + mockTransportBatch + .Setup(transport => transport.AsReadOnly()) + .Returns(new List()); + mockTransportSender .Setup(transport => transport.SendBatchAsync(It.IsAny(), It.IsAny())) .Returns(async () => await Task.WhenAny(completionSource.Task, Task.Delay(Timeout.Infinite, cancellationSource.Token))); From 7b28463d24d12fad7acbd97eb03930c0c518616d Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Thu, 10 Feb 2022 18:45:48 +0100 Subject: [PATCH 04/10] Cleanup no longer needed using --- .../src/Diagnostics/DiagnosticExtensions.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs index a34a4d2d721e4..1f7ed7c73d2a0 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs @@ -4,7 +4,6 @@ using System; using System.Text; using Azure.Core.Pipeline; -using System.Linq; using System.Collections.Generic; namespace Azure.Messaging.ServiceBus.Diagnostics From 44a7101a0fa40c3dcf483e441b31eb48480b2529 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Thu, 10 Feb 2022 19:16:25 +0100 Subject: [PATCH 05/10] Switch case over the backing store --- .../src/Primitives/ServiceBusModelFactory.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs index 929e73f5a7979..d1e1337b94660 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs @@ -504,7 +504,12 @@ public override bool TryAddMessage(ServiceBusMessage message) /// /// The set of events as an enumerable of the requested type. /// - public override IReadOnlyCollection AsReadOnly() => new List((IEnumerable)_backingStore); + public override IReadOnlyCollection AsReadOnly() => + _backingStore switch + { + IReadOnlyCollection collection => collection, + _ => new List((IEnumerable)_backingStore) + }; /// /// Performs the task needed to clean up resources used by the . From 19cd8112ae9af9189f8dc4257fd53fb31b3c3356 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Sun, 13 Feb 2022 10:53:04 +0100 Subject: [PATCH 06/10] Use directly the concrete collection --- .../src/Amqp/AmqpMessageBatch.cs | 4 ++-- .../src/Amqp/AmqpMessageConverter.cs | 4 ++-- .../src/Amqp/AmqpSender.cs | 6 ++--- .../src/Core/TransportMessageBatch.cs | 2 +- .../src/Core/TransportSender.cs | 2 +- .../src/Primitives/ServiceBusModelFactory.cs | 12 +++++----- .../src/Sender/ServiceBusMessageBatch.cs | 2 +- .../src/Sender/ServiceBusSender.cs | 16 ++++++------- .../tests/Amqp/AmqpMessageBatchTests.cs | 8 +++---- .../Diagnostics/DiagnosticScopeLiveTests.cs | 2 +- .../tests/Diagnostics/EventSourceLiveTests.cs | 4 ++-- .../tests/Diagnostics/EventSourceTests.cs | 4 ++-- .../tests/Message/MessageBatchTests.cs | 2 +- .../tests/Receiver/ReceiverLiveTests.cs | 24 +++++++++---------- .../Receiver/SessionReceiverLiveTests.cs | 20 ++++++++-------- .../tests/Sender/SenderTests.cs | 2 +- .../Sender/ServiceBusMessageBatchTests.cs | 6 ++--- 17 files changed, 60 insertions(+), 60 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs index 47366a2684e0f..b251d827c33e4 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs @@ -154,14 +154,14 @@ public override void Clear() /// /// The set of messages as an enumerable of the requested type. /// - public override IReadOnlyCollection AsReadOnly() + public override List AsList() { if (typeof(T) != typeof(ServiceBusMessage)) { throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedTransportEventType, typeof(T).Name)); } - return (IReadOnlyCollection) BatchMessages; + return BatchMessages as List; } /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs index 02ea3d4613f63..36f5ccb3938d6 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs @@ -37,7 +37,7 @@ public static AmqpMessage BatchSBMessagesAsAmqpMessage(ServiceBusMessage source, return BuildAmqpBatchFromMessages(batchMessages, source, forceBatch); } - public static AmqpMessage BatchSBMessagesAsAmqpMessage(IReadOnlyCollection source, bool forceBatch = false) + public static AmqpMessage BatchSBMessagesAsAmqpMessage(List source, bool forceBatch = false) { Argument.AssertNotNull(source, nameof(source)); return BuildAmqpBatchFromMessage(source, forceBatch); @@ -53,7 +53,7 @@ public static AmqpMessage BatchSBMessagesAsAmqpMessage(IReadOnlyCollectionThe batch containing the source messages. /// - private static AmqpMessage BuildAmqpBatchFromMessage(IReadOnlyCollection source, bool forceBatch) + private static AmqpMessage BuildAmqpBatchFromMessage(List source, bool forceBatch) { AmqpMessage firstAmqpMessage = null; ServiceBusMessage firstMessage = null; diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs index 380f6a9566af5..001ea9ba73d4a 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs @@ -217,7 +217,7 @@ await sender.SendBatchInternalAsync( timeout, token).ConfigureAwait(false); }, - (this, messageBatch.AsReadOnly()), + (this, messageBatch.AsList()), _connectionScope, cancellationToken).ConfigureAwait(false); } @@ -231,7 +231,7 @@ await sender.SendBatchInternalAsync( /// An optional instance to signal the request to cancel the operation. /// internal virtual async Task SendBatchInternalAsync( - IReadOnlyCollection messages, + List messages, TimeSpan timeout, CancellationToken cancellationToken) { @@ -301,7 +301,7 @@ internal virtual async Task SendBatchInternalAsync( /// The list of messages to send. /// An optional instance to signal the request to cancel the operation. public override async Task SendAsync( - IReadOnlyList messages, + List messages, CancellationToken cancellationToken) { await _retryPolicy.RunOperation(static async (value, timeout, token) => diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs index eb232392bbd2c..58f10d7cc446b 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs @@ -61,7 +61,7 @@ internal abstract class TransportMessageBatch : IDisposable /// /// The set of messages as an enumerable of the requested type. /// - public abstract IReadOnlyCollection AsReadOnly(); + public abstract List AsList(); /// /// Performs the task needed to clean up resources used by the . diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs index 0451fea53756d..8757a59a017ff 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs @@ -57,7 +57,7 @@ public abstract ValueTask CreateMessageBatchAsync( /// The list of messages to send. /// An optional instance to signal the request to cancel the operation. public abstract Task SendAsync( - IReadOnlyList messages, + List messages, CancellationToken cancellationToken); /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs index d1e1337b94660..965975fb308f8 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs @@ -504,12 +504,12 @@ public override bool TryAddMessage(ServiceBusMessage message) /// /// The set of events as an enumerable of the requested type. /// - public override IReadOnlyCollection AsReadOnly() => - _backingStore switch - { - IReadOnlyCollection collection => collection, - _ => new List((IEnumerable)_backingStore) - }; + public override List AsList() => _backingStore switch + { + List storeList => storeList, + IList storeIList => new List(storeIList), + _ => _backingStore as List + }; /// /// Performs the task needed to clean up resources used by the . diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs index 844d5bcca3522..c29da0a3e44ba 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs @@ -140,7 +140,7 @@ internal void Clear() /// /// The set of messages as an enumerable of the requested type. /// - internal IReadOnlyCollection AsReadOnly() => _innerBatch.AsReadOnly(); + internal List AsList() => _innerBatch.AsList(); /// /// Locks the batch to prevent new messages from being added while a service diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs index 0dd9a94c2f68e..2cf67fd5a7bf9 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs @@ -190,9 +190,9 @@ public virtual async Task SendMessagesAsync( Argument.AssertNotDisposed(IsClosed, nameof(ServiceBusSender)); _connection.ThrowIfClosed(); - IReadOnlyList messageList = messages switch + List messageList = messages switch { - IReadOnlyList alreadyList => alreadyList, + List alreadyList => alreadyList, _ => messages.ToList() }; @@ -203,7 +203,7 @@ public virtual async Task SendMessagesAsync( cancellationToken.ThrowIfCancellationRequested(); Logger.SendMessageStart(Identifier, messageCount: messageList.Count); - using DiagnosticScope scope = CreateDiagnosticScope(messages, DiagnosticProperty.SendActivityName); + using DiagnosticScope scope = CreateDiagnosticScope(messageList, DiagnosticProperty.SendActivityName); scope.Start(); try @@ -223,7 +223,7 @@ await _innerSender.SendAsync( Logger.SendMessageComplete(Identifier); } - private DiagnosticScope CreateDiagnosticScope(IEnumerable messages, string activityName) + private DiagnosticScope CreateDiagnosticScope(List messages, string activityName) { foreach (ServiceBusMessage message in messages) { @@ -325,7 +325,7 @@ public virtual async Task SendMessagesAsync( cancellationToken.ThrowIfCancellationRequested(); Logger.SendMessageStart(Identifier, messageBatch.Count); using DiagnosticScope scope = CreateDiagnosticScope( - messageBatch.AsReadOnly(), + messageBatch.AsList(), DiagnosticProperty.SendActivityName); scope.Start(); @@ -405,9 +405,9 @@ public virtual async Task> ScheduleMessagesAsync( _connection.ThrowIfClosed(); cancellationToken.ThrowIfCancellationRequested(); - IReadOnlyList messageList = messages switch + List messageList = messages switch { - IReadOnlyList alreadyList => alreadyList, + List alreadyList => alreadyList, _ => messages.ToList() }; @@ -422,7 +422,7 @@ public virtual async Task> ScheduleMessagesAsync( scheduledEnqueueTime.ToString(CultureInfo.InvariantCulture)); using DiagnosticScope scope = CreateDiagnosticScope( - messages, + messageList, DiagnosticProperty.ScheduleActivityName); scope.Start(); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs index 614dce0b3b0bf..1fadd5c813b7e 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs @@ -166,7 +166,7 @@ public void TryAddSetsTheCount() } /// - /// Verifies functionality of the + /// Verifies functionality of the /// method. /// /// @@ -176,11 +176,11 @@ public void AsReadOnlyValidatesTheTypeParameter() var options = new CreateMessageBatchOptions { MaxSizeInBytes = 5000 }; var batch = new AmqpMessageBatch(options); - Assert.That(() => batch.AsReadOnly(), Throws.InstanceOf()); + Assert.That(() => batch.AsList(), Throws.InstanceOf()); } /// - /// Verifies functionality of the + /// Verifies functionality of the /// method. /// /// @@ -199,7 +199,7 @@ public void AsReadOnlyReturnsTheMessages() batch.TryAddMessage(batchMessages[index]); } - var batchReadOnly = batch.AsReadOnly(); + var batchReadOnly = batch.AsList(); Assert.That(batchReadOnly, Is.Not.Null, "The batch enumerable should have been populated."); Assert.That(batchReadOnly.Count, Is.EqualTo(batch.Count), "The wrong number of messages was in the enumerable."); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs index 7d893c87edd7c..8aa9b61910480 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs @@ -161,7 +161,7 @@ public async Task SenderReceiverActivities(bool useSessions) batch.TryAddMessage(ServiceBusTestUtilities.GetMessage(sessionId)); } await sender.SendMessagesAsync(batch); - AssertSendActivities(useSessions, sender, batch.AsReadOnly()); + AssertSendActivities(useSessions, sender, batch.AsList()); }; } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs index b23a9fe816cbd..347ba6cd7f56d 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs @@ -48,7 +48,7 @@ public async Task LogsEvents() _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchStartEvent, e => e.Payload.Contains(sender.Identifier)); _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchCompleteEvent, e => e.Payload.Contains(sender.Identifier)); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); _listener.SingleEventById(ServiceBusEventSource.CreateSendLinkStartEvent, e => e.Payload.Contains(sender.Identifier)); @@ -163,7 +163,7 @@ public async Task LogsSessionEvents() _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchStartEvent, e => e.Payload.Contains(sender.Identifier)); _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchCompleteEvent, e => e.Payload.Contains(sender.Identifier)); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsList(); await sender.SendMessagesAsync(batch); _listener.SingleEventById(ServiceBusEventSource.CreateSendLinkStartEvent, e => e.Payload.Contains(sender.Identifier)); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs index 38ed7a0c782c0..8336a9a08e867 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs @@ -88,7 +88,7 @@ public void SendMessageExceptionLogsEvents() mockTransportSender.Setup( sender => sender.SendAsync( - It.IsAny>(), + It.IsAny>(), It.IsAny())) .Throws(new Exception()); @@ -129,7 +129,7 @@ public async Task SendBatchOfMessagesLogsEvents() .Returns(3); mockTransportBatch - .Setup(transport => transport.AsReadOnly()) + .Setup(transport => transport.AsList()) .Returns(new List()); mockTransportSender.Setup( diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs index 208204fa2bf66..f29d73671d1d5 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs @@ -31,7 +31,7 @@ public void EventDataBatchRespectsTheTryAddCallback() Assert.That(() => batch.TryAddMessage(new ServiceBusMessage(new BinaryData("Too many"))), Is.False, "The batch is full; a second attempt to add a new event should not succeed."); Assert.That(store.Count, Is.EqualTo(eventLimit), "The batch should be at its limit after the failed TryAdd attempts."); - Assert.That(batch.AsReadOnly(), Is.EquivalentTo(store), "The batch enumerable should reflect the events in the backing store."); + Assert.That(batch.AsList(), Is.EquivalentTo(store), "The batch enumerable should reflect the events in the backing store."); } /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs index 56bf46a03adaa..5c605b4063906 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs @@ -25,7 +25,7 @@ public async Task PeekUsingConnectionStringWithSharedKey() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsReadOnly(); + IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsList(); await sender.SendMessagesAsync(batch); @@ -85,7 +85,7 @@ public async Task PeekUsingConnectionStringWithSas() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsReadOnly(); + IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsList(); await sender.SendMessagesAsync(batch); @@ -141,7 +141,7 @@ public async Task ReceiveMessagesWhenQueueEmpty() var messageCount = 2; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); var receiver = client.CreateReceiver(scope.QueueName); @@ -173,7 +173,7 @@ public async Task CancellingDoesNotLoseMessages(bool prefetch) var messageCount = 10; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); var receiver = client.CreateReceiver( scope.QueueName, @@ -244,7 +244,7 @@ public async Task ReceiveMessagesInPeekLockMode() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); @@ -281,7 +281,7 @@ public async Task CompleteMessages() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); @@ -317,7 +317,7 @@ public async Task ServerBusyRespected() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); @@ -391,7 +391,7 @@ public async Task AbandonMessages() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); @@ -500,7 +500,7 @@ public async Task DeferMessagesList() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); @@ -555,7 +555,7 @@ public async Task DeferMessagesArray() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); @@ -604,7 +604,7 @@ public async Task DeferMessagesEnumerable() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); @@ -688,7 +688,7 @@ public async Task ReceiveMessagesInReceiveAndDeleteMode() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); await sender.SendMessagesAsync(batch); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs index 030adb8c841a8..eb3cb7fe4556a 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs @@ -30,7 +30,7 @@ public async Task PeekSession(long? sequenceNumber) // send the messages using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt, sessionId, sessionId) - .AsReadOnly(); + .AsList(); await sender.SendMessagesAsync(batch); Dictionary sentMessageIdToMsg = new Dictionary(); @@ -218,7 +218,7 @@ public async Task ReceiveMessagesWhenQueueEmpty() var sessionId = "sessionId1"; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); ServiceBusReceiver receiver = await client.AcceptNextSessionAsync( @@ -259,7 +259,7 @@ public async Task ReceiveMessagesInPeekLockMode() var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); @@ -301,7 +301,7 @@ public async Task ReceiveMessagesInReceiveAndDeleteMode() var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); @@ -348,7 +348,7 @@ public async Task CompleteMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); @@ -389,7 +389,7 @@ public async Task AbandonMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); @@ -446,7 +446,7 @@ public async Task DeadLetterMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); @@ -511,7 +511,7 @@ public async Task DeadLetterMessagesSubscription(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); var topicName = scope.TopicName; @@ -587,7 +587,7 @@ public async Task DeferMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); await sender.SendMessagesAsync(batch); @@ -955,7 +955,7 @@ public async Task CancellingDoesNotLoseSessionMessages(bool prefetch) var messageCount = 10; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsReadOnly(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsList(); await sender.SendMessagesAsync(batch); var receiver = await client.AcceptSessionAsync( scope.QueueName, diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs index 73aa1a3637e20..03b3a24fc6acd 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs @@ -161,7 +161,7 @@ public async Task SendBatchManagesLockingTheBatch() .Returns(1); mockTransportBatch - .Setup(transport => transport.AsReadOnly()) + .Setup(transport => transport.AsList()) .Returns(new List()); mockTransportSender diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs index a1da726b552d2..f3a0d4004e693 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs @@ -83,7 +83,7 @@ public void TryAddIsDelegatedToTheTransportClient() } /// - /// Verifies property accessors for the + /// Verifies property accessors for the /// method. /// /// @@ -95,7 +95,7 @@ public void AsReadOnlyIsDelegatedToTheTransportClient() var batch = new ServiceBusMessageBatch(mockBatch, mockScope); - batch.AsReadOnly(); + batch.AsList(); Assert.That(mockBatch.AsReadOnlyCalledWith, Is.EqualTo(typeof(string)), "The enumerable should delegated the requested type parameter."); } @@ -226,7 +226,7 @@ public override bool TryAddMessage(ServiceBusMessage message) return true; } - public override IReadOnlyCollection AsReadOnly() + public override List AsList() { AsReadOnlyCalledWith = typeof(T); return default; From 370e159d3ff342780516eff206963ebe57145ceb Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Sun, 13 Feb 2022 11:03:12 +0100 Subject: [PATCH 07/10] Use concrete type where possible for Diagnostic Avoid additional array allocation when receiving --- .../src/Diagnostics/DiagnosticExtensions.cs | 31 ++++++++++++++++--- .../src/Diagnostics/EntityScopeFactory.cs | 22 +++++++++++++ .../src/Processor/ReceiverManager.cs | 2 +- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs index 1f7ed7c73d2a0..7da215f5fd9a5 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs @@ -15,17 +15,22 @@ public static string GetAsciiString(this ArraySegment arraySegment) return arraySegment.Array == null ? string.Empty : Encoding.ASCII.GetString(arraySegment.Array, arraySegment.Offset, arraySegment.Count); } - public static void SetMessageData(this DiagnosticScope scope, IEnumerable messages) + public static void SetMessageData(this DiagnosticScope scope, ServiceBusReceivedMessage message) + { + scope.AddLinkedDiagnostics(message); + } + + public static void SetMessageData(this DiagnosticScope scope, IReadOnlyList messages) { scope.AddLinkedDiagnostics(messages); } - public static void SetMessageData(this DiagnosticScope scope, IEnumerable messages) + public static void SetMessageData(this DiagnosticScope scope, List messages) { scope.AddLinkedDiagnostics(messages); } - private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable messages) + private static void AddLinkedDiagnostics(this DiagnosticScope scope, IReadOnlyList messages) { if (scope.IsEnabled) { @@ -36,7 +41,15 @@ private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable } } - private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable messages) + private static void AddLinkedDiagnostics(this DiagnosticScope scope, ServiceBusReceivedMessage message) + { + if (scope.IsEnabled) + { + AddLinkedDiagnostics(scope, message.ApplicationProperties); + } + } + + private static void AddLinkedDiagnostics(this DiagnosticScope scope, List messages) { if (scope.IsEnabled) { @@ -47,6 +60,16 @@ private static void AddLinkedDiagnostics(this DiagnosticScope scope, IEnumerable } } + private static void AddLinkedDiagnostics(this DiagnosticScope scope, IReadOnlyDictionary properties) + { + if (EntityScopeFactory.TryExtractDiagnosticId( + properties, + out string diagnosticId)) + { + scope.AddLink(diagnosticId, null); + } + } + private static void AddLinkedDiagnostics(this DiagnosticScope scope, IDictionary properties) { if (EntityScopeFactory.TryExtractDiagnosticId( diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs index 4ca2df02c8441..889fb8ae81857 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/EntityScopeFactory.cs @@ -36,6 +36,28 @@ public EntityScopeFactory( _fullyQualifiedNamespace = fullyQualifiedNamespace; } + /// + /// Extracts a diagnostic id from a message's properties. + /// + /// + /// The properties holding the diagnostic id. + /// The value of the diagnostics identifier assigned to the event. + /// + /// true if the event was contained the diagnostic id; otherwise, false. + /// + public static bool TryExtractDiagnosticId(IReadOnlyDictionary properties, out string id) + { + id = null; + + if (properties.TryGetValue(DiagnosticProperty.DiagnosticIdAttribute, out var objectId) && objectId is string stringId) + { + id = stringId; + return true; + } + + return false; + } + /// /// Extracts a diagnostic id from a message's properties. /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Processor/ReceiverManager.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Processor/ReceiverManager.cs index d24f929889537..cfc84b594d1c0 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Processor/ReceiverManager.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Processor/ReceiverManager.cs @@ -118,7 +118,7 @@ await RaiseExceptionReceived( protected async Task ProcessOneMessageWithinScopeAsync(ServiceBusReceivedMessage message, string activityName, CancellationToken cancellationToken) { using DiagnosticScope scope = _scopeFactory.CreateScope(activityName, DiagnosticScope.ActivityKind.Consumer); - scope.SetMessageData(new ServiceBusReceivedMessage[] { message }); + scope.SetMessageData(message); scope.Start(); try { From 3b12dbebbd56576ec750169b5b3a4cd69ae371eb Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Sun, 13 Feb 2022 11:09:15 +0100 Subject: [PATCH 08/10] Use the concrete list type also for scheduled messages including the count --- .../Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs | 6 +++--- .../Azure.Messaging.ServiceBus/src/Core/TransportSender.cs | 2 +- .../tests/Diagnostics/EventSourceTests.cs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs index 001ea9ba73d4a..ec718c29cc426 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs @@ -375,7 +375,7 @@ private void OnManagementLinkClosed(object managementLink, EventArgs e) => /// /// public override async Task> ScheduleMessagesAsync( - IReadOnlyList messages, + List messages, CancellationToken cancellationToken = default) { return await _retryPolicy.RunOperation(static async (value, timeout, token) => @@ -400,7 +400,7 @@ public override async Task> ScheduleMessagesAsync( /// /// internal async Task> ScheduleMessageInternalAsync( - IReadOnlyList messages, + List messages, TimeSpan timeout, CancellationToken cancellationToken = default) { @@ -417,7 +417,7 @@ internal async Task> ScheduleMessageInternalAsync( request.AmqpMessage.ApplicationProperties.Map[ManagementConstants.Request.AssociatedLinkName] = sendLink.Name; } - List entries = new List(); + List entries = new List(messages.Count); foreach (ServiceBusMessage message in messages) { using AmqpMessage amqpMessage = AmqpMessageConverter.SBMessageToAmqpMessage(message); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs index 8757a59a017ff..f88dee65312c2 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs @@ -80,7 +80,7 @@ public abstract Task SendBatchAsync( /// /// public abstract Task> ScheduleMessagesAsync( - IReadOnlyList messages, + List messages, CancellationToken cancellationToken = default); /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs index 8336a9a08e867..c0a86ff32d024 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs @@ -177,7 +177,7 @@ public async Task ScheduleMessageLogsEvents() }; mockTransportSender.Setup( sender => sender.ScheduleMessagesAsync( - It.IsAny>(), + It.IsAny>(), It.IsAny())) .Returns(Task.FromResult((IReadOnlyList) new List { 1 })); @@ -214,7 +214,7 @@ public void ScheduleMessageExceptionLogsEvents() mockTransportSender.Setup( sender => sender.ScheduleMessagesAsync( - It.IsAny>(), + It.IsAny>(), It.IsAny())) .Throws(new Exception()); From 19cf8fc0c47c520b453b02b75be9be8cbfeaf689 Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Sun, 13 Feb 2022 11:45:59 +0100 Subject: [PATCH 09/10] Switch back to readonly since that seems the better strategy even though it will be slower to enumerate --- .../src/Amqp/AmqpMessageBatch.cs | 4 +-- .../src/Amqp/AmqpMessageConverter.cs | 4 +-- .../src/Amqp/AmqpSender.cs | 10 +++--- .../src/Core/TransportMessageBatch.cs | 2 +- .../src/Core/TransportSender.cs | 4 +-- .../src/Diagnostics/DiagnosticExtensions.cs | 8 ++--- .../src/Primitives/ServiceBusModelFactory.cs | 7 ++-- .../src/Sender/ServiceBusMessageBatch.cs | 2 +- .../src/Sender/ServiceBusSender.cs | 34 +++++++++---------- .../tests/Amqp/AmqpMessageBatchTests.cs | 4 +-- .../Diagnostics/DiagnosticScopeLiveTests.cs | 2 +- .../tests/Diagnostics/EventSourceLiveTests.cs | 4 +-- .../tests/Diagnostics/EventSourceTests.cs | 2 +- .../tests/Message/MessageBatchTests.cs | 2 +- .../tests/Receiver/ReceiverLiveTests.cs | 24 ++++++------- .../Receiver/SessionReceiverLiveTests.cs | 20 +++++------ .../tests/Sender/SenderTests.cs | 2 +- .../Sender/ServiceBusMessageBatchTests.cs | 6 ++-- 18 files changed, 70 insertions(+), 71 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs index b251d827c33e4..47366a2684e0f 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs @@ -154,14 +154,14 @@ public override void Clear() /// /// The set of messages as an enumerable of the requested type. /// - public override List AsList() + public override IReadOnlyCollection AsReadOnly() { if (typeof(T) != typeof(ServiceBusMessage)) { throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources.UnsupportedTransportEventType, typeof(T).Name)); } - return BatchMessages as List; + return (IReadOnlyCollection) BatchMessages; } /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs index 36f5ccb3938d6..02ea3d4613f63 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageConverter.cs @@ -37,7 +37,7 @@ public static AmqpMessage BatchSBMessagesAsAmqpMessage(ServiceBusMessage source, return BuildAmqpBatchFromMessages(batchMessages, source, forceBatch); } - public static AmqpMessage BatchSBMessagesAsAmqpMessage(List source, bool forceBatch = false) + public static AmqpMessage BatchSBMessagesAsAmqpMessage(IReadOnlyCollection source, bool forceBatch = false) { Argument.AssertNotNull(source, nameof(source)); return BuildAmqpBatchFromMessage(source, forceBatch); @@ -53,7 +53,7 @@ public static AmqpMessage BatchSBMessagesAsAmqpMessage(List s /// /// The batch containing the source messages. /// - private static AmqpMessage BuildAmqpBatchFromMessage(List source, bool forceBatch) + private static AmqpMessage BuildAmqpBatchFromMessage(IReadOnlyCollection source, bool forceBatch) { AmqpMessage firstAmqpMessage = null; ServiceBusMessage firstMessage = null; diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs index ec718c29cc426..4c1bb92469595 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpSender.cs @@ -217,7 +217,7 @@ await sender.SendBatchInternalAsync( timeout, token).ConfigureAwait(false); }, - (this, messageBatch.AsList()), + (this, messageBatch.AsReadOnly()), _connectionScope, cancellationToken).ConfigureAwait(false); } @@ -231,7 +231,7 @@ await sender.SendBatchInternalAsync( /// An optional instance to signal the request to cancel the operation. /// internal virtual async Task SendBatchInternalAsync( - List messages, + IReadOnlyCollection messages, TimeSpan timeout, CancellationToken cancellationToken) { @@ -301,7 +301,7 @@ internal virtual async Task SendBatchInternalAsync( /// The list of messages to send. /// An optional instance to signal the request to cancel the operation. public override async Task SendAsync( - List messages, + IReadOnlyCollection messages, CancellationToken cancellationToken) { await _retryPolicy.RunOperation(static async (value, timeout, token) => @@ -375,7 +375,7 @@ private void OnManagementLinkClosed(object managementLink, EventArgs e) => /// /// public override async Task> ScheduleMessagesAsync( - List messages, + IReadOnlyCollection messages, CancellationToken cancellationToken = default) { return await _retryPolicy.RunOperation(static async (value, timeout, token) => @@ -400,7 +400,7 @@ public override async Task> ScheduleMessagesAsync( /// /// internal async Task> ScheduleMessageInternalAsync( - List messages, + IReadOnlyCollection messages, TimeSpan timeout, CancellationToken cancellationToken = default) { diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs index 58f10d7cc446b..eb232392bbd2c 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportMessageBatch.cs @@ -61,7 +61,7 @@ internal abstract class TransportMessageBatch : IDisposable /// /// The set of messages as an enumerable of the requested type. /// - public abstract List AsList(); + public abstract IReadOnlyCollection AsReadOnly(); /// /// Performs the task needed to clean up resources used by the . diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs index f88dee65312c2..a4668916cc32e 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/TransportSender.cs @@ -57,7 +57,7 @@ public abstract ValueTask CreateMessageBatchAsync( /// The list of messages to send. /// An optional instance to signal the request to cancel the operation. public abstract Task SendAsync( - List messages, + IReadOnlyCollection messages, CancellationToken cancellationToken); /// @@ -80,7 +80,7 @@ public abstract Task SendBatchAsync( /// /// public abstract Task> ScheduleMessagesAsync( - List messages, + IReadOnlyCollection messages, CancellationToken cancellationToken = default); /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs index 7da215f5fd9a5..b3e59ff2bba51 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Diagnostics/DiagnosticExtensions.cs @@ -20,17 +20,17 @@ public static void SetMessageData(this DiagnosticScope scope, ServiceBusReceived scope.AddLinkedDiagnostics(message); } - public static void SetMessageData(this DiagnosticScope scope, IReadOnlyList messages) + public static void SetMessageData(this DiagnosticScope scope, IReadOnlyCollection messages) { scope.AddLinkedDiagnostics(messages); } - public static void SetMessageData(this DiagnosticScope scope, List messages) + public static void SetMessageData(this DiagnosticScope scope, IReadOnlyCollection messages) { scope.AddLinkedDiagnostics(messages); } - private static void AddLinkedDiagnostics(this DiagnosticScope scope, IReadOnlyList messages) + private static void AddLinkedDiagnostics(this DiagnosticScope scope, IReadOnlyCollection messages) { if (scope.IsEnabled) { @@ -49,7 +49,7 @@ private static void AddLinkedDiagnostics(this DiagnosticScope scope, ServiceBusR } } - private static void AddLinkedDiagnostics(this DiagnosticScope scope, List messages) + private static void AddLinkedDiagnostics(this DiagnosticScope scope, IReadOnlyCollection messages) { if (scope.IsEnabled) { diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs index 965975fb308f8..6a58886416fea 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Primitives/ServiceBusModelFactory.cs @@ -504,11 +504,10 @@ public override bool TryAddMessage(ServiceBusMessage message) /// /// The set of events as an enumerable of the requested type. /// - public override List AsList() => _backingStore switch + public override IReadOnlyCollection AsReadOnly() => _backingStore switch { - List storeList => storeList, - IList storeIList => new List(storeIList), - _ => _backingStore as List + IReadOnlyCollection readOnlyCollection => readOnlyCollection, + _ => new List((IEnumerable) _backingStore) }; /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs index c29da0a3e44ba..844d5bcca3522 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusMessageBatch.cs @@ -140,7 +140,7 @@ internal void Clear() /// /// The set of messages as an enumerable of the requested type. /// - internal List AsList() => _innerBatch.AsList(); + internal IReadOnlyCollection AsReadOnly() => _innerBatch.AsReadOnly(); /// /// Locks the batch to prevent new messages from being added while a service diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs index 2cf67fd5a7bf9..cdb280e129cbf 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Sender/ServiceBusSender.cs @@ -190,26 +190,26 @@ public virtual async Task SendMessagesAsync( Argument.AssertNotDisposed(IsClosed, nameof(ServiceBusSender)); _connection.ThrowIfClosed(); - List messageList = messages switch + IReadOnlyCollection readOnlyCollection = messages switch { - List alreadyList => alreadyList, - _ => messages.ToList() + IReadOnlyCollection alreadyReadOnlyCollection => alreadyReadOnlyCollection, + _ => messages.ToArray() }; - if (messageList.Count == 0) + if (readOnlyCollection.Count == 0) { return; } cancellationToken.ThrowIfCancellationRequested(); - Logger.SendMessageStart(Identifier, messageCount: messageList.Count); - using DiagnosticScope scope = CreateDiagnosticScope(messageList, DiagnosticProperty.SendActivityName); + Logger.SendMessageStart(Identifier, messageCount: readOnlyCollection.Count); + using DiagnosticScope scope = CreateDiagnosticScope(readOnlyCollection, DiagnosticProperty.SendActivityName); scope.Start(); try { await _innerSender.SendAsync( - messageList, + readOnlyCollection, cancellationToken).ConfigureAwait(false); } @@ -223,7 +223,7 @@ await _innerSender.SendAsync( Logger.SendMessageComplete(Identifier); } - private DiagnosticScope CreateDiagnosticScope(List messages, string activityName) + private DiagnosticScope CreateDiagnosticScope(IReadOnlyCollection messages, string activityName) { foreach (ServiceBusMessage message in messages) { @@ -325,7 +325,7 @@ public virtual async Task SendMessagesAsync( cancellationToken.ThrowIfCancellationRequested(); Logger.SendMessageStart(Identifier, messageBatch.Count); using DiagnosticScope scope = CreateDiagnosticScope( - messageBatch.AsList(), + messageBatch.AsReadOnly(), DiagnosticProperty.SendActivityName); scope.Start(); @@ -405,35 +405,35 @@ public virtual async Task> ScheduleMessagesAsync( _connection.ThrowIfClosed(); cancellationToken.ThrowIfCancellationRequested(); - List messageList = messages switch + IReadOnlyCollection readOnlyCollection = messages switch { - List alreadyList => alreadyList, - _ => messages.ToList() + IReadOnlyCollection alreadyReadOnlyCollection => alreadyReadOnlyCollection, + _ => messages.ToArray() }; - if (messageList.Count == 0) + if (readOnlyCollection.Count == 0) { return Array.Empty(); } Logger.ScheduleMessagesStart( Identifier, - messageList.Count, + readOnlyCollection.Count, scheduledEnqueueTime.ToString(CultureInfo.InvariantCulture)); using DiagnosticScope scope = CreateDiagnosticScope( - messageList, + readOnlyCollection, DiagnosticProperty.ScheduleActivityName); scope.Start(); IReadOnlyList sequenceNumbers = null; try { - foreach (ServiceBusMessage message in messageList) + foreach (ServiceBusMessage message in readOnlyCollection) { message.ScheduledEnqueueTime = scheduledEnqueueTime.UtcDateTime; } - sequenceNumbers = await _innerSender.ScheduleMessagesAsync(messageList, cancellationToken).ConfigureAwait(false); + sequenceNumbers = await _innerSender.ScheduleMessagesAsync(readOnlyCollection, cancellationToken).ConfigureAwait(false); } catch (Exception exception) { diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs index 1fadd5c813b7e..8d7bf1869e269 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Amqp/AmqpMessageBatchTests.cs @@ -176,7 +176,7 @@ public void AsReadOnlyValidatesTheTypeParameter() var options = new CreateMessageBatchOptions { MaxSizeInBytes = 5000 }; var batch = new AmqpMessageBatch(options); - Assert.That(() => batch.AsList(), Throws.InstanceOf()); + Assert.That(() => batch.AsReadOnly(), Throws.InstanceOf()); } /// @@ -199,7 +199,7 @@ public void AsReadOnlyReturnsTheMessages() batch.TryAddMessage(batchMessages[index]); } - var batchReadOnly = batch.AsList(); + var batchReadOnly = batch.AsReadOnly(); Assert.That(batchReadOnly, Is.Not.Null, "The batch enumerable should have been populated."); Assert.That(batchReadOnly.Count, Is.EqualTo(batch.Count), "The wrong number of messages was in the enumerable."); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs index 8aa9b61910480..7d893c87edd7c 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/DiagnosticScopeLiveTests.cs @@ -161,7 +161,7 @@ public async Task SenderReceiverActivities(bool useSessions) batch.TryAddMessage(ServiceBusTestUtilities.GetMessage(sessionId)); } await sender.SendMessagesAsync(batch); - AssertSendActivities(useSessions, sender, batch.AsList()); + AssertSendActivities(useSessions, sender, batch.AsReadOnly()); }; } diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs index 347ba6cd7f56d..b23a9fe816cbd 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceLiveTests.cs @@ -48,7 +48,7 @@ public async Task LogsEvents() _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchStartEvent, e => e.Payload.Contains(sender.Identifier)); _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchCompleteEvent, e => e.Payload.Contains(sender.Identifier)); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); _listener.SingleEventById(ServiceBusEventSource.CreateSendLinkStartEvent, e => e.Payload.Contains(sender.Identifier)); @@ -163,7 +163,7 @@ public async Task LogsSessionEvents() _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchStartEvent, e => e.Payload.Contains(sender.Identifier)); _listener.SingleEventById(ServiceBusEventSource.CreateMessageBatchCompleteEvent, e => e.Payload.Contains(sender.Identifier)); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsReadOnly(); await sender.SendMessagesAsync(batch); _listener.SingleEventById(ServiceBusEventSource.CreateSendLinkStartEvent, e => e.Payload.Contains(sender.Identifier)); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs index c0a86ff32d024..a91b1f0ae9d70 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs @@ -129,7 +129,7 @@ public async Task SendBatchOfMessagesLogsEvents() .Returns(3); mockTransportBatch - .Setup(transport => transport.AsList()) + .Setup(transport => transport.AsReadOnly()) .Returns(new List()); mockTransportSender.Setup( diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs index f29d73671d1d5..208204fa2bf66 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Message/MessageBatchTests.cs @@ -31,7 +31,7 @@ public void EventDataBatchRespectsTheTryAddCallback() Assert.That(() => batch.TryAddMessage(new ServiceBusMessage(new BinaryData("Too many"))), Is.False, "The batch is full; a second attempt to add a new event should not succeed."); Assert.That(store.Count, Is.EqualTo(eventLimit), "The batch should be at its limit after the failed TryAdd attempts."); - Assert.That(batch.AsList(), Is.EquivalentTo(store), "The batch enumerable should reflect the events in the backing store."); + Assert.That(batch.AsReadOnly(), Is.EquivalentTo(store), "The batch enumerable should reflect the events in the backing store."); } /// diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs index 5c605b4063906..56bf46a03adaa 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/ReceiverLiveTests.cs @@ -25,7 +25,7 @@ public async Task PeekUsingConnectionStringWithSharedKey() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsList(); + IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -85,7 +85,7 @@ public async Task PeekUsingConnectionStringWithSas() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsList(); + IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -141,7 +141,7 @@ public async Task ReceiveMessagesWhenQueueEmpty() var messageCount = 2; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); var receiver = client.CreateReceiver(scope.QueueName); @@ -173,7 +173,7 @@ public async Task CancellingDoesNotLoseMessages(bool prefetch) var messageCount = 10; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); var receiver = client.CreateReceiver( scope.QueueName, @@ -244,7 +244,7 @@ public async Task ReceiveMessagesInPeekLockMode() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -281,7 +281,7 @@ public async Task CompleteMessages() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -317,7 +317,7 @@ public async Task ServerBusyRespected() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -391,7 +391,7 @@ public async Task AbandonMessages() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -500,7 +500,7 @@ public async Task DeferMessagesList() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -555,7 +555,7 @@ public async Task DeferMessagesArray() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -604,7 +604,7 @@ public async Task DeferMessagesEnumerable() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -688,7 +688,7 @@ public async Task ReceiveMessagesInReceiveAndDeleteMode() ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount).AsReadOnly(); await sender.SendMessagesAsync(batch); diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs index eb3cb7fe4556a..030adb8c841a8 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Receiver/SessionReceiverLiveTests.cs @@ -30,7 +30,7 @@ public async Task PeekSession(long? sequenceNumber) // send the messages using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); IEnumerable sentMessages = ServiceBusTestUtilities.AddMessages(batch, messageCt, sessionId, sessionId) - .AsList(); + .AsReadOnly(); await sender.SendMessagesAsync(batch); Dictionary sentMessageIdToMsg = new Dictionary(); @@ -218,7 +218,7 @@ public async Task ReceiveMessagesWhenQueueEmpty() var sessionId = "sessionId1"; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); ServiceBusReceiver receiver = await client.AcceptNextSessionAsync( @@ -259,7 +259,7 @@ public async Task ReceiveMessagesInPeekLockMode() var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -301,7 +301,7 @@ public async Task ReceiveMessagesInReceiveAndDeleteMode() var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -348,7 +348,7 @@ public async Task CompleteMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -389,7 +389,7 @@ public async Task AbandonMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -446,7 +446,7 @@ public async Task DeadLetterMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -511,7 +511,7 @@ public async Task DeadLetterMessagesSubscription(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); var topicName = scope.TopicName; @@ -587,7 +587,7 @@ public async Task DeferMessages(bool useSpecificSession) var messageCount = 10; var sessionId = "sessionId1"; using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, sessionId).AsReadOnly(); await sender.SendMessagesAsync(batch); @@ -955,7 +955,7 @@ public async Task CancellingDoesNotLoseSessionMessages(bool prefetch) var messageCount = 10; ServiceBusSender sender = client.CreateSender(scope.QueueName); using ServiceBusMessageBatch batch = await sender.CreateMessageBatchAsync(); - IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsList(); + IEnumerable messages = ServiceBusTestUtilities.AddMessages(batch, messageCount, "sessionId").AsReadOnly(); await sender.SendMessagesAsync(batch); var receiver = await client.AcceptSessionAsync( scope.QueueName, diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs index 03b3a24fc6acd..73aa1a3637e20 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/SenderTests.cs @@ -161,7 +161,7 @@ public async Task SendBatchManagesLockingTheBatch() .Returns(1); mockTransportBatch - .Setup(transport => transport.AsList()) + .Setup(transport => transport.AsReadOnly()) .Returns(new List()); mockTransportSender diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs index f3a0d4004e693..a1da726b552d2 100755 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Sender/ServiceBusMessageBatchTests.cs @@ -83,7 +83,7 @@ public void TryAddIsDelegatedToTheTransportClient() } /// - /// Verifies property accessors for the + /// Verifies property accessors for the /// method. /// /// @@ -95,7 +95,7 @@ public void AsReadOnlyIsDelegatedToTheTransportClient() var batch = new ServiceBusMessageBatch(mockBatch, mockScope); - batch.AsList(); + batch.AsReadOnly(); Assert.That(mockBatch.AsReadOnlyCalledWith, Is.EqualTo(typeof(string)), "The enumerable should delegated the requested type parameter."); } @@ -226,7 +226,7 @@ public override bool TryAddMessage(ServiceBusMessage message) return true; } - public override List AsList() + public override IReadOnlyCollection AsReadOnly() { AsReadOnlyCalledWith = typeof(T); return default; From 2727715d581c4a8c9c73075a3e2d8bfdbb8a61eb Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Mon, 14 Feb 2022 18:19:34 +0100 Subject: [PATCH 10/10] Fix the event source tests --- .../tests/Diagnostics/EventSourceTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs index a91b1f0ae9d70..8842b58ee9857 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/tests/Diagnostics/EventSourceTests.cs @@ -88,7 +88,7 @@ public void SendMessageExceptionLogsEvents() mockTransportSender.Setup( sender => sender.SendAsync( - It.IsAny>(), + It.IsAny>(), It.IsAny())) .Throws(new Exception()); @@ -177,7 +177,7 @@ public async Task ScheduleMessageLogsEvents() }; mockTransportSender.Setup( sender => sender.ScheduleMessagesAsync( - It.IsAny>(), + It.IsAny>(), It.IsAny())) .Returns(Task.FromResult((IReadOnlyList) new List { 1 })); @@ -214,7 +214,7 @@ public void ScheduleMessageExceptionLogsEvents() mockTransportSender.Setup( sender => sender.ScheduleMessagesAsync( - It.IsAny>(), + It.IsAny>(), It.IsAny())) .Throws(new Exception());