Skip to content

Commit

Permalink
Fix issue when sending messages after the Connected event. (#1)
Browse files Browse the repository at this point in the history
* Prepare stream before triggering the Connected event.
  • Loading branch information
welbertwpg authored Mar 26, 2023
1 parent 7777648 commit 892a4fe
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ on:
- 'src/**'
branches: [ main ]

#permissions:
# statuses: write
# checks: write
# contents: write
# pull-requests: write

jobs:
build:

runs-on: ${{ matrix.os }}

permissions:
checks: write

strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
Expand Down
61 changes: 61 additions & 0 deletions src/Nager.TcpClient.UnitTest/SendAfterConnectedEventTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Text;
using System.Threading.Tasks;

namespace Nager.TcpClient.UnitTest
{
[TestClass]
public class SendAfterConnectedEventTest
{
private async void SendMessage(TcpClient tcpClient)
{
var data = Encoding.UTF8.GetBytes("ping\n");
await tcpClient.SendAsync(data);
}

[TestMethod]
public void SendAfterConnect_Successful()
{
var ipAddress = "tcpbin.com";
var port = 4242;

var mockLoggerTcpClient = LoggerHelper.GetLogger<TcpClient>();
using var tcpClient = new TcpClient(logger: mockLoggerTcpClient.Object);
tcpClient.Connected += () => SendMessage(tcpClient);
tcpClient.Connect(ipAddress, port, 1000);

mockLoggerTcpClient.Verify(
x => x.Log(
LogLevel.Debug,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => o.ToString().Contains("SendAsync")),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
}

[TestMethod]
public async Task SendAfterConnectAsync_Successful()
{
var ipAddress = "tcpbin.com";
var port = 4242;

var mockLoggerTcpClient = LoggerHelper.GetLogger<TcpClient>();
using var tcpClient = new TcpClient(logger: mockLoggerTcpClient.Object);
tcpClient.Connected += () => SendMessage(tcpClient);
await tcpClient.ConnectAsync(ipAddress, port);

mockLoggerTcpClient.Verify(
x => x.Log(
LogLevel.Debug,
It.IsAny<EventId>(),
It.Is<It.IsAnyType>((o, t) => o.ToString().Contains("SendAsync")),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
}
}
}
2 changes: 1 addition & 1 deletion src/Nager.TcpClient/Nager.TcpClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<TargetFrameworks>netstandard2.0;netstandard2.1;net6</TargetFrameworks>

<Version>1.1.0</Version>
<Version>1.1.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 8 additions & 5 deletions src/Nager.TcpClient/TcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ public bool Connect(
waitHandle.Close();
waitHandle.Dispose();

this.PrepareStream();

this._logger.LogInformation($"{nameof(Connect)} - Connected");
this.SwitchToConnected();

this.PrepareStream();

return true;
}
catch (Exception exception)
Expand Down Expand Up @@ -338,10 +338,11 @@ public async Task<bool> ConnectAsync(
return false;
}

this.PrepareStream();

this._logger.LogInformation($"{nameof(ConnectAsync)} - Connected");
this.SwitchToConnected();

this.PrepareStream();

return true;
}

Expand Down Expand Up @@ -400,10 +401,11 @@ public async Task<bool> ConnectAsync(
return false;
}

this.PrepareStream();

this._logger.LogInformation($"{nameof(ConnectAsync)} - Connected");
this.SwitchToConnected();

this.PrepareStream();
return true;
}

Expand Down Expand Up @@ -431,6 +433,7 @@ public async Task SendAsync(
{
if (this._stream == null)
{
this._logger.LogError($"{nameof(SendAsync)} - Stream is null");
return;
}

Expand Down

0 comments on commit 892a4fe

Please sign in to comment.