Skip to content

Commit

Permalink
fix deadlock test
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Jan 7, 2025
1 parent f128c87 commit a8b6876
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Telerik.JustMock;
using Nito.AsyncEx;
using System.Threading;
using System.Threading.Tasks;

namespace NewRelic.Agent.Core.DataTransport.Client
{
Expand Down Expand Up @@ -156,19 +157,28 @@ public void Send_Does_Not_Deadlock()
Content = { SerializedData = "{\"Test\"}", ContentType = "application/json" }
};

// start a thread that might deadlock - if join times out, we know it deadlocked
var thread = new Thread(() => SendWithAsyncContext(client, request));
thread.Start();
if (!thread.Join(5000)) {
Assert.Fail("Deadlock detected. Check logic in NRHttpClient.Send() and HttpClientWrapper.Send() for correct usage of .ConfigureAwait(false)");
}
Assert.DoesNotThrow(() =>
{
// start a thread that might deadlock - if the thread doesn't throw TaskCanceledException within 5 seconds, then we have a deadlock
var thread = new Thread(() => SendWithAsyncContext(client, request));
thread.Start();
if (!thread.Join(5000))
throw new Exception("Deadlock detected.");
}, "Deadlock detected. Check logic in NRHttpClient.Send() and HttpClientWrapper.Send() for correct usage of .ConfigureAwait(false)");
}

private static void SendWithAsyncContext(NRHttpClient client, HttpRequest request)
{
AsyncContext.Run(() =>
{
try
{
var response = client.Send(request);
}
catch (TaskCanceledException)
{
// expected exception
}
});
}

Expand Down

0 comments on commit a8b6876

Please sign in to comment.