Skip to content

Commit 81d2655

Browse files
authored
🔧 NumberOfTimesCalled is now thread safe. (#37)
1 parent fc2403a commit 81d2655

File tree

4 files changed

+12
-25
lines changed

4 files changed

+12
-25
lines changed

src/HttpClient.Helpers/FakeMessageHandler.cs

+1-21
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
7878
}
7979

8080
// Increment the number of times this option had been 'called'.
81-
IncrementCalls(expectedOption);
81+
expectedOption.IncrementNumberOfTimesCalled();
8282

8383
// Pass the request along.
8484
expectedOption.HttpResponseMessage.RequestMessage = request;
@@ -151,26 +151,6 @@ private HttpMessageOptions GetExpectedOption(HttpMessageOptions option)
151151
HeadersAreEqual(x.Headers, option.Headers)));
152152
}
153153

154-
private static void IncrementCalls(HttpMessageOptions options)
155-
{
156-
if (options == null)
157-
{
158-
throw new ArgumentNullException(nameof(options));
159-
}
160-
161-
var type = typeof(HttpMessageOptions);
162-
var propertyInfo = type.GetTypeInfo()
163-
?.GetDeclaredProperty("NumberOfTimesCalled");
164-
//var propertyInfo = type.GetProperty("NumberOfTimesCalled");
165-
if (propertyInfo == null)
166-
{
167-
return;
168-
}
169-
170-
var existingValue = (int)propertyInfo.GetValue(options);
171-
propertyInfo.SetValue(options, ++existingValue);
172-
}
173-
174154
private static bool ContentAreEqual(HttpContent source, HttpContent destination)
175155
{
176156
if (source == null &&

src/HttpClient.Helpers/HttpClient.Helpers.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</PropertyGroup>
3030

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

3535
</Project>

src/HttpClient.Helpers/HttpMessageOptions.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net.Http;
5+
using System.Threading;
56
using System.Threading.Tasks;
67

78
namespace WorldDomination.Net.Http
@@ -11,6 +12,7 @@ public class HttpMessageOptions
1112
private const string AnyValue = "*";
1213
private HttpContent _httpContent;
1314
private string _httpContentSerialized;
15+
private int _numberOfTimesCalled = 0;
1416

1517
/// <summary>
1618
/// Optional: If not provided, then assumed to be *any* endpoint. Otherise, the endpoint we are trying to call/hit/test.
@@ -51,7 +53,12 @@ public HttpContent HttpContent
5153
// Secondly, this occurs during a UNIT TEST, so I consider the expensive reflection costs to be
5254
// acceptable in this situation.
5355
// ReSharper disable once UnusedAutoPropertyAccessor.Local
54-
public int NumberOfTimesCalled { get; private set; }
56+
public int NumberOfTimesCalled { get { return _numberOfTimesCalled; } }
57+
58+
public void IncrementNumberOfTimesCalled()
59+
{
60+
Interlocked.Increment(ref _numberOfTimesCalled);
61+
}
5562

5663
public override string ToString()
5764
{

tests/HttpClient.Helpers.Tests/HttpClient.Helpers.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
1515
<PackageReference Include="Shouldly" Version="3.0.2" />
1616
<PackageReference Include="xunit.core" Version="2.4.1" />
17-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2">
1818
<PrivateAssets>all</PrivateAssets>
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
</PackageReference>

0 commit comments

Comments
 (0)