-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue when sending messages after the Connected event. (#1)
* Prepare stream before triggering the Connected event.
- Loading branch information
1 parent
7777648
commit 892a4fe
Showing
4 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Nager.TcpClient.UnitTest/SendAfterConnectedEventTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters