Skip to content

Commit

Permalink
Add fixes for outdated fxcop analyzer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
abhipsaMisra committed Mar 8, 2023
1 parent 2f5558d commit a98e5d5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
12 changes: 12 additions & 0 deletions iothub/device/src/Transport/RetryDelegatingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ internal class RetryDelegatingHandler : DefaultDelegatingHandler

private RetryPolicy _internalRetryPolicy;

#pragma warning disable CA2213
// The semaphores are getting disposed in the Dispose() block below but it seems like
// Microsoft.CodeAnalysis.FxCopAnalyzers isn't able to analyze and interpret our code successfully.
// We've moved to Microsoft.CodeAnalysis.NetAnalyzers in main and it understands the below disposal block successfully.
private readonly SemaphoreSlim _clientOpenSemaphore = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim _cloudToDeviceMessageSubscriptionSemaphore = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim _cloudToDeviceEventSubscriptionSemaphore = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim _directMethodSubscriptionSemaphore = new SemaphoreSlim(1, 1);
private readonly SemaphoreSlim _twinEventsSubscriptionSemaphore = new SemaphoreSlim(1, 1);
#pragma warning restore CA2213

private bool _openCalled;
private bool _methodsEnabled;
private bool _twinEnabled;
Expand All @@ -34,8 +40,14 @@ internal class RetryDelegatingHandler : DefaultDelegatingHandler
private long _isOpened; // store the opened status in an int which can be accessed via Interlocked class. opened = 1, closed = 0.

private Task _transportClosedTask;

#pragma warning disable CA2213
// The cancellation token sources are getting canceled and disposed in the Dispose() block below but it seems like
// Microsoft.CodeAnalysis.FxCopAnalyzers isn't able to analyze and interpret our code successfully.
// We've moved to Microsoft.CodeAnalysis.NetAnalyzers in main and it understands the below disposal block successfully.
private readonly CancellationTokenSource _handleDisconnectCts = new CancellationTokenSource();
private readonly CancellationTokenSource _cancelPendingOperationsCts = new CancellationTokenSource();
#pragma warning restore CA2213

private readonly ConnectionStatusChangesHandler _onConnectionStatusChanged;

Expand Down
32 changes: 16 additions & 16 deletions iothub/device/tests/RetryDelegatingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task RetryDelegatingHandler_OpenAsyncRetries()
// arrange
int callCounter = 0;

PipelineContext contextMock = Substitute.For<PipelineContext>();
IPipelineContext contextMock = Substitute.For<IPipelineContext>();
IDelegatingHandler innerHandlerMock = Substitute.For<IDelegatingHandler>();

innerHandlerMock
Expand Down Expand Up @@ -55,7 +55,7 @@ public async Task RetryDelegatingHandler_SendEventAsyncRetries()
// arrange
int callCounter = 0;

PipelineContext contextMock = Substitute.For<PipelineContext>();
IPipelineContext contextMock = Substitute.For<IPipelineContext>();
IDelegatingHandler innerHandlerMock = Substitute.For<IDelegatingHandler>();
using var message = new Message(new MemoryStream(new byte[] { 1, 2, 3 }));
innerHandlerMock
Expand Down Expand Up @@ -90,7 +90,7 @@ public async Task RetryDelegatingHandler_DoesNotRetryOnNotSupportedException()
// arrange
int callCounter = 0;

PipelineContext contextMock = Substitute.For<PipelineContext>();
IPipelineContext contextMock = Substitute.For<IPipelineContext>();
IDelegatingHandler innerHandlerMock = Substitute.For<IDelegatingHandler>();
var memoryStream = new NotSeekableStream(new byte[] { 1, 2, 3 });
using var message = new Message(memoryStream);
Expand Down Expand Up @@ -124,7 +124,7 @@ public async Task RetryOneMessageHasBeenTouchedTransientExceptionOccuredSuccess(
// arrange
int callCounter = 0;

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
using var message = new Message(new MemoryStream(new byte[] { 1, 2, 3 }));
IEnumerable<Message> messages = new[] { message };
Expand Down Expand Up @@ -158,7 +158,7 @@ public async Task RetryMessageWithSeekableStreamHasBeenReadTransientExceptionOcc
// arrange
int callCounter = 0;

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
using var message = new Message(new MemoryStream(new byte[] { 1, 2, 3 }));
innerHandlerMock
Expand Down Expand Up @@ -191,7 +191,7 @@ public async Task RetryNonTransientErrorThrownThrows()
// arrange
int callCounter = 0;

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
innerHandlerMock
.OpenAsync(Arg.Any<CancellationToken>())
Expand All @@ -217,7 +217,7 @@ public async Task RetryNonTransientErrorThrownThrows()
public async Task DeviceNotFoundExceptionReturnsDeviceDisabledStatus()
{
// arrange
var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
innerHandlerMock.OpenAsync(Arg.Any<CancellationToken>()).Returns(t => throw new DeviceNotFoundException());

Expand Down Expand Up @@ -247,7 +247,7 @@ public async Task RetryTransientErrorThrownAfterNumberOfRetriesThrows()
{
// arrange
using var cts = new CancellationTokenSource(1000);
var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
innerHandlerMock
.OpenAsync(Arg.Any<CancellationToken>())
Expand All @@ -274,7 +274,7 @@ public async Task RetryCancellationTokenCanceledOpen()
cts.Cancel();
innerHandlerMock.OpenAsync(Arg.Any<CancellationToken>()).Returns(TaskHelpers.CompletedTask);

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);

// act and assert
Expand All @@ -288,7 +288,7 @@ public async Task RetryCancellationTokenCanceledSendEvent()
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
innerHandlerMock.SendEventAsync((Message)null, CancellationToken.None).ReturnsForAnyArgs(TaskHelpers.CompletedTask);

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);
using var cts = new CancellationTokenSource();
cts.Cancel();
Expand All @@ -304,7 +304,7 @@ public async Task RetryCancellationTokenCanceledSendEventWithIEnumMessage()
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
innerHandlerMock.SendEventAsync((IEnumerable<Message>)null, CancellationToken.None).ReturnsForAnyArgs(TaskHelpers.CompletedTask);

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);
using var cts = new CancellationTokenSource();
cts.Cancel();
Expand All @@ -325,7 +325,7 @@ public async Task RetryCancellationTokenCanceledReceive()

cts.Cancel();
innerHandlerMock.ReceiveAsync(cts.Token).Returns(new Task<Message>(() => new Message(new byte[0])));
var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);

// act and assert
Expand All @@ -337,7 +337,7 @@ public async Task RetrySetRetryPolicyVerifyInternalsSuccess()
{
// arrange
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);

var retryPolicy = new TestRetryPolicyRetryTwice();
Expand Down Expand Up @@ -373,7 +373,7 @@ public async Task RetryCancellationTokenCanceledComplete()
cts.Cancel();
innerHandlerMock.CompleteAsync(Arg.Any<string>(), cts.Token).Returns(TaskHelpers.CompletedTask);

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);

// act and assert
Expand All @@ -387,7 +387,7 @@ public async Task RetryCancellationTokenCanceledAbandon()
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
innerHandlerMock.AbandonAsync(null, Arg.Any<CancellationToken>()).ReturnsForAnyArgs(TaskHelpers.CompletedTask);

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);
using var cts = new CancellationTokenSource();
cts.Cancel();
Expand All @@ -406,7 +406,7 @@ public async Task RetryCancellationTokenCanceledReject()
var innerHandlerMock = Substitute.For<IDelegatingHandler>();
innerHandlerMock.RejectAsync(null, Arg.Any<CancellationToken>()).ReturnsForAnyArgs(TaskHelpers.CompletedTask);

var contextMock = Substitute.For<PipelineContext>();
var contextMock = Substitute.For<IPipelineContext>();
var sut = new RetryDelegatingHandler(contextMock, innerHandlerMock);
using var cts = new CancellationTokenSource();
cts.Cancel();
Expand Down
3 changes: 3 additions & 0 deletions iothub/service/src/IoTHubExceptionResult.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;

namespace Microsoft.Azure.Devices
Expand All @@ -10,6 +11,8 @@ namespace Microsoft.Azure.Devices
/// </summary>
internal class IoTHubExceptionResult
{
[SuppressMessage("Usage", "CA1507: Use nameof in place of string literal 'Message'",
Justification = "This JsonProperty annotation depends on service-defined contract (name) and is independent of the property name selected by the SDK.")]
[JsonProperty("Message")]
internal string Message { get; set; }
}
Expand Down

0 comments on commit a98e5d5

Please sign in to comment.