This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add cross-platform support for keep-alive socket options (closes #25040) #29963
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
577435c
Add cross-platform support for keep-alive socket options
luigiberrettini e2b75cc
Use the OS specific socket option name constant in native code
luigiberrettini 92fcff9
Match the scoped API shape
luigiberrettini c94435d
Use CMake configuration to check if compiling on OS X
luigiberrettini 2a0af8c
Execute keep-alive tests only for netcoreapp
luigiberrettini c3f9ed0
Make keep-alive enabled check less strict
luigiberrettini fcd4625
Align CMake keep-alive config to format used elsewhere
luigiberrettini 85bd2a5
Test one keep-alive setting at a time on all platforms
luigiberrettini 106e4e0
Clean-up
luigiberrettini 7935fc9
Test *SocketOption on all OS/version combinations
luigiberrettini de3eaf0
Test support for keep-alive time setting on all OS/version combinations
luigiberrettini 2ca6d6c
Rename keep-alive test methods
luigiberrettini 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
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
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
97 changes: 97 additions & 0 deletions
97
src/System.Net.Sockets/tests/FunctionalTests/KeepAliveTest.netcoreapp.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,97 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
namespace System.Net.Sockets.Tests | ||
{ | ||
public class KeepAliveTest | ||
{ | ||
private const int RetryCount = 60; | ||
private const int Time = 5; | ||
private const int Interval = 2; | ||
|
||
private static bool IsUnixOrWindowsAtLeast1703 => | ||
!PlatformDetection.IsWindows || PlatformDetection.IsWindows10Version1703OrGreater; | ||
|
||
private static bool IsWindowsBelow1703 => | ||
PlatformDetection.IsWindows && !PlatformDetection.IsWindows10Version1703OrGreater; | ||
|
||
private static bool IsUnixOrWindowsAtLeast1709 => | ||
!PlatformDetection.IsWindows || PlatformDetection.IsWindows10Version1709OrGreater; | ||
|
||
private static bool IsWindowsBelow1709 => | ||
PlatformDetection.IsWindows && !PlatformDetection.IsWindows10Version1709OrGreater; | ||
|
||
[Fact] | ||
public void Socket_KeepAlive_Disabled_By_Default() | ||
{ | ||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
Assert.Equal<int>(0, (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Socket_KeepAlive_Enable() | ||
{ | ||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); | ||
Assert.NotEqual<int>(0, (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive)); | ||
} | ||
} | ||
|
||
[ConditionalFact(typeof(KeepAliveTest), nameof(IsUnixOrWindowsAtLeast1703))] | ||
public void Socket_Set_KeepAlive_RetryCount_Success() | ||
{ | ||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, RetryCount); | ||
Assert.Equal<int>(RetryCount, (int)socket.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount)); | ||
} | ||
} | ||
|
||
[ConditionalFact(typeof(KeepAliveTest), nameof(IsWindowsBelow1703))] | ||
public void Socket_Set_KeepAlive_RetryCount_Failure() | ||
{ | ||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
Assert.Throws<SocketException>(() => socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, RetryCount)); | ||
Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Socket_Set_KeepAlive_Time_Success() | ||
{ | ||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, Time); | ||
Assert.Equal<int>(Time, (int)socket.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime)); | ||
} | ||
} | ||
|
||
[ConditionalFact(typeof(KeepAliveTest), nameof(IsUnixOrWindowsAtLeast1709))] | ||
public void Socket_Set_KeepAlive_Interval_Success() | ||
{ | ||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Interval); | ||
Assert.Equal<int>(Interval, (int)socket.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval)); | ||
} | ||
} | ||
|
||
[ConditionalFact(typeof(KeepAliveTest), nameof(IsWindowsBelow1709))] | ||
public void Socket_Set_KeepAlive_Interval_Failure() | ||
{ | ||
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) | ||
{ | ||
Assert.Throws<SocketException>(() => socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, Interval)); | ||
Assert.Throws<SocketException>(() => socket.GetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval)); | ||
} | ||
} | ||
} | ||
} |
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
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.
These numbers conflict with DropSourceMembership, TypeOfService, and BlockSource, respectively.
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.
Since
SocketOptionName
values are traditionally operating into their own namespaces (aka levels), I proposed that we stick to the tradition of reusing native Windows values for options here, explaining the values16
,3
and17
.As seen in the source for SocketOptionName, there are already many conflicting enumeration values, which is supposed to be fine, since those operate at a different level.
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 these are all the Windows values, then sounds good.