-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Added Span overloads for Socket.SendFile #47230
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,8 @@ public void Disposed_ThrowsException() | |
{ | ||
s.Dispose(); | ||
Assert.Throws<ObjectDisposedException>(() => s.SendFile(null)); | ||
Assert.Throws<ObjectDisposedException>(() => s.SendFile(null, null, null, TransmitFileOptions.UseDefaultWorkerThread)); | ||
Assert.Throws<ObjectDisposedException>(() => s.SendFile(null, ReadOnlySpan<byte>.Empty, ReadOnlySpan<byte>.Empty, TransmitFileOptions.UseDefaultWorkerThread)); | ||
Assert.Throws<ObjectDisposedException>(() => s.BeginSendFile(null, null, null)); | ||
Assert.Throws<ObjectDisposedException>(() => s.BeginSendFile(null, null, null, TransmitFileOptions.UseDefaultWorkerThread, null, null)); | ||
Assert.Throws<ObjectDisposedException>(() => s.EndSendFile(null)); | ||
|
@@ -100,11 +102,44 @@ public void NotConnected_ThrowsException() | |
using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
Assert.Throws<NotSupportedException>(() => s.SendFile(null)); | ||
Assert.Throws<NotSupportedException>(() => s.SendFile(null, null, null, TransmitFileOptions.UseDefaultWorkerThread)); | ||
Assert.Throws<NotSupportedException>(() => s.SendFile(null, ReadOnlySpan<byte>.Empty, ReadOnlySpan<byte>.Empty, TransmitFileOptions.UseDefaultWorkerThread)); | ||
Assert.Throws<NotSupportedException>(() => s.BeginSendFile(null, null, null)); | ||
Assert.Throws<NotSupportedException>(() => s.BeginSendFile(null, null, null, TransmitFileOptions.UseDefaultWorkerThread, null, null)); | ||
} | ||
} | ||
|
||
[ConditionalTheory] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
[PlatformSpecific(TestPlatforms.Windows)] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public async Task UdpConnection_ThrowsException(bool useAsync) | ||
{ | ||
// Create file to send | ||
byte[] preBuffer; | ||
byte[] postBuffer; | ||
Fletcher32 sentChecksum; | ||
string filename = CreateFileToSend(size: 1, sendPreAndPostBuffers: false, out preBuffer, out postBuffer, out sentChecksum); | ||
|
||
using var client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | ||
using var listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); | ||
listener.BindToAnonymousPort(IPAddress.Loopback); | ||
|
||
client.Connect(listener.LocalEndPoint); | ||
|
||
if (useAsync) | ||
{ | ||
await Assert.ThrowsAsync<SocketException>(() => Task.Factory.FromAsync<string>(client.BeginSendFile, client.EndSendFile, filename, null)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it make sense to also check |
||
} | ||
else | ||
{ | ||
Assert.Throws<SocketException>(() => client.SendFile(filename)); | ||
} | ||
|
||
// Clean up the file we created | ||
File.Delete(filename); | ||
} | ||
|
||
[Theory] | ||
[InlineData(false, false, false)] | ||
[InlineData(false, false, true)] | ||
|
@@ -156,6 +191,39 @@ public async Task SendFile_NoFile_Succeeds(bool useAsync, bool usePreBuffer, boo | |
Assert.Equal(0, client.Available); | ||
} | ||
|
||
[Theory] | ||
[InlineData(false, false)] | ||
[InlineData(false, true)] | ||
[InlineData(true, false)] | ||
[InlineData(true, true)] | ||
public void SendFileSpan_NoFile_Succeeds(bool usePreBuffer, bool usePostBuffer) | ||
{ | ||
using var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
using var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
listener.BindToAnonymousPort(IPAddress.Loopback); | ||
listener.Listen(1); | ||
|
||
client.Connect(listener.LocalEndPoint); | ||
using Socket server = listener.Accept(); | ||
|
||
server.SendFile(null); | ||
Assert.Equal(0, client.Available); | ||
|
||
byte[] preBuffer = usePreBuffer ? new byte[1] : null; | ||
byte[] postBuffer = usePostBuffer ? new byte[1] : null; | ||
int bytesExpected = (usePreBuffer ? 1 : 0) + (usePostBuffer ? 1 : 0); | ||
|
||
server.SendFile(null, preBuffer.AsSpan(), postBuffer.AsSpan(), TransmitFileOptions.UseDefaultWorkerThread); | ||
|
||
byte[] receiveBuffer = new byte[1]; | ||
for (int i = 0; i < bytesExpected; i++) | ||
{ | ||
Assert.Equal(1, client.Receive(receiveBuffer)); | ||
} | ||
|
||
Assert.Equal(0, client.Available); | ||
} | ||
|
||
[ActiveIssue("https://github.com/dotnet/runtime/issues/42534", TestPlatforms.Windows)] | ||
[OuterLoop("Creates and sends a file several gigabytes long")] | ||
[Theory] | ||
|
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.
Looks like we don't have test for the
FileNotFoundException
case.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.
Indeed, that's missing.
As the tests will be refactored, should we file an issue for this and where these points get collected? Or won't we forget about this anyway?
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 one of us manages to PR the test refactor within a week, I wouldn't bother with an issue, otherwise it would be wise to start tracking it.
My main fear is that more extensive testing will discover a bunch of corner cases and platform inconsistencies (or even actual bugs) we'll have to deal with. This is what made things hard with my other PR.