Skip to content

Commit

Permalink
Arg validation clean up (#11893)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored May 8, 2020
1 parent 582f54b commit aca8fa2
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 814 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ public AmqpConnectionScope(Uri serviceEndpoint,

private async Task<Controller> CreateControllerAsync(TimeSpan timeout)
{
var timeoutHelper = new TimeoutHelper(timeout, true);
AmqpConnection connection = await ActiveConnection.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false);
var stopWatch = ValueStopwatch.StartNew();
AmqpConnection connection = await ActiveConnection.GetOrCreateAsync(timeout.CalculateRemaining(stopWatch.GetElapsedTime())).ConfigureAwait(false);

var sessionSettings = new AmqpSessionSettings { Properties = new Fields() };
AmqpSession amqpSession = null;
Expand All @@ -184,10 +184,10 @@ private async Task<Controller> CreateControllerAsync(TimeSpan timeout)
try
{
amqpSession = connection.CreateSession(sessionSettings);
await amqpSession.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false);
await amqpSession.OpenAsync(timeout.CalculateRemaining(stopWatch.GetElapsedTime())).ConfigureAwait(false);

controller = new Controller(amqpSession, timeoutHelper.RemainingTime());
await controller.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false);
controller = new Controller(amqpSession, timeout.CalculateRemaining(stopWatch.GetElapsedTime()));
await controller.OpenAsync(timeout.CalculateRemaining(stopWatch.GetElapsedTime())).ConfigureAwait(false);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -459,7 +459,7 @@ protected virtual async Task<RequestResponseAmqpLink> CreateManagementLinkAsync(
// Create and open the link.

var linkSettings = new AmqpLinkSettings();
linkSettings.AddProperty(AmqpProperty.Timeout, (uint)timeout.CalculateRemaining(stopWatch.GetElapsedTime()).TotalMilliseconds);
linkSettings.AddProperty(AmqpClientConstants.TimeoutName, (uint)timeout.CalculateRemaining(stopWatch.GetElapsedTime()).TotalMilliseconds);
linkSettings.AddProperty(AmqpClientConstants.EntityTypeName, AmqpClientConstants.EntityTypeManagement);
entityPath += '/' + AmqpClientConstants.ManagementAddress;

Expand Down Expand Up @@ -725,7 +725,7 @@ protected virtual async Task<SendingAmqpLink> CreateSendingLinkAsync(
linkSettings.AddProperty(AmqpClientConstants.TransferDestinationAddress, entityPath);
}

linkSettings.AddProperty(AmqpProperty.Timeout, (uint)timeout.CalculateRemaining(stopWatch.GetElapsedTime()).TotalMilliseconds);
linkSettings.AddProperty(AmqpClientConstants.TimeoutName, (uint)timeout.CalculateRemaining(stopWatch.GetElapsedTime()).TotalMilliseconds);

var link = new SendingAmqpLink(linkSettings);
linkSettings.LinkName = $"{ Id };{ connection.Identifier }:{ session.Identifier }:{ link.Identifier }";
Expand Down Expand Up @@ -964,9 +964,12 @@ protected virtual async Task<DateTime> RequestAuthorizationUsingCbsAsync(
DateTime cbsTokenExpiresAtUtc = DateTime.MaxValue;
foreach (string resource in audience)
{
cbsTokenExpiresAtUtc = TimeoutHelper.Min(
cbsTokenExpiresAtUtc,
await authLink.SendTokenAsync(TokenProvider, endpoint, resource, resource, requiredClaims, timeout).ConfigureAwait(false));
DateTime expiresAt =
await authLink.SendTokenAsync(TokenProvider, endpoint, resource, resource, requiredClaims, timeout).ConfigureAwait(false);
if (expiresAt < cbsTokenExpiresAtUtc)
{
cbsTokenExpiresAtUtc = expiresAt;
}
}
return cbsTokenExpiresAtUtc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,7 @@ public static AmqpMessage SBMessageToAmqpMessage(SBMessage sbMessage)

public static ServiceBusReceivedMessage AmqpMessageToSBMessage(AmqpMessage amqpMessage, bool isPeeked = false)
{
if (amqpMessage == null)
{
throw Fx.Exception.ArgumentNull(nameof(amqpMessage));
}
Argument.AssertNotNull(amqpMessage, nameof(amqpMessage));

ServiceBusReceivedMessage sbMessage;

Expand Down Expand Up @@ -567,7 +564,7 @@ internal static bool TryGetAmqpObjectFromNetObject(object netObject, MappingType
}
else if (mappingType == MappingType.ApplicationProperty)
{
throw Fx.Exception.AsError(new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(netObject.GetType().FullName)));
throw new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(netObject.GetType().FullName));
}
else if (netObject is byte[] netObjectAsByteArray)
{
Expand Down Expand Up @@ -656,7 +653,7 @@ private static bool TryGetNetObjectFromAmqpObject(object amqpObject, MappingType
}
else if (mappingType == MappingType.ApplicationProperty)
{
throw Fx.Exception.AsError(new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(amqpObject.GetType().FullName)));
throw new SerializationException(Resources.FailedToSerializeUnsupportedType.FormatForUser(amqpObject.GetType().FullName));
}
else if (amqpObject is AmqpMap map)
{
Expand Down
181 changes: 0 additions & 181 deletions sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpProperty.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -564,15 +564,8 @@ internal virtual Task DeadLetterInternalAsync(
string deadLetterReason,
string deadLetterErrorDescription)
{
if (deadLetterReason != null && deadLetterReason.Length > Constants.MaxDeadLetterReasonLength)
{
throw new ArgumentOutOfRangeException(nameof(deadLetterReason), string.Format(Resources.MaxPermittedLengthExceeded, Constants.MaxDeadLetterReasonLength));
}

if (deadLetterErrorDescription != null && deadLetterErrorDescription.Length > Constants.MaxDeadLetterReasonLength)
{
throw new ArgumentOutOfRangeException(nameof(deadLetterErrorDescription), string.Format(Resources.MaxPermittedLengthExceeded, Constants.MaxDeadLetterReasonLength));
}
Argument.AssertNotTooLong(deadLetterReason, Constants.MaxDeadLetterReasonLength, nameof(deadLetterReason));
Argument.AssertNotTooLong(deadLetterErrorDescription, Constants.MaxDeadLetterReasonLength, nameof(deadLetterErrorDescription));

Guid lockTokenGuid = new Guid(lockToken);
var lockTokenGuids = new[] { lockTokenGuid };
Expand Down
18 changes: 18 additions & 0 deletions sdk/servicebus/Azure.Messaging.ServiceBus/src/Core/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ public static void AssertNotNegative(TimeSpan argumentValue, string argumentName
}
}

/// <summary>
/// Ensures that an argument's value is a positive value, throwing an
/// <see cref="ArgumentOutOfRangeException" /> if that invariant is not met.
/// </summary>
///
/// <param name="argumentValue">The value of the argument to verify.</param>
/// <param name="argumentName">The name of the argument being considered.</param>
///
/// <exception cref="ArgumentOutOfRangeException"><paramref name="argumentValue"/> is not positive <see cref="TimeSpan"/> value.</exception>
///
public static void AssertPositive(TimeSpan argumentValue, string argumentName)
{
if (argumentValue <= TimeSpan.Zero)
{
throw new ArgumentOutOfRangeException(argumentName, $"Argument {argumentName} must be a positive timespan value. The provided value was {argumentValue}.");
}
}

/// <summary>
/// Ensures that an argument's value is at least as large as a given lower bound, throwing
/// <see cref="ArgumentException" /> if that invariant is not met.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Azure.Core;
using Azure.Messaging.ServiceBus.Primitives;

namespace Azure.Messaging.ServiceBus.Filters
Expand Down Expand Up @@ -47,11 +48,7 @@ public CorrelationFilter()
public CorrelationFilter(string correlationId)
: this()
{
if (string.IsNullOrWhiteSpace(correlationId))
{
throw Fx.Exception.ArgumentNullOrWhiteSpace(nameof(correlationId));
}

Argument.AssertNotNullOrWhiteSpace(correlationId, nameof(correlationId));
CorrelationId = correlationId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using Azure.Core;
using Azure.Messaging.ServiceBus.Primitives;

namespace Azure.Messaging.ServiceBus.Filters
Expand Down Expand Up @@ -45,7 +46,8 @@ public RuleDescription(string name)
/// <param name="filter">The filter expression used to match messages.</param>
public RuleDescription(string name, Filter filter)
{
Filter = filter ?? throw Fx.Exception.ArgumentNull(nameof(filter));
Argument.AssertNotNull(filter, nameof(filter));
Filter = filter;
Name = name;
}

Expand All @@ -58,7 +60,11 @@ public Filter Filter
{
get => _filter;

set => _filter = value ?? throw Fx.Exception.ArgumentNull(nameof(Filter));
set
{
Argument.AssertNotNull(value, nameof(Filter));
_filter = value;
}
}

/// <summary>
Expand Down
Loading

0 comments on commit aca8fa2

Please sign in to comment.