コード例 #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());
                }
            }
        }