Пример #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);
                }
            }
        }
Пример #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);
                }
            }
        }
Пример #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);
                }
            }
        }
Пример #4
0
 public void FahClientConnection_TcpConnectionReturnsNullWhenConnectionIsNotOpen()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act & Assert
         Assert.IsNull(connection.TcpConnection);
     }
 }
Пример #5
0
 public void FahClientConnection_TcpConnectionReturnsInstanceWhenConnectionIsOpen()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory()))
     {
         connection.Open();
         // Act & Assert
         Assert.IsNotNull(connection.TcpConnection);
     }
 }
Пример #6
0
 public void FahClientConnection_CreateCommandReturnsCommandWhenNotConnected()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act
         var command = connection.CreateCommand();
         // Assert
         Assert.IsNotNull(command);
     }
 }
Пример #7
0
 public void FahClientConnection_CreateReaderReturnsReaderWhenNotConnected()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act
         var reader = connection.CreateReader();
         // Assert
         Assert.IsNotNull(reader);
     }
 }
Пример #8
0
 public void FahClientConnection_CanSetConnectionTimeout()
 {
     // Arrange
     using (var connection = new FahClientConnection("foo", 2000))
     {
         // Act
         connection.ConnectionTimeout = 10000;
         // Assert
         Assert.AreEqual(10000, connection.ConnectionTimeout);
     }
 }
Пример #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);
     }
 }
Пример #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());
     }
 }
Пример #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);
     }
 }
Пример #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);
     }
 }
Пример #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);
     }
 }
Пример #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);
     }
 }
Пример #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());
            }
        }
Пример #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);
     }
 }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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();
         }
     }
 }
Пример #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);
     }
 }
Пример #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);
        }
Пример #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);
            }
        }
Пример #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);
            }
        }
Пример #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);
     }
 }
Пример #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()));
            }
        }
Пример #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()));
            }
        }
Пример #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());
            }
        }
Пример #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);
     }
 }
Пример #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);
                }
            }
        }
Пример #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());
                }
            }
        }