From 81d26555ea75a986a2696ddda9d24274ad17d764 Mon Sep 17 00:00:00 2001 From: Pure Krome Date: Tue, 28 Jul 2020 11:32:55 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20NumberOfTimesCalled=20is=20now?= =?UTF-8?q?=20thread=20safe.=20(#37)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/HttpClient.Helpers/FakeMessageHandler.cs | 22 +------------------ .../HttpClient.Helpers.csproj | 2 +- src/HttpClient.Helpers/HttpMessageOptions.cs | 9 +++++++- .../HttpClient.Helpers.Tests.csproj | 4 ++-- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/HttpClient.Helpers/FakeMessageHandler.cs b/src/HttpClient.Helpers/FakeMessageHandler.cs index 209f1ab..ae5e1ec 100644 --- a/src/HttpClient.Helpers/FakeMessageHandler.cs +++ b/src/HttpClient.Helpers/FakeMessageHandler.cs @@ -78,7 +78,7 @@ protected override Task 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; @@ -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 && diff --git a/src/HttpClient.Helpers/HttpClient.Helpers.csproj b/src/HttpClient.Helpers/HttpClient.Helpers.csproj index a15541f..98059e5 100644 --- a/src/HttpClient.Helpers/HttpClient.Helpers.csproj +++ b/src/HttpClient.Helpers/HttpClient.Helpers.csproj @@ -29,7 +29,7 @@ - + \ No newline at end of file diff --git a/src/HttpClient.Helpers/HttpMessageOptions.cs b/src/HttpClient.Helpers/HttpMessageOptions.cs index d241f1b..e5aeb51 100644 --- a/src/HttpClient.Helpers/HttpMessageOptions.cs +++ b/src/HttpClient.Helpers/HttpMessageOptions.cs @@ -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 @@ -11,6 +12,7 @@ public class HttpMessageOptions private const string AnyValue = "*"; private HttpContent _httpContent; private string _httpContentSerialized; + private int _numberOfTimesCalled = 0; /// /// 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 // 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() { diff --git a/tests/HttpClient.Helpers.Tests/HttpClient.Helpers.Tests.csproj b/tests/HttpClient.Helpers.Tests/HttpClient.Helpers.Tests.csproj index 3ee0b59..040a02f 100644 --- a/tests/HttpClient.Helpers.Tests/HttpClient.Helpers.Tests.csproj +++ b/tests/HttpClient.Helpers.Tests/HttpClient.Helpers.Tests.csproj @@ -11,10 +11,10 @@ - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive