public void HttpConnection_Query_Timeout() { EnhancedSocket sockListen = null; EnhancedSocket sockAccept = null; HttpConnection con; HttpRequest request; HttpResponse response; string content; TimeSpan orgTimeout; IAsyncResult ar; orgTimeout = HttpStack.TimeoutSweepInterval; HttpStack.TimeoutSweepInterval = TimeSpan.FromMilliseconds(250); sockListen = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sockListen.Bind(new IPEndPoint(IPAddress.Any, ServerPort)); sockListen.Listen(10); try { ar = sockListen.BeginAccept(null, null); con = new HttpConnection(HttpOption.None); con.Connect("http://localhost:" + ServerPort.ToString()); sockAccept = sockListen.EndAccept(ar); content = "Test: Timeout"; request = new HttpRequest("GET", "/foo.htm", null); request["Response"] = content; request["Close"] = "no"; try { response = con.Query(request, SysTime.Now + TimeSpan.FromMilliseconds(250)); Thread.Sleep(1000); Assert.Fail(); } catch (TimeoutException) { } Assert.IsTrue(con.IsClosed); con.Close(); } finally { HttpStack.TimeoutSweepInterval = orgTimeout; if (sockListen != null) { sockListen.Close(); } if (sockAccept != null) { sockAccept.Close(); } } }
private void OnAccept(IAsyncResult ar) { try { syncCompletion = ar.CompletedSynchronously; acceptSock = sock.EndAccept(ar); } catch (Exception e) { asyncException = e; } finally { asyncEvent.Set(); } }
public void HttpConnection_Send_Fail() { EnhancedSocket sockListen = null; EnhancedSocket sockAccept = null; HttpConnection con; HttpRequest request; HttpResponse response; TimeSpan orgTimeout; IAsyncResult ar; orgTimeout = HttpStack.TimeoutSweepInterval; HttpStack.TimeoutSweepInterval = TimeSpan.FromMilliseconds(250); sockListen = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sockListen.Bind(new IPEndPoint(IPAddress.Any, ServerPort)); sockListen.Listen(10); try { ar = sockListen.BeginAccept(null, null); con = new HttpConnection(HttpOption.None); con.Connect("http://localhost:" + ServerPort.ToString()); sockAccept = sockListen.EndAccept(ar); request = new HttpRequest("GET", "/foo.htm", null); request.Content = new BlockArray(new byte[100000]); request["Content-Length"] = request.Content.Size.ToString(); ar = con.BeginQuery(request, DateTime.MaxValue, null, null); sockAccept.Close(); sockAccept = null; try { response = con.EndQuery(ar); Assert.Fail(); } catch { } Assert.IsTrue(con.IsClosed); con.Close(); } finally { HttpStack.TimeoutSweepInterval = orgTimeout; if (sockListen != null) { sockListen.Close(); } if (sockAccept != null) { sockAccept.Close(); } } }