Skip to content
Merged
Changes from 6 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,7 +6,10 @@
using System.Diagnostics.Tracing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tests;
using Microsoft.DotNet.RemoteExecutor;
Expand Down Expand Up @@ -1160,6 +1163,88 @@ 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>();
async Task StartListenerAsync()
{
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
int port = ((IPEndPoint)listener.LocalEndpoint).Port;
portTcs.SetResult(port);
TcpClient client = await listener.AcceptTcpClientAsync();
using (NetworkStream stream = client.GetStream())
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0);
}
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())
{
byte[] data = Encoding.UTF8.GetBytes(new string('*', 1 << 26));
await stream.WriteAsync(data, 0, data.Length);
}
}
}
Task listenerTask = StartListenerAsync();
Task clientTask = StartClientAsync();
await Task.WhenAll(listenerTask, clientTask);
ManualResetEvent[] waitEvents = [eventListener.TPWaitIOEnqueueEvent, eventListener.TPWaitIODequeueEvent];
WaitHandle.WaitAll(waitEvents, TimeSpan.FromSeconds(15));
Assert.True(eventListener.TPIOEnqueue > 0);
Assert.True(eventListener.TPIODequeue > 0);
}
}).Dispose();
}

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

Expand All @@ -1169,6 +1254,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;
}
}