Skip to content
Merged
Prev Previous commit
Next Next commit
improve the tests
  • Loading branch information
adamsitnik committed Oct 13, 2021
commit b0f8adb8d6ad5864a9e89e01a9592ed2b3127bc1
31 changes: 16 additions & 15 deletions src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,25 @@ static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream na
[PlatformSpecific(TestPlatforms.AnyUnix)]
public async Task ReadAllBytes_NonSeekableFileStream_InUnix()
{
var path = "/dev/tty";
if (!File.Exists(path))
{
throw new SkipTestException(path + " is not available in this environment.");
}
string fifoPath = GetTestFilePath();
Assert.Equal(0, mkfifo(fifoPath, 438 /* 666 in octal */ ));

var contentBytes = new byte[] { 1, 2, 3 };

using (var cts = new CancellationTokenSource())
{
Task writingTask = File.WriteAllBytesAsync(path, contentBytes, cts.Token);
Task<byte[]> readTask = Task.Run(() => File.ReadAllBytes(path), cts.Token);
cts.CancelAfter(TimeSpan.FromMilliseconds(500));

await writingTask;
byte[] readBytes = await readTask;
Assert.Equal<byte>(contentBytes, readBytes);
}
await Task.WhenAll(
Task.Run(() =>
{
byte[] readBytes = File.ReadAllBytes(fifoPath);
Assert.Equal<byte>(contentBytes, readBytes);
}),
Task.Run(() =>
{
using var fs = new FileStream(fifoPath, FileMode.Open, FileAccess.Write, FileShare.Read);
foreach (byte content in contentBytes)
{
fs.WriteByte(content);
}
}));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,25 @@ static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream na
[PlatformSpecific(TestPlatforms.AnyUnix)]
public async Task ReadAllBytesAsync_NonSeekableFileStream_InUnix()
{
var path = "/dev/tty";
if (!File.Exists(path))
{
throw new SkipTestException(path + " is not available in this environment.");
}
string fifoPath = GetTestFilePath();
Assert.Equal(0, mkfifo(fifoPath, 438 /* 666 in octal */ ));

var contentBytes = new byte[] { 1, 2, 3 };

using (var cts = new CancellationTokenSource())
{
Task writingTask = File.WriteAllBytesAsync(path, contentBytes, cts.Token);
Task<byte[]> readTask = File.ReadAllBytesAsync(path, cts.Token);
cts.CancelAfter(TimeSpan.FromMilliseconds(500));

await writingTask;
byte[] readBytes = await readTask;
Assert.Equal<byte>(contentBytes, readBytes);
}
await Task.WhenAll(
Task.Run(async () =>
{
byte[] readBytes = await File.ReadAllBytesAsync(fifoPath);
Assert.Equal<byte>(contentBytes, readBytes);
}),
Task.Run(() =>
{
using var fs = new FileStream(fifoPath, FileMode.Open, FileAccess.Write, FileShare.Read);
foreach (byte content in contentBytes)
{
fs.WriteByte(content);
}
}));
}
}
}