Skip to content

🔧 NumberOfTimesCalled is now thread safe. #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions src/HttpClient.Helpers/FakeMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
}

// Increment the number of times this option had been 'called'.
IncrementCalls(expectedOption);
expectedOption.IncrementNumberOfTimesCalled();

// Pass the request along.
expectedOption.HttpResponseMessage.RequestMessage = request;
Expand Down Expand Up @@ -151,26 +151,6 @@ private HttpMessageOptions GetExpectedOption(HttpMessageOptions option)
HeadersAreEqual(x.Headers, option.Headers)));
}

private static void IncrementCalls(HttpMessageOptions options)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

var type = typeof(HttpMessageOptions);
var propertyInfo = type.GetTypeInfo()
?.GetDeclaredProperty("NumberOfTimesCalled");
//var propertyInfo = type.GetProperty("NumberOfTimesCalled");
if (propertyInfo == null)
{
return;
}

var existingValue = (int)propertyInfo.GetValue(options);
propertyInfo.SetValue(options, ++existingValue);
}

private static bool ContentAreEqual(HttpContent source, HttpContent destination)
{
if (source == null &&
Expand Down
2 changes: 1 addition & 1 deletion src/HttpClient.Helpers/HttpClient.Helpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19351-01" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

</Project>
9 changes: 8 additions & 1 deletion src/HttpClient.Helpers/HttpMessageOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace WorldDomination.Net.Http
Expand All @@ -11,6 +12,7 @@ public class HttpMessageOptions
private const string AnyValue = "*";
private HttpContent _httpContent;
private string _httpContentSerialized;
private int _numberOfTimesCalled = 0;

/// <summary>
/// Optional: If not provided, then assumed to be *any* endpoint. Otherise, the endpoint we are trying to call/hit/test.
Expand Down Expand Up @@ -51,7 +53,12 @@ public HttpContent HttpContent
// Secondly, this occurs during a UNIT TEST, so I consider the expensive reflection costs to be
// acceptable in this situation.
// ReSharper disable once UnusedAutoPropertyAccessor.Local
public int NumberOfTimesCalled { get; private set; }
public int NumberOfTimesCalled { get { return _numberOfTimesCalled; } }

public void IncrementNumberOfTimesCalled()
{
Interlocked.Increment(ref _numberOfTimesCalled);
}

public override string ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit.core" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down