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

Fix SendPacketsAsync failures on macos #62726

Merged
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 @@ -13,21 +13,19 @@

namespace System.Net.Sockets.Tests
{
public class SendPacketsAsync
public class SendPacketsAsync : IDisposable
{
private readonly ITestOutputHelper _log;

private IPAddress _serverAddress = IPAddress.IPv6Loopback;
// Accessible directories for UWP app:
// C:\Users\<UserName>\AppData\Local\Packages\<ApplicationPackageName>\
private string TestFileName = Environment.GetEnvironmentVariable("LocalAppData") + @"\NCLTest.Socket.SendPacketsAsync.testpayload";
private static int s_testFileSize = 1024;

#region Additional test attributes
private TempFile _tempFile;
private static int s_testFileSize = 1024;
private string TestFileName => _tempFile.Path;

public SendPacketsAsync(ITestOutputHelper output)
{
_log = TestLogging.GetInstance();
_log = output;

byte[] buffer = new byte[s_testFileSize];

Expand All @@ -36,22 +34,15 @@ public SendPacketsAsync(ITestOutputHelper output)
buffer[i] = (byte)(i % 255);
}

try
{
_log.WriteLine("Creating file {0} with size: {1}", TestFileName, s_testFileSize);
using (FileStream fs = new FileStream(TestFileName, FileMode.CreateNew))
{
fs.Write(buffer, 0, buffer.Length);
}
}
catch (IOException)
{
// Test payload file already exists.
_log.WriteLine("Payload file exists: {0}", TestFileName);
}
_tempFile = TempFile.Create(buffer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One good aspect of the current approach is that Helix can cleanup on test or other failures. Since the run directory is unique this is very easy. I don't know if Helix actually does: cc @MattGal
Using TempFile.Create has potential of filling up temp space.
File name starting with '' seems weird but I don't know if that is relevant.

Copy link
Member Author

@antonfirsov antonfirsov Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there is a hang or a crash, TempFile.Dispose() should delete the file after every test run:

public void Dispose()
{
_tempFile.Dispose();
}

If we want to be more robust, it should be a feature request towards Helix, so they cleanup Path.GetTempPath() after the test process finished. Plenty of BCL tests use the TempFile util especially System.IO.* stuff, I see no reason for keeping a custom approach in SendPacketsAsync.

_log.WriteLine($"Created file {_tempFile.Path} with size: {s_testFileSize}");
}

public void Dispose()
{
_tempFile.Dispose();
}

#endregion Additional test attributes

#region Basic Arguments

Expand Down