Skip to content

Commit e755e92

Browse files
authored
test(DataPackageAdapter): add unit test (#6353)
* test: 更新发送失败单元测试 * test: 更新单元测试 * test: 修复 FixLengthDataPackageHandler 单元测试 * test: 更新粘包通讯单元测试 * test: 更新终止符数据处理器单元测试 * refactor: 移除 SendAsync 方法 * test: 精简单元测试 * test: 补充单元测试提高覆盖率 * refactor: 更改代码提高代码覆盖率
1 parent ad6f782 commit e755e92

File tree

5 files changed

+115
-156
lines changed

5 files changed

+115
-156
lines changed

src/BootstrapBlazor/Services/TcpSocket/DataPackage/DataPackageHandlerBase.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,7 @@ public abstract class DataPackageHandlerBase : IDataPackageHandler
2626
/// <param name="data"></param>
2727
/// <param name="token"></param>
2828
/// <returns></returns>
29-
public virtual ValueTask<ReadOnlyMemory<byte>> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
30-
{
31-
return ValueTask.FromResult(data);
32-
}
33-
34-
/// <summary>
35-
/// <inheritdoc/>
36-
/// </summary>
37-
/// <param name="data"></param>
38-
/// <param name="token"></param>
39-
/// <returns></returns>
40-
public virtual ValueTask ReceiveAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
41-
{
42-
return ValueTask.CompletedTask;
43-
}
29+
public abstract ValueTask ReceiveAsync(ReadOnlyMemory<byte> data, CancellationToken token = default);
4430

4531
/// <summary>
4632
/// Handles the processing of a sticky package by adjusting the provided buffer and length.

src/BootstrapBlazor/Services/TcpSocket/DataPackage/IDataPackageHandler.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@ public interface IDataPackageHandler
1818
/// </summary>
1919
Func<ReadOnlyMemory<byte>, ValueTask>? ReceivedCallBack { get; set; }
2020

21-
/// <summary>
22-
/// Sends the specified data asynchronously to the target destination.
23-
/// </summary>
24-
/// <remarks>The method performs an asynchronous operation to send the provided data. The caller must
25-
/// ensure that the data is valid and non-empty. The returned memory block may contain a response or acknowledgment
26-
/// depending on the implementation of the target destination.</remarks>
27-
/// <param name="data">The data to be sent, represented as a block of memory.</param>
28-
/// <param name="token">An optional <see cref="CancellationToken"/> to observe while waiting for the operation to complete.</param>
29-
/// <returns>A task that represents the asynchronous operation. The task result contains a <see cref="Memory{T}"/> of <see
30-
/// cref="byte"/> representing the response or acknowledgment received from the target destination.</returns>
31-
ValueTask<ReadOnlyMemory<byte>> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default);
32-
3321
/// <summary>
3422
/// Asynchronously receives data and processes it.
3523
/// </summary>

src/BootstrapBlazor/Services/TcpSocket/DefaultSocketClientProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async ValueTask<bool> ConnectAsync(IPEndPoint endPoint, CancellationToken
5050
public async ValueTask<bool> SendAsync(ReadOnlyMemory<byte> data, CancellationToken token = default)
5151
{
5252
var ret = false;
53-
if (_client is { Connected: true })
53+
if (_client != null)
5454
{
5555
var stream = _client.GetStream();
5656
await stream.WriteAsync(data, token);

src/BootstrapBlazor/Services/TcpSocket/TcpSocketClientBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ public virtual async ValueTask<Memory<byte>> ReceiveAsync(CancellationToken toke
184184

185185
private async ValueTask AutoReceiveAsync()
186186
{
187+
// 自动接收方法
187188
_receiveCancellationTokenSource ??= new();
188-
while (_receiveCancellationTokenSource is { IsCancellationRequested: false })
189+
while (true)
189190
{
190191
if (SocketClientProvider is not { IsConnected: true })
191192
{
@@ -197,6 +198,7 @@ private async ValueTask AutoReceiveAsync()
197198
var len = await ReceiveCoreAsync(SocketClientProvider, buffer, _receiveCancellationTokenSource.Token);
198199
if (len == 0)
199200
{
201+
// 远端关闭或者 DisposeAsync 方法被调用时退出
200202
break;
201203
}
202204
}

0 commit comments

Comments
 (0)