-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add support for non-seekable files to RandomAccess #58506
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2d1527
update XML docs
adamsitnik 529d66b
relax the requirements
adamsitnik 9b7874c
Windows part
adamsitnik f8a03bb
tests
adamsitnik 501ebfa
Unix part
adamsitnik ffb5cf2
cover the unhappy paths for vectorized IO
adamsitnik 2b1510f
don't perform artificial partial writes for non-seekable files, as it…
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
396 changes: 396 additions & 0 deletions
396
src/libraries/System.IO.FileSystem/tests/RandomAccess/NonSeekable.cs
Large diffs are not rendered by default.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
src/libraries/System.IO.FileSystem/tests/RandomAccess/NonSeekable_AsyncHandles.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO.Pipes; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Win32.SafeHandles; | ||
using Xunit; | ||
|
||
namespace System.IO.Tests | ||
{ | ||
public class RandomAccess_NonSeekable_AsyncHandles : RandomAccess_NonSeekable | ||
{ | ||
protected override PipeOptions PipeOptions => PipeOptions.Asynchronous; | ||
|
||
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))] // cancellable file IO is supported only on Windows | ||
[InlineData(FileAccess.Read)] | ||
[InlineData(FileAccess.Write)] | ||
public async Task CancellationIsSupported(FileAccess access) | ||
{ | ||
string pipeName = FileSystemTest.GetNamedPipeServerStreamName(); | ||
string pipePath = Path.GetFullPath($@"\\.\pipe\{pipeName}"); | ||
|
||
using (var server = new NamedPipeServerStream(pipeName, PipeDirection.InOut)) | ||
using (SafeFileHandle clientHandle = File.OpenHandle(pipePath, FileMode.Open, access, FileShare.None, FileOptions.Asynchronous)) | ||
{ | ||
await server.WaitForConnectionAsync(); | ||
|
||
Assert.True(clientHandle.IsAsync); | ||
|
||
CancellationTokenSource cts = new(TimeSpan.FromMilliseconds(250)); | ||
CancellationToken token = cts.Token; | ||
byte[] buffer = new byte[1]; | ||
|
||
OperationCanceledException ex = await Assert.ThrowsAsync<OperationCanceledException>( | ||
() => access == FileAccess.Write | ||
? RandomAccess.WriteAsync(clientHandle, buffer, 0, token).AsTask() | ||
: RandomAccess.ReadAsync(clientHandle, buffer, 0, token).AsTask()); | ||
|
||
Assert.Equal(token, ex.CancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephentoub I've removed the call to
GetNumberOfBytesToWrite
which as you know is used only for testing partial writes inDEBUG
.With pipes|sockets, every read operation is blocked until there is some data available. Even if it's a zero byte read to an empty buffer.
I wanted to test the scenario where we write X bytes to the pipe and try to read Y bytes from it, where Y > X and ensure that the read returns X bytes.
With partial writes emulation provided by
GetNumberOfBytesToWrite
, the read was returningX / 2
bytes (because this is how much we have written). I could perform more reads using exact length, but in reality we usually don't know how many bytes are available in the pipe|socket. I could not read until read returns0
because when there is no data available, the read is blocked and does not return 0 ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's trading off testing one thing for another thing. It's not clear to me that's the right trade off, but if you think so, ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please keep in mind that we are still testing partial writes for seekable files. I just don't think that we can test non-seekable files using the same approach, as they just block the read when there is no data available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the write and read are issued concurrently, it should be fine.
That's how reading generally works, issuing repeated reads until all data expected is consumed.
What guarantees it'll return exactly X bytes at the OS level?