public virtual void TestConnectFutureFailureTiming() { Int32 port = 12345; StringBuilder buf = new StringBuilder(); IoConnector connector = CreateConnector(); connector.SessionCreated += (s, e) => buf.Append("X"); connector.SessionOpened += (s, e) => buf.Append("Y"); connector.ExceptionCaught += (s, e) => buf.Append("Z"); try { IConnectFuture future = connector.Connect(CreateEndPoint(port)); future.Await(); buf.Append("1"); try { future.Session.Close(true); Assert.Fail(); } catch { // Signifies a successful test execution Assert.IsTrue(true); } Assert.AreEqual("1", buf.ToString()); } finally { connector.Dispose(); } }
public void TestSessionCallbackInvocation() { Int32 callbackInvoked = 0; Int32 sessionCreatedInvoked = 1; Int32 sessionCreatedInvokedBeforeCallback = 2; Boolean[] assertions = { false, false, false }; CountdownEvent countdown = new CountdownEvent(2); IConnectFuture[] callbackFuture = new IConnectFuture[1]; Int32 port = 12345; IoAcceptor acceptor = CreateAcceptor(); IoConnector connector = CreateConnector(); try { acceptor.Bind(CreateEndPoint(port)); connector.SessionCreated += (s, e) => { assertions[sessionCreatedInvoked] = true; assertions[sessionCreatedInvokedBeforeCallback] = !assertions[callbackInvoked]; countdown.Signal(); }; IConnectFuture future = connector.Connect(CreateEndPoint(port), (s, f) => { assertions[callbackInvoked] = true; callbackFuture[0] = f; countdown.Signal(); }); Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(5)), "Timed out waiting for callback and IoHandler.sessionCreated to be invoked"); Assert.IsTrue(assertions[callbackInvoked], "Callback was not invoked"); Assert.IsTrue(assertions[sessionCreatedInvoked], "IoHandler.sessionCreated was not invoked"); Assert.IsFalse(assertions[sessionCreatedInvokedBeforeCallback], "IoHandler.sessionCreated was invoked before session callback"); Assert.AreSame(future, callbackFuture[0], "Callback future should have been same future as returned by connect"); } finally { try { connector.Dispose(); } finally { acceptor.Dispose(); } } }
public void TestSendLargeFile() { Assert.AreEqual(FILE_SIZE, file.Length, "Test file not as big as specified"); CountdownEvent countdown = new CountdownEvent(1); Boolean[] success = { false }; Exception[] exception = { null }; Int32 port = 12345; IoAcceptor acceptor = CreateAcceptor(); IoConnector connector = CreateConnector(); try { acceptor.ExceptionCaught += (s, e) => { exception[0] = e.Exception; e.Session.Close(true); }; Int32 index = 0; acceptor.MessageReceived += (s, e) => { IoBuffer buffer = (IoBuffer)e.Message; while (buffer.HasRemaining) { int x = buffer.GetInt32(); if (x != index) { throw new Exception(String.Format("Integer at {0} was {1} but should have been {0}", index, x)); } index++; } if (index > FILE_SIZE / 4) { throw new Exception("Read too much data"); } if (index == FILE_SIZE / 4) { success[0] = true; e.Session.Close(true); } }; acceptor.Bind(CreateEndPoint(port)); connector.ExceptionCaught += (s, e) => { exception[0] = e.Exception; e.Session.Close(true); }; connector.SessionClosed += (s, e) => countdown.Signal(); IConnectFuture future = connector.Connect(CreateEndPoint(port)); future.Await(); IoSession session = future.Session; session.Write(file); countdown.Wait(); if (exception[0] != null) { throw exception[0]; } Assert.IsTrue(success[0], "Did not complete file transfer successfully"); Assert.AreEqual(1, session.WrittenMessages, "Written messages should be 1 (we wrote one file)"); Assert.AreEqual(FILE_SIZE, session.WrittenBytes, "Written bytes should match file size"); } finally { try { connector.Dispose(); } finally { acceptor.Dispose(); } } }
private void FormChat_FormClosing(object sender, FormClosingEventArgs e) { connector.Dispose(); }