public void TcpClientConnection_ConnectThrowsInvalidOperationExceptionWhenConnectionIsAlreadyConnected() { // Arrange using (new LocalTcpListener()) { var connection = new TcpClientConnection(); // Act (Connect) connection.Connect(LocalTcpListener.Host, LocalTcpListener.Port, 5000); // Act (Attempt Another Connection) & Assert Assert.Throws <InvalidOperationException>(() => connection.Connect(LocalTcpListener.Host, LocalTcpListener.Port, 5000)); } }
public void TcpClientConnection_ConnectThrowsArgumentOutOfRangeExceptionWhenPortNumberIsNotValid() { // Arrange var connection = new TcpClientConnection(); // Act & Assert Assert.Throws <ArgumentOutOfRangeException>(() => connection.Connect(LocalTcpListener.Host, -1, 5000)); }
public void TcpClientConnection_ConnectThrowsArgumentNullExceptionWhenHostIsNull() { // Arrange var connection = new TcpClientConnection(); // Act & Assert Assert.Throws <ArgumentNullException>(() => connection.Connect(null, LocalTcpListener.Port, 5000)); }
public void TcpClientConnection_ConnectThrowsObjectDisposedExceptionWhenAttemptingToConnectUsingConnectionThatWasClosed() { // Arrange using (new LocalTcpListener()) { var connection = new TcpClientConnection(); connection.Close(); // Act & Assert Assert.Throws <ObjectDisposedException>(() => connection.Connect(LocalTcpListener.Host, LocalTcpListener.Port, 5000)); } }
public void TcpClientConnection_ConnectAttemptTimesOut() { // Arrange var connection = new TcpClientConnection(); // use a local IP that no physical machine is using var host = "172.20.0.1"; // Act & Assert Assert.Throws <TimeoutException>(() => connection.Connect(host, LocalTcpListener.Port, _shortTimeout)); Assert.IsFalse(connection.Connected); }
public void TcpClientConnection_ConnectAttemptThrows() { // Arrange var connection = new TcpClientConnection(); // use a local IP that no physical machine is using var host = "172.20.0.1"; // Act & Assert try { connection.Connect(host, LocalTcpListener.Port, _longTimeout); Assert.Fail($"Expected: {typeof(SocketException)}"); } catch (SocketException) { } Assert.IsFalse(connection.Connected); }
public void TcpClientConnection_ConnectSuccessfullyAndClose() { // Arrange using (new LocalTcpListener()) { var connection = new TcpClientConnection(); // Act (Connect) connection.Connect(LocalTcpListener.Host, LocalTcpListener.Port, 5000); // Assert Assert.IsTrue(connection.Connected); var stream = connection.GetStream(); Assert.IsNotNull(stream); Assert.IsInstanceOf <NetworkStream>(stream); // Act (Close) connection.Close(); // Assert Assert.IsFalse(connection.Connected); Assert.IsNull(connection.GetStream()); } }