Skip to content
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

Change Queue to ConcurrentQueue #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions RichardSzalay.MockHttp.Tests/MockHttpMessageHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -232,6 +235,29 @@ public void Should_not_match_expect_out_of_order()
Assert.Equal(HttpStatusCode.NotFound, result.StatusCode);
}

[Fact]
public async Task Should_work_with_multiple_concurrent_writes_to_queue()
{
var mockHandler = new MockHttpMessageHandler();
var tasks = new ConcurrentQueue<Task>();
var readyEvent = new ManualResetEvent(false);

for (int i = 0; i < 100; i++)
{
tasks.Enqueue(Task.Run(async () =>
{
await Task.Yield();
readyEvent.WaitOne();
mockHandler
.Expect("/test")
.Respond("application/json", "{'status' : 'First'}");
}));
}
readyEvent.Set();

await Task.WhenAll(tasks).ConfigureAwait(false);
}

[Fact]
public void VerifyNoOutstandingExpectation_should_fail_if_outstanding_expectation()
{
Expand Down
26 changes: 22 additions & 4 deletions RichardSzalay.MockHttp/MockHttpMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RichardSzalay.MockHttp.Matchers;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand All @@ -13,7 +14,7 @@ namespace RichardSzalay.MockHttp;
/// </summary>
public class MockHttpMessageHandler : HttpMessageHandler
{
private Queue<IMockedRequest> requestExpectations = new();
private ConcurrentQueue<IMockedRequest> requestExpectations = new();
private List<IMockedRequest> backendDefinitions = new();
private Dictionary<IMockedRequest, int> matchCounts = new();
private object lockObject = new();
Expand Down Expand Up @@ -108,15 +109,25 @@ private IMockedRequest FindHandler(HttpRequestMessage request)

if (requestExpectations.Count > 0)
{
var handler = requestExpectations.Peek();
var handlerResult = EvaluateMockedRequest(handler, request);
var peeked = requestExpectations.TryPeek(out var peekedHandler);
if (!peeked)
{
throw new InvalidOperationException("Concurrent read/write on mock queue");
}

var handlerResult = EvaluateMockedRequest(peekedHandler!, request);
results.RequestExpectationResult = handlerResult;

results.UnevaluatedRequestExpectations.AddRange(requestExpectations.Skip(1));

if (handlerResult.Success)
{
requestExpectations.Dequeue();
requestExpectations.TryDequeue(out var handler);

if (peekedHandler != handler)
{
throw new Exception("Queue order changed between evaluation and result");
}

results.Handler = handler;
}
Expand Down Expand Up @@ -366,7 +377,14 @@ public void VerifyNoOutstandingExpectation()
/// </summary>
public void ResetExpectations()
{
#if NET5_0_OR_GREATER
requestExpectations.Clear();
#else
while (!requestExpectations.IsEmpty)
{
requestExpectations.TryDequeue(out IMockedRequest _);
}
#endif
}

/// <summary>
Expand Down