Skip to content

Commit 21aaef0

Browse files
committed
test: 增加单元测试
1 parent 0dd7c8e commit 21aaef0

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public abstract class TcpSocketClientBase(SocketClientOptions options) : ITcpSoc
3939
/// <summary>
4040
/// Gets or sets the service provider used to resolve dependencies.
4141
/// </summary>
42-
[NotNull]
4342
public IServiceProvider? ServiceProvider { get; set; }
4443

4544
/// <summary>
@@ -88,13 +87,15 @@ public virtual void SetDataHandler(IDataPackageHandler handler)
8887
public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken token = default)
8988
{
9089
var ret = false;
90+
SocketClientProvider = ServiceProvider?.GetRequiredService<ISocketClientProvider>()
91+
?? throw new InvalidOperationException("SocketClientProvider is not registered in the service provider.");
92+
9193
try
9294
{
9395
// 释放资源
9496
await CloseAsync();
9597

9698
// 创建新的 TcpClient 实例
97-
SocketClientProvider = ServiceProvider.GetRequiredService<ISocketClientProvider>();
9899
SocketClientProvider.LocalEndPoint = Options.LocalEndPoint;
99100

100101
_localEndPoint = Options.LocalEndPoint;
@@ -123,10 +124,10 @@ public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken
123124
}
124125
catch (OperationCanceledException ex)
125126
{
126-
//var message = token.IsCancellationRequested
127-
// ? $"TCP Socket connect operation was canceled from {LocalEndPoint} to {endPoint}"
128-
// : $"TCP Socket connect operation timed out from {LocalEndPoint} to {endPoint}";
129-
//Log(LogLevel.Warning, ex, message);
127+
var message = token.IsCancellationRequested
128+
? $"TCP Socket connect operation was canceled from {LocalEndPoint} to {endPoint}"
129+
: $"TCP Socket connect operation timed out from {LocalEndPoint} to {endPoint}";
130+
Log(LogLevel.Warning, ex, message);
130131
}
131132
catch (Exception ex)
132133
{
@@ -275,8 +276,8 @@ private async ValueTask<int> ReceiveCoreAsync(ISocketClientProvider client, Memo
275276
/// </summary>
276277
protected void Log(LogLevel logLevel, Exception? ex, string? message)
277278
{
278-
Logger ??= ServiceProvider.GetRequiredService<ILogger<TcpSocketClientBase>>();
279-
Logger.Log(logLevel, ex, "{Message}", message);
279+
Logger ??= ServiceProvider?.GetRequiredService<ILogger<TcpSocketClientBase>>();
280+
Logger?.Log(logLevel, ex, "{Message}", message);
280281
}
281282

282283
/// <summary>

test/UnitTest/Services/TcpSocketFactoryTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ public async Task ConnectAsync_Failed()
7777
Assert.False(connect);
7878
}
7979

80+
[Fact]
81+
public async Task ConnectAsync_Error()
82+
{
83+
var client = CreateClient();
84+
85+
// 反射设置 SocketClientProvider 为空
86+
var propertyInfo = client.GetType().GetProperty("ServiceProvider", BindingFlags.Public | BindingFlags.Instance);
87+
Assert.NotNull(propertyInfo);
88+
propertyInfo.SetValue(client, null);
89+
90+
// 测试 ConnectAsync 方法连接失败
91+
var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => await client.ConnectAsync("localhost", 9999));
92+
Assert.NotNull(ex);
93+
94+
// 反射测试 Log 方法
95+
var methodInfo = client.GetType().GetMethod("Log", BindingFlags.NonPublic | BindingFlags.Instance);
96+
Assert.NotNull(methodInfo);
97+
methodInfo.Invoke(client, [LogLevel.Error, null!, "Test error log"]);
98+
}
99+
80100
[Fact]
81101
public async Task Send_Timeout()
82102
{

0 commit comments

Comments
 (0)