Skip to content
Merged
Prev Previous commit
Next Next commit
issue-58383 Fix comment: add linux side test cases.
  • Loading branch information
lateapexearlyspeed committed Sep 29, 2021
commit b47fd7fd3343d4593c70f44b68129a8af69bfe93
41 changes: 33 additions & 8 deletions src/libraries/System.IO.FileSystem/tests/File/ReadWriteAllBytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using System.IO.Pipes;
Expand Down Expand Up @@ -176,28 +177,52 @@ public void ProcFs_NotEmpty(string path)

[Fact]
[PlatformSpecific(TestPlatforms.Windows)] // DOS device paths (\\.\ and \\?\) are a Windows concept
public async Task ReadAllBytes_NonSeekableFileStream()
public async Task ReadAllBytes_NonSeekableFileStream_InWindows()
{
string pipeName = FileSystemTest.GetNamedPipeServerStreamName();
string pipePath = Path.GetFullPath($@"\\.\pipe\{pipeName}");

var namedPipeWriterStream = new NamedPipeServerStream(pipeName, PipeDirection.Out);
var contentBytes = new byte[] { 1, 2, 3 };
Task writingServerTask = WaitConnectionAndWritePipeStreamAsync(namedPipeWriterStream, contentBytes);

byte[] readBytes = File.ReadAllBytes(pipePath);
using (var cts = new CancellationTokenSource())
{
Task writingServerTask = WaitConnectionAndWritePipeStreamAsync(namedPipeWriterStream, contentBytes, cts.Token);
Task<byte[]> readTask = Task.Run(() => File.ReadAllBytes(pipePath), cts.Token);
cts.CancelAfter(TimeSpan.FromSeconds(10));

await writingServerTask;
Assert.Equal<byte>(contentBytes, readBytes);
await writingServerTask;
byte[] readBytes = await readTask;
Assert.Equal<byte>(contentBytes, readBytes);
}

static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream namedPipeWriterStream, byte[] contentBytes)
static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream namedPipeWriterStream, byte[] contentBytes, CancellationToken cancellationToken)
{
await using (namedPipeWriterStream)
{
await namedPipeWriterStream.WaitForConnectionAsync();
await namedPipeWriterStream.WriteAsync(contentBytes);
await namedPipeWriterStream.WaitForConnectionAsync(cancellationToken);
await namedPipeWriterStream.WriteAsync(contentBytes, cancellationToken);
}
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Linux)]
public async Task ReadAllBytes_NonSeekableFileStream_InLinux()
{
var path = "/dev/tty";
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,45 @@ public async Task ReadAllBytesAsync_NonSeekableFileStream()

var namedPipeWriterStream = new NamedPipeServerStream(pipeName, PipeDirection.Out);
var contentBytes = new byte[] { 1, 2, 3 };
Task writingServerTask = WaitConnectionAndWritePipeStreamAsync(namedPipeWriterStream, contentBytes);

byte[] readBytes = await File.ReadAllBytesAsync(pipePath);
using (var cts = new CancellationTokenSource())
{
Task writingServerTask = WaitConnectionAndWritePipeStreamAsync(namedPipeWriterStream, contentBytes, cts.Token);
Task<byte[]> readTask = File.ReadAllBytesAsync(pipePath, cts.Token);
cts.CancelAfter(TimeSpan.FromSeconds(10));

await writingServerTask;
Assert.Equal<byte>(contentBytes, readBytes);
await writingServerTask;
byte[] readBytes = await readTask;
Assert.Equal<byte>(contentBytes, readBytes);
}

static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream namedPipeWriterStream, byte[] contentBytes)
static async Task WaitConnectionAndWritePipeStreamAsync(NamedPipeServerStream namedPipeWriterStream, byte[] contentBytes, CancellationToken cancellationToken)
{
await using (namedPipeWriterStream)
{
await namedPipeWriterStream.WaitForConnectionAsync();
await namedPipeWriterStream.WriteAsync(contentBytes);
await namedPipeWriterStream.WaitForConnectionAsync(cancellationToken);
await namedPipeWriterStream.WriteAsync(contentBytes, cancellationToken);
}
}
}

[Fact]
[PlatformSpecific(TestPlatforms.Linux)]
public async Task ReadAllBytesAsync_NonSeekableFileStream_InLinux()
{
var path = "/dev/tty";
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);
}
}
}
}