Skip to content

Commit

Permalink
Add test. Verified that fails before the changes and passes after
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardo-vp committed Feb 8, 2024
1 parent 76e578d commit a3c111c
Showing 1 changed file with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.DotNet.RemoteExecutor;
using System.Diagnostics.Tracing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -228,38 +231,61 @@ async Task VerifyAccept(TcpListener listener)
}
}

[Fact]
public async Task ReadAsyncTest()
internal sealed class RuntimeEventListener : EventListener
{
async Task StartListenerAsync()
protected override void OnEventSourceCreated(EventSource source)
{
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
Tcp client = await listener.AcceptTcpClientAsync();
using (NetworkStream stream = client.GetStream())
if (source.Name.Equals("Microsoft-Windows-DotNETRuntime"))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0);
EnableEvents(source, EventLevel.Verbose, (EventKeywords)0x10000);
}
listener.Stop();
}
}

async Task StartClientAsync()
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void ReadAsyncTest()
{
RemoteExecutor.Invoke(async () =>
{
TcpClient client = new TcpClient(IPAddress.Loopback, 0);
using (NetworkStream stream = client.GetStream())
using (RuntimeEventListener eventListener = new RuntimeEventListener())
{
byte[] data = Encoding.UTF8.GetBytes(new string('*', 1 << 22));
await Task.Delay(10);
await stream.WriteAsync(data, 0, data.Length);
Task.Run(() => Console.WriteLine(Environment.StackTrace)).Wait();
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);
}
client.Close();
}

Task.Run(() => Console.WriteLine(Environment.StackTrace)).Wait();
await StartListenerAsync();
await StartClientAsync();
}).Dispose();
}

[Fact]
Expand Down

0 comments on commit a3c111c

Please sign in to comment.