コード例 #1
0
        public void TestInitialization()
        {
            // Default values.
            var awaitable = new SocketAwaitable();
            Assert.IsNull(awaitable.AcceptSocket);

            var awaiter = awaitable.GetAwaiter();
            Assert.IsTrue(awaiter.IsCompleted);
            Assert.AreEqual(awaiter.GetResult(), SocketError.Success);
        }
コード例 #2
0
        public async Task TestAwaiterStatus()
        {
            using (var listener = new Socket(SocketType.Stream, ProtocolType.Tcp))
            {
                listener.Bind(new IPEndPoint(IPAddress.IPv6Any, 0));
                listener.Listen(1);
                var acceptTask = Task.Run(async () =>
                {
                    using (var awaitable = new SocketAwaitable())
                    {
                        var awaiter = awaitable.GetAwaiter();
                        Assert.IsTrue(awaiter.IsCompleted);
                        Assert.AreEqual(awaiter.GetResult(), SocketError.Success);

                        var a = listener.AcceptAsync(awaitable);
                        Assert.IsFalse(awaiter.IsCompleted);
                        Assert.AreEqual(awaiter.GetResult(), SocketError.AlreadyInProgress);

                        var result = await a;
                        Assert.IsTrue(awaiter.IsCompleted);
                        Assert.AreEqual(awaiter.GetResult(), result);
                    }
                });

                await Task.Delay(500);

                using (var client = new Socket(SocketType.Stream, ProtocolType.Tcp))
                {
                    using (var awaitable = new SocketAwaitable())
                    {
                        awaitable.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Loopback, (listener.LocalEndPoint as IPEndPoint).Port);

                        var awaiter = awaitable.GetAwaiter();
                        Assert.IsTrue(awaiter.IsCompleted);
                        Assert.AreEqual(awaiter.GetResult(), SocketError.Success);

                        var a = client.ConnectAsync(awaitable);
                        Assert.IsFalse(awaiter.IsCompleted);
                        Assert.AreEqual(awaiter.GetResult(), SocketError.AlreadyInProgress);

                        var result = await a;
                        Assert.IsTrue(awaiter.IsCompleted);
                        Assert.AreEqual(awaiter.GetResult(), result);
                    }
                }

                await acceptTask;
            }
        }
コード例 #3
0
 public void TestGettingAwaiter()
 {
     var awaitable = new SocketAwaitable();
     var awaiter = awaitable.GetAwaiter();
     Assert.IsTrue(awaiter.IsCompleted);
     Assert.AreEqual(awaiter.GetResult(), default(SocketError));
 }