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

Add Read/Write Async test #98432

Merged
merged 8 commits into from
Mar 1, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using System.Threading.Tests;
Expand Down Expand Up @@ -1160,6 +1162,95 @@ public void ThreadPoolMinMaxThreadsEventTest()
}).Dispose();
}

private sealed class RuntimeEventListener : EventListener
{
private const string ClrProviderName = "Microsoft-Windows-DotNETRuntime";
private const EventKeywords ThreadingKeyword = (EventKeywords)0x10000;

public volatile int tpIOEnqueue = 0;
public volatile int tpIODequeue = 0;
public ManualResetEvent tpWaitIOEnqueueEvent = new ManualResetEvent(false);
public ManualResetEvent tpWaitIODequeueEvent = new ManualResetEvent(false);

protected override void OnEventSourceCreated(EventSource eventSource)
{
if (eventSource.Name.Equals(ClrProviderName))
{
EnableEvents(eventSource, EventLevel.Verbose, ThreadingKeyword);
}

base.OnEventSourceCreated(eventSource);
}

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (eventData.EventName.Equals("ThreadPoolIOEnqueue"))
{
Interlocked.Increment(ref tpIOEnqueue);
tpWaitIOEnqueueEvent.Set();
}
else if (eventData.EventName.Equals("ThreadPoolIODequeue"))
{
Interlocked.Increment(ref tpIODequeue);
tpWaitIODequeueEvent.Set();
}
}
}

[ConditionalFact(nameof(IsThreadingAndRemoteExecutorSupported), nameof(UseWindowsThreadPool))]
public void ReadWriteAsyncTest()
{
RemoteExecutor.Invoke(async () =>
{
using (RuntimeEventListener eventListener = new RuntimeEventListener())
{
TaskCompletionSource<int> portTcs = new TaskCompletionSource<int>();
TaskCompletionSource<bool> readAsyncReadyTcs = new TaskCompletionSource<bool>();

async Task StartListenerAsync()
{
using TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
portTcs.SetResult(port);
using TcpClient client = await listener.AcceptTcpClientAsync();
using (NetworkStream stream = client.GetStream())
{
byte[] buffer = new byte[1];
Task readAsyncTask = stream.ReadAsync(buffer, 0, buffer.Length);
readAsyncReadyTcs.SetResult(true);
await readAsyncTask;
}
listener.Stop();
}

async Task StartClientAsync()
{
int port = await portTcs.Task;
using (TcpClient client = new TcpClient(new IPEndPoint(IPAddress.Loopback, 0)))
{
await client.ConnectAsync(IPAddress.Loopback, port);
using (NetworkStream stream = client.GetStream())
{
bool readAsyncReady = await readAsyncReadyTcs.Task;
byte[] data = new byte[1];
await stream.WriteAsync(data, 0, data.Length);
}
}
}

Task listenerTask = StartListenerAsync();
Task clientTask = StartClientAsync();
await Task.WhenAll(listenerTask, clientTask);
ManualResetEvent[] waitEvents = [eventListener.tpWaitIOEnqueueEvent, eventListener.tpWaitIODequeueEvent];

Assert.True(WaitHandle.WaitAll(waitEvents, TimeSpan.FromSeconds(15))); // Assert that there wasn't a timeout
Assert.True(eventListener.tpIOEnqueue > 0);
Assert.True(eventListener.tpIODequeue > 0);
}
}).Dispose();
}

public static bool IsThreadingAndRemoteExecutorSupported =>
PlatformDetection.IsThreadingSupported && RemoteExecutor.IsSupported;

Expand All @@ -1169,6 +1260,7 @@ private static bool GetUseWindowsThreadPool()
return useWindowsThreadPool;
}

private static bool UsePortableThreadPool { get; } = !GetUseWindowsThreadPool();
private static bool UseWindowsThreadPool { get; } = GetUseWindowsThreadPool();
private static bool UsePortableThreadPool { get; } = !UseWindowsThreadPool;
}
}