Skip to content

Commit 9ae5481

Browse files
committed
test: 增加单元测试
1 parent 3a9ba8e commit 9ae5481

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ public interface IDataPackageAdapter
4343
/// <summary>
4444
/// Attempts to convert the specified binary data into an object representation.
4545
/// </summary>
46-
/// <remarks>This method does not throw exceptions for invalid input. Instead, it returns <see
47-
/// langword="false"/> if the conversion fails, and <paramref name="entity"/> will be <see
48-
/// langword="null"/>.</remarks>
46+
/// <remarks>This method does not throw an exception if the conversion fails. Instead, it returns <see
47+
/// langword="false"/> and sets <paramref name="entity"/> to <see langword="null"/>.</remarks>
4948
/// <param name="data">The binary data to be converted. Must not be empty.</param>
5049
/// <param name="entity">When this method returns <see langword="true"/>, contains the converted object. When this method returns <see
51-
/// langword="false"/>, the value is <see langword="null"/>.</param>
50+
/// langword="false"/>, contains <see langword="null"/>.</param>
5251
/// <returns><see langword="true"/> if the conversion was successful; otherwise, <see langword="false"/>.</returns>
5352
bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)] out object? entity);
5453
}

test/UnitTest/Services/TcpSocketFactoryTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,44 @@ public async Task TryConvertTo_Ok()
482482
Assert.NotNull(entity);
483483
Assert.Equal(entity.Header, [1, 2, 3, 4, 5]);
484484
Assert.Equal(entity.Body, [3, 4]);
485+
486+
// 测试异常流程
487+
var adapter2 = new DataPackageAdapter();
488+
var result = adapter2.TryConvertTo(data, out var t);
489+
Assert.False(result);
490+
Assert.Null(t);
491+
}
492+
493+
[Fact]
494+
public async Task TryConvertTo_Null()
495+
{
496+
var port = 8890;
497+
var server = StartTcpServer(port, MockSplitPackageAsync);
498+
var client = CreateClient();
499+
var tcs = new TaskCompletionSource();
500+
MockEntity? entity = null;
501+
502+
// 设置数据适配器
503+
var adapter = new MockErrorEntityDataPackageAdapter
504+
{
505+
DataPackageHandler = new FixLengthDataPackageHandler(7),
506+
};
507+
client.SetDataPackageAdapter<MockEntity>(adapter, t =>
508+
{
509+
entity = t;
510+
tcs.SetResult();
511+
return Task.CompletedTask;
512+
});
513+
514+
// 连接 TCP Server
515+
var connect = await client.ConnectAsync("localhost", port);
516+
517+
// 发送数据
518+
var data = new ReadOnlyMemory<byte>([1, 2, 3, 4, 5]);
519+
await client.SendAsync(data);
520+
await tcs.Task;
521+
522+
Assert.Null(entity);
485523
}
486524

487525
private static TcpListener StartTcpServer(int port, Func<TcpClient, Task> handler)
@@ -701,6 +739,15 @@ public override bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)]
701739
}
702740
}
703741

742+
class MockErrorEntityDataPackageAdapter : DataPackageAdapter
743+
{
744+
public override bool TryConvertTo(ReadOnlyMemory<byte> data, [NotNullWhen(true)] out object? entity)
745+
{
746+
entity = new Foo();
747+
return true;
748+
}
749+
}
750+
704751
class MockEntity
705752
{
706753
public byte[]? Header { get; set; }

0 commit comments

Comments
 (0)