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()); } }
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); } } }
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); } } }
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); } } }
public async Task FahClientConnection_ReopenAsyncConnectionThatWasPreviouslyOpenedAndClosed() { // Arrange using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory())) { // Act (Open) await connection.OpenAsync(); // Assert Assert.IsTrue(connection.Connected); // Act (Close) connection.Close(); // Assert Assert.IsFalse(connection.Connected); // Act (Open Again) await connection.OpenAsync(); // Assert Assert.IsTrue(connection.Connected); } }
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); } }
public async Task FahClientConnection_OpenAsyncSuccessfullyAndClose() { // Arrange using (var connection = new FahClientConnection("foo", 2000, new MockTcpConnectionFactory())) { // Act (Open) await connection.OpenAsync(); // Assert Assert.IsTrue(connection.Connected); // Act (Close) connection.Close(); // Assert Assert.IsFalse(connection.Connected); } }
public async Task FahClientConnection_SimulateHFMUpdateCommandsAsynchronously() { using (var connection = new FahClientConnection(Host, Port)) { await connection.OpenAsync(); await connection.CreateCommand("log-updates restart").ExecuteAsync(); await connection.CreateCommand("updates add 0 60 $heartbeat").ExecuteAsync(); await connection.CreateCommand("updates add 1 1 $info").ExecuteAsync(); await connection.CreateCommand("updates add 2 1 $(options -a)").ExecuteAsync(); await connection.CreateCommand("updates add 3 1 $slot-info").ExecuteAsync(); var reader = connection.CreateReader(); for (int i = 0; i < 10 && await reader.ReadAsync(); i++) { Console.WriteLine(reader.Message); } } }