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); }
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; } }
public void TestGettingAwaiter() { var awaitable = new SocketAwaitable(); var awaiter = awaitable.GetAwaiter(); Assert.IsTrue(awaiter.IsCompleted); Assert.AreEqual(awaiter.GetResult(), default(SocketError)); }