Ejemplo n.º 1
0
        public async Task FahClientConnection_CloseConnectionWhileExecutingReaderAsynchronously()
        {
            using (var connection = new FahClientConnection(Host, Port))
            {
                await connection.OpenAsync();

                CloseConnectionAfter(3000, connection);

                var command = connection.CreateCommand();
                command.CommandText = "info";
                await command.ExecuteAsync();

                command.CommandText = "log-updates restart";
                await command.ExecuteAsync();

                var reader = connection.CreateReader();
                try
                {
                    while (await reader.ReadAsync())
                    {
                        Console.WriteLine(reader.Message);
                    }
                }
                catch (Exception ex)
                {
                    Assert.IsFalse(connection.Connected);
                    Console.WriteLine(ex);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task FahClientConnection_WritesCommandsAndReadsMessageAsynchronously()
        {
            using (var connection = new FahClientConnection(Host, Port))
            {
                await connection.OpenAsync();

                var command = connection.CreateCommand();
                command.CommandText = "info";
                await command.ExecuteAsync();

                var reader = connection.CreateReader();
                if (await reader.ReadAsync())
                {
                    Console.WriteLine(reader.Message);
                }

                command.CommandText = "log-updates restart";
                await command.ExecuteAsync();

                if (await reader.ReadAsync())
                {
                    Console.WriteLine(reader.Message);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task FahClientConnection_CloseConnectionWhileExecutingCommandAsynchronously()
        {
            using (var connection = new FahClientConnection(Host, Port))
            {
                await connection.OpenAsync();

                CloseConnectionAfter(1000, connection);

                var command = connection.CreateCommand();
                command.CommandText = "info";

                try
                {
                    // continually execute the command
                    while (await command.ExecuteAsync() > 0)
                    {
                    }
                }
                catch (Exception ex)
                {
                    Assert.IsFalse(connection.Connected);
                    Console.WriteLine(ex);
                }
            }
        }
Ejemplo n.º 4
0
 public void FahClientConnection_TcpConnectionReturnsNullWhenConnectionIsNotOpen()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act & Assert
         Assert.IsNull(connection.TcpConnection);
     }
 }
Ejemplo n.º 5
0
 public void FahClientConnection_TcpConnectionReturnsInstanceWhenConnectionIsOpen()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         connection.Open();
         // Act & Assert
         Assert.IsNotNull(connection.TcpConnection);
     }
 }
Ejemplo n.º 6
0
 public void FahClientConnection_CreateCommandReturnsCommandWhenNotConnected()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act
         var command = connection.CreateCommand();
         // Assert
         Assert.IsNotNull(command);
     }
 }
Ejemplo n.º 7
0
 public void FahClientConnection_CreateReaderReturnsReaderWhenNotConnected()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act
         var reader = connection.CreateReader();
         // Assert
         Assert.IsNotNull(reader);
     }
 }
Ejemplo n.º 8
0
 public void FahClientConnection_CanSetConnectionTimeout()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act
         connection.ConnectionTimeout = 10000;
         // Assert
         Assert.AreEqual(10000, connection.ConnectionTimeout);
     }
 }
Ejemplo n.º 9
0
 public void FahClientConnection_OpenAttemptTimesOut()
 {
     // Arrange
     using (var connection = new FahClientConnection(LocalTcpListener.Host, LocalTcpListener.Port))
     {
         connection.ConnectionTimeout = ShortTimeout;
         // Act & Assert
         Assert.Throws <TimeoutException>(() => connection.Open());
         Assert.IsFalse(connection.Connected);
     }
 }
Ejemplo n.º 10
0
 public void FahClientConnection_OpenThrowsInvalidOperationExceptionWhenConnectionIsAlreadyConnected()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         // Act (Open)
         connection.Open();
         // Act (Attempt Another Connection) & Assert
         Assert.Throws <InvalidOperationException>(() => connection.Open());
     }
 }
Ejemplo n.º 11
0
 public void FahClientConnection_VerifyPropertiesOnNewInstance()
 {
     // Act
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Assert
         Assert.AreEqual("foo", connection.Host);
         Assert.AreEqual(2000, connection.Port);
         Assert.AreEqual(5000, connection.ConnectionTimeout);
         Assert.IsFalse(connection.Connected);
     }
 }
Ejemplo n.º 12
0
 public void FahClientConnection_CreateCommandReturnsCommandWithCommandText()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act
         var command = connection.CreateCommand("bar");
         // Assert
         Assert.IsNotNull(command);
         Assert.AreEqual("bar", command.CommandText);
     }
 }
Ejemplo n.º 13
0
 public void FahClientConnection_OpenAsyncAttemptTimesOut()
 {
     // Arrange
     using (var connection = new FahClientConnection(LocalTcpListener.Host, LocalTcpListener.Port))
     {
         connection.ConnectionTimeout = ShortTimeout;
         // Act & Assert
         // ReSharper disable once AccessToDisposedClosure
         Assert.ThrowsAsync <TimeoutException>(() => connection.OpenAsync());
         Assert.IsFalse(connection.Connected);
     }
 }
Ejemplo n.º 14
0
 public void FahClientConnection_TcpConnectionReturnsNullAfterConnectionIsOpenedAndClosed()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         connection.Open();
         Assert.IsNotNull(connection.TcpConnection);
         connection.Close();
         // Act & Assert
         Assert.IsNull(connection.TcpConnection);
     }
 }
Ejemplo n.º 15
0
        public async Task FahClientConnection_OpenAsyncThrowsInvalidOperationExceptionWhenConnectionIsAlreadyConnected()
        {
            // Arrange
            using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
            {
                // Act (Open)
                await connection.OpenAsync();

                // Act (Attempt Another Connection) & Assert
                // ReSharper disable once AccessToDisposedClosure
                Assert.ThrowsAsync <InvalidOperationException>(() => connection.OpenAsync());
            }
        }
Ejemplo n.º 16
0
 public void FahClientConnection_TcpConnectionReturnsSameInstanceWhileConnectionRemainsOpen()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         connection.Open();
         // Act
         var tcpConnection1 = connection.TcpConnection;
         var tcpConnection2 = connection.TcpConnection;
         // Assert
         Assert.AreSame(tcpConnection1, tcpConnection2);
     }
 }
Ejemplo n.º 17
0
        public void FahClientCommand_ExecuteAsyncRethrowsExceptionFromStreamWriteAsyncAndClosesTheConnection()
        {
            // Arrange
            Func <TcpConnection> factory = () => new MockTcpConnection(() => new MockStreamThrowsOnWrite());

            using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory(factory)))
            {
                connection.Open();
                var command = new FahClientCommand(connection);
                // Act & Assert
                Assert.ThrowsAsync <IOException>(() => command.ExecuteAsync());
                Assert.IsFalse(connection.Connected);
            }
        }
Ejemplo n.º 18
0
        public void FahClientReader_ReadAsyncRethrowsExceptionFromStreamReadAsyncAndClosesTheConnection()
        {
            // Arrange
            Func <TcpConnection> factory = () => new MockTcpConnection(() => new MockStreamThrowsOnRead());

            using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory(factory)))
            {
                connection.Open();
                var reader = new FahClientReader(connection);
                // Act & Assert
                Assert.ThrowsAsync <IOException>(() => reader.ReadAsync());
                Assert.IsFalse(connection.Connected);
            }
        }
Ejemplo n.º 19
0
 public void FahClientConnection_OpenSuccessfullyAndCloseMultipleTimes()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         // Act (Open)
         connection.Open();
         // Act (Close)
         foreach (var _ in Enumerable.Range(0, 3))
         {
             connection.Close();
         }
     }
 }
Ejemplo n.º 20
0
 public void FahClientConnection_OpenSuccessfullyAndClose()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         // Act (Open)
         connection.Open();
         // Assert
         Assert.IsTrue(connection.Connected);
         // Act (Close)
         connection.Close();
         // Assert
         Assert.IsFalse(connection.Connected);
     }
 }
Ejemplo n.º 21
0
        public void FahClientConnection_DisposeClosesInnerTcpConnection()
        {
            // Arrange
            var tcpConnectionFactory = new MockTcpConnectionFactory();

            using (var connection = new FahClientConnection("foo", 2000, tcpConnectionFactory))
            {
                // Act (Open)
                connection.Open();
                // Assert
                Assert.IsTrue(tcpConnectionFactory.TcpConnection.Connected);
            }
            // Assert
            Assert.IsFalse(tcpConnectionFactory.TcpConnection.Connected);
        }
Ejemplo n.º 22
0
        public void FahClientReader_OnReadStream_ReturnsZeroWhenBufferIsNull()
        {
            // Arrange
            Func <TcpConnection> factory = () => new MockTcpConnection();

            using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory(factory)))
            {
                connection.Open();
                var reader = new FahClientReaderReturnsZeroWhenBufferIsNull(connection);
                // Act
                bool result = reader.Read();
                // Assert
                Assert.IsFalse(result);
                Assert.IsNull(reader.Message);
            }
        }
Ejemplo n.º 23
0
        public void FahClientReader_ReadReadsNoMessageFromConnection()
        {
            // Arrange
            Func <TcpConnection> factory = () => new MockTcpConnection();

            using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory(factory)))
            {
                connection.Open();
                var reader = new FahClientReader(connection);
                // Act
                bool result = reader.Read();
                // Assert
                Assert.IsFalse(result);
                Assert.IsNull(reader.Message);
            }
        }
Ejemplo n.º 24
0
 public void FahClientConnection_TcpConnectionReturnsDifferentInstanceEachTimeConnectionIsOpened()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         connection.Open();
         // Act
         var tcpConnection1 = connection.TcpConnection;
         // close and open again
         connection.Close();
         connection.Open();
         // Act
         var tcpConnection2 = connection.TcpConnection;
         // Assert
         Assert.AreNotSame(tcpConnection1, tcpConnection2);
     }
 }
Ejemplo n.º 25
0
        public void FahClientCommand_ExecuteWritesNullCommandTextToConnection()
        {
            // Arrange
            var tcpConnectionFactory = new MockTcpConnectionFactory();

            using (var connection = new FahClientConnection("foo", 2000, tcpConnectionFactory))
            {
                connection.Open();
                var command = new FahClientCommand(connection, null);
                // Act
                int bytesWritten = command.Execute();
                // Assert
                Assert.AreEqual(0, bytesWritten);
                var memoryStream = (MemoryStream)tcpConnectionFactory.TcpConnection.GetStream();
                Assert.AreEqual("", Encoding.ASCII.GetString(memoryStream.ToArray()));
            }
        }
Ejemplo n.º 26
0
        public async Task FahClientCommand_ExecuteAsyncWritesCommandTextToConnection()
        {
            // Arrange
            var tcpConnectionFactory = new MockTcpConnectionFactory();

            using (var connection = new FahClientConnection("foo", 2000, tcpConnectionFactory))
            {
                connection.Open();
                var command = new FahClientCommand(connection, "command text");
                // Act
                int bytesWritten = await command.ExecuteAsync();

                // Assert
                Assert.AreEqual(13, bytesWritten);
                var memoryStream = (MemoryStream)tcpConnectionFactory.TcpConnection.GetStream();
                Assert.AreEqual("command text\n", Encoding.ASCII.GetString(memoryStream.ToArray()));
            }
        }
Ejemplo n.º 27
0
        public void FahClientReader_ReadExtractsExistingMessage()
        {
            // Arrange
            Func <TcpConnection> factory = () => new MockTcpConnection();

            using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory(factory)))
            {
                connection.Open();
                var reader = new FahClientReader(connection, new FahClientMessageExtractorWithMessage(MessageFromStream));
                reader.BufferSize = 8;
                // Act
                bool result = reader.Read();
                // Assert
                Assert.IsTrue(result);
                var message = reader.Message;
                Assert.AreEqual(FahClientMessageType.Info, message.Identifier.MessageType);
                Assert.AreEqual(MessageFromReader, message.MessageText.ToString());
            }
        }
Ejemplo n.º 28
0
 public void FahClientConnection_ReopenConnectionThatWasPreviouslyOpenedAndClosed()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         // Act (Open)
         connection.Open();
         // Assert
         Assert.IsTrue(connection.Connected);
         // Act (Close)
         connection.Close();
         // Assert
         Assert.IsFalse(connection.Connected);
         // Act (Open Again)
         connection.Open();
         // Assert
         Assert.IsTrue(connection.Connected);
     }
 }
Ejemplo n.º 29
0
        public void FahClientConnection_SimulateHFMUpdateCommandsSynchronously()
        {
            using (var connection = new FahClientConnection(Host, Port))
            {
                connection.Open();

                connection.CreateCommand("log-updates restart").Execute();
                connection.CreateCommand("updates add 0 60 $heartbeat").Execute();
                connection.CreateCommand("updates add 1 1 $info").Execute();
                connection.CreateCommand("updates add 2 1 $(options -a)").Execute();
                connection.CreateCommand("updates add 3 1 $slot-info").Execute();

                var reader = connection.CreateReader();
                for (int i = 0; i < 10 && reader.Read(); i++)
                {
                    Console.WriteLine(reader.Message);
                }
            }
        }
Ejemplo n.º 30
0
        public async Task FahClientReader_ReadAsyncReadsMessageFromConnection()
        {
            // Arrange
            using (var stream = CreateStreamWithMessage())
            {
                Func <TcpConnection> factory = () => new MockTcpConnection(() => stream);
                using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory(factory)))
                {
                    connection.Open();
                    var reader = new FahClientReader(connection);
                    reader.BufferSize = 8;
                    // Act
                    bool result = await reader.ReadAsync();

                    // Assert
                    Assert.IsTrue(result);
                    var message = reader.Message;
                    Assert.AreEqual(FahClientMessageType.Info, message.Identifier.MessageType);
                    Assert.AreEqual(MessageFromReader, message.MessageText.ToString());
                }
            }
        }