예제 #1
0
        public async Task ConcurrentSendReceiveAsync()
        {
            using (Socket server = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
                using (Socket client = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
                {
                    const int Iters       = 2048;
                    byte[]    sendData    = new byte[Iters];
                    byte[]    receiveData = new byte[sendData.Length];
                    Random.Shared.NextBytes(sendData);

                    string path = GetRandomNonExistingFilePath();

                    server.Bind(new UnixDomainSocketEndPoint(path));
                    server.Listen(1);

                    Task <Socket> acceptTask = server.AcceptAsync();
                    client.Connect(new UnixDomainSocketEndPoint(path));
                    await  acceptTask;
                    Socket accepted = acceptTask.Result;

                    Task[]       writes = new Task[Iters];
                    Task <int>[] reads  = new Task <int> [Iters];
                    for (int i = 0; i < Iters; i++)
                    {
                        writes[i] = client.SendAsync(new ArraySegment <byte>(sendData, i, 1), SocketFlags.None);
                    }
                    for (int i = 0; i < Iters; i++)
                    {
                        reads[i] = accepted.ReceiveAsync(new ArraySegment <byte>(receiveData, i, 1), SocketFlags.None);
                    }

                    await TestSettings.WhenAllOrAnyFailedWithTimeout(writes.Concat(reads).ToArray());

                    AssertExtensions.SequenceEqual(sendData, receiveData);
                }
        }