<commit_before>// 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.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Net.Sockets.Tests
{
    public class ReceiveMessageFromAsync
    {
        public void OnCompleted(object sender, SocketAsyncEventArgs args)
        {
            EventWaitHandle handle = (EventWaitHandle)args.UserToken;
            handle.Set();
        }

        [OuterLoop] // TODO: Issue #11345
        [Fact]
        public void Success_IPv4()
        {
            ManualResetEvent completed = new ManualResetEvent(false);

            if (Socket.OSSupportsIPv4)
            {
                using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
                {
                    int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
                    receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);

                    Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));

                    for (int i = 0; i < TestSettings.UDPRedundancy; i++)
                    {
                        sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
                    }

                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    args.SetBuffer(new byte[1024], 0, 1024);
                    args.Completed += OnCompleted;
                    args.UserToken = completed;

                    bool pending = receiver.ReceiveMessageFromAsync(args);
                    if (!pending)
                    {
                        OnCompleted(null, args);
                    }

                    Assert.True(completed.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");

                    Assert.Equal(1024, args.BytesTransferred);
                    Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
                    Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);

                    sender.Dispose();
                }
            }
        }

        [OuterLoop] // TODO: Issue #11345
        [Fact]
        public void Success_IPv6()
        {
            ManualResetEvent completed = new ManualResetEvent(false);

            if (Socket.OSSupportsIPv6)
            {
                using (Socket receiver = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp))
                {
                    int port = receiver.BindToAnonymousPort(IPAddress.IPv6Loopback);
                    receiver.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);

                    Socket sender = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
                    sender.Bind(new IPEndPoint(IPAddress.IPv6Loopback, 0));

                    for (int i = 0; i < TestSettings.UDPRedundancy; i++)
                    {
                        sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.IPv6Loopback, port));
                    }

                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);
                    args.SetBuffer(new byte[1024], 0, 1024);
                    args.Completed += OnCompleted;
                    args.UserToken = completed;

                    bool pending = receiver.ReceiveMessageFromAsync(args);
                    if (!pending)
                    {
                        OnCompleted(null, args);
                    }

                    Assert.True(completed.WaitOne(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");

                    Assert.Equal(1024, args.BytesTransferred);
                    Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
                    Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);

                    sender.Dispose();
                }
            }
        }

        [OuterLoop] // TODO: Issue #11345
        [Theory]
        [InlineData(false)]
        [InlineData(true)]
        public async Task Task_Success(bool ipv4)
        {
            AddressFamily family = ipv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
            IPAddress loopback = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;

            using (Socket receiver = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
            using (Socket sender = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
            {
                int port = receiver.BindToAnonymousPort(loopback);
                receiver.SetSocketOption(ipv4 ? SocketOptionLevel.IP : SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);

                sender.Bind(new IPEndPoint(loopback, 0));

                for (int i = 0; i < TestSettings.UDPRedundancy; i++)
                {
                    sender.SendTo(new byte[1024], new IPEndPoint(loopback, port));
                }

                SocketReceiveMessageFromResult result = await receiver.ReceiveMessageFromAsync(
                    new ArraySegment<byte>(new byte[1024], 0, 1024), SocketFlags.None,
                    new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0));
                Assert.Equal(1024, result.ReceivedBytes);
                Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint);
                Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, result.PacketInformation.Address);
            }
        }
    }
}
<commit_msg>Revise ReceiveMessageFromAsync test<commit_after>// 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.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Net.Sockets.Tests
{
    public class ReceiveMessageFromAsync
    {
        [OuterLoop] // TODO: Issue #11345
        [Theory]
        [InlineData(false, false)]
        [InlineData(false, true)]
        [InlineData(true, false)]
        [InlineData(true, true)]
        public void ReceiveSentMessages_SocketAsyncEventArgs_Success(bool ipv4, bool changeReceiveBufferEachCall)
        {
            const int DataLength = 1024;
            AddressFamily family = ipv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
            IPAddress loopback = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;

            var completed = new ManualResetEventSlim(false);
            using (var sender = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
            using (var receiver = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
            {
                sender.Bind(new IPEndPoint(loopback, 0));
                receiver.SetSocketOption(ipv4 ? SocketOptionLevel.IP : SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
                int port = receiver.BindToAnonymousPort(loopback);

                var args = new SocketAsyncEventArgs() { RemoteEndPoint = new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0) };
                args.Completed += (s,e) => completed.Set();
                args.SetBuffer(new byte[DataLength], 0, DataLength);

                for (int iters = 0; iters < 5; iters++)
                {
                    for (int i = 0; i < TestSettings.UDPRedundancy; i++)
                    {
                        sender.SendTo(new byte[DataLength], new IPEndPoint(loopback, port));
                    }

                    if (changeReceiveBufferEachCall)
                    {
                        args.SetBuffer(new byte[DataLength], 0, DataLength);
                    }

                    if (!receiver.ReceiveMessageFromAsync(args))
                    {
                        completed.Set();
                    }
                    Assert.True(completed.Wait(TestSettings.PassingTestTimeout), "Timeout while waiting for connection");
                    completed.Reset();

                    Assert.Equal(DataLength, args.BytesTransferred);
                    Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
                    Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);
                }
            }
        }

        [OuterLoop] // TODO: Issue #11345
        [Theory]
        [InlineData(false)]
        [InlineData(true)]
        public async Task ReceiveSentMessages_Tasks_Success(bool ipv4)
        {
            const int DataLength = 1024;
            AddressFamily family = ipv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
            IPAddress loopback = ipv4 ? IPAddress.Loopback : IPAddress.IPv6Loopback;

            using (var receiver = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
            using (var sender = new Socket(family, SocketType.Dgram, ProtocolType.Udp))
            {
                sender.Bind(new IPEndPoint(loopback, 0));
                receiver.SetSocketOption(ipv4 ? SocketOptionLevel.IP : SocketOptionLevel.IPv6, SocketOptionName.PacketInformation, true);
                int port = receiver.BindToAnonymousPort(loopback);

                for (int iters = 0; iters < 5; iters++)
                {
                    for (int i = 0; i < TestSettings.UDPRedundancy; i++)
                    {
                        sender.SendTo(new byte[DataLength], new IPEndPoint(loopback, port));
                    }

                    SocketReceiveMessageFromResult result = await receiver.ReceiveMessageFromAsync(
                        new ArraySegment<byte>(new byte[DataLength], 0, DataLength), SocketFlags.None,
                        new IPEndPoint(ipv4 ? IPAddress.Any : IPAddress.IPv6Any, 0));
                    Assert.Equal(DataLength, result.ReceivedBytes);
                    Assert.Equal(sender.LocalEndPoint, result.RemoteEndPoint);
                    Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, result.PacketInformation.Address);
                }
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.08753065459347292, 'fraction_numerical': 0.013582342954159592, 'mean_word_length': 1.8618083670715249, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4941626', 'n_tokens_mistral': 2878, 'n_tokens_neox': 2637, 'n_words': 561}