Skip to content

Commit d7b4231

Browse files
authored
feat(SocketLogging): add SocketLogging unit test (#564)
* feat: 增加日志 * test: 增加 Logging 单元测试 * test: 增加单元测试 * test: 更新 ByteConverter 单元测试 * refactor: 更新转换逻辑 * test: 更新 Logging 单元测试 * chore: bump version 9.0.11 * test: 提高代码覆盖率 * chore: 增加依赖 * chore: 增加排除设置
1 parent a439342 commit d7b4231

9 files changed

Lines changed: 78 additions & 11 deletions

File tree

src/extensions/BootstrapBlazor.Socket/BootstrapBlazor.Socket.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Version>9.0.10</Version>
4+
<Version>9.0.11</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>

src/extensions/BootstrapBlazor.Socket/DataConverter/DataConverter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,23 @@ protected virtual bool Parse(ReadOnlyMemory<byte> data, TEntity entity)
7272
{
7373
var value = attr.ConvertTo(data);
7474
var valueType = value?.GetType();
75-
if (valueType != null && p.PropertyType.IsAssignableFrom(valueType))
75+
if (p.PropertyType.IsAssignableFrom(valueType))
7676
{
7777
p.SetValue(entity, value);
7878
}
79+
else
80+
{
81+
SocketLogging.LogInformation($"{nameof(Parse)} failed. Can't convert value from {GetValueType(valueType)} to {p.PropertyType}");
82+
}
7983
}
8084
}
8185
ret = true;
8286
}
8387
return ret;
8488
}
8589

90+
private static string GetValueType(Type? type) => type?.FullName ?? "NULL";
91+
8692
private DataPropertyConverterAttribute? GetPropertyConverterAttribute(PropertyInfo propertyInfo)
8793
{
8894
DataPropertyConverterAttribute? attr = null;

src/extensions/BootstrapBlazor.Socket/Logging/SocketLogging.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@ public static void Init(ILogger logger)
3434
/// </summary>
3535
/// <param name="ex"></param>
3636
/// <param name="message"></param>
37-
public static void LogError(Exception ex, string? message = null)
38-
{
39-
if (_logger == null)
40-
{
41-
return;
42-
}
37+
public static void LogError(Exception ex, string? message = null) => _logger?.LogError(ex, "{message}", message);
4338

44-
_logger.LogError(ex, "{message}", message);
45-
}
39+
/// <summary>
40+
/// 记录警告信息方法
41+
/// </summary>
42+
/// <param name="message"></param>
43+
public static void LogWarning(string message) => _logger?.LogWarning("{message}", message);
44+
45+
/// <summary>
46+
/// 记录信息方法
47+
/// </summary>
48+
/// <param name="message"></param>
49+
public static void LogInformation(string message) => _logger?.LogInformation("{message}", message);
50+
51+
/// <summary>
52+
/// 记录调试信息方法
53+
/// </summary>
54+
/// <param name="message"></param>
55+
public static void LogDebug(string message) => _logger?.LogDebug("{message}", message);
4656
}

src/extensions/BootstrapBlazor.Socket/PropertyConverter/DataByteConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public class DataByteConverter : IDataPropertyConverter
1515
/// <param name="data"></param>
1616
public object? Convert(ReadOnlyMemory<byte> data)
1717
{
18-
return data.Length > 0 ? data.Span[0] : 0x0;
18+
return data.Length > 0 ? data.Span[0] : byte.MinValue;
1919
}
2020
}

test/UnitTestTcpSocket/Assembly.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright (c) BootstrapBlazor & Argo Zhang (argo@live.ca). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
[assembly: ExcludeFromCodeCoverage]

test/UnitTestTcpSocket/SocketLoggingTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ public class SocketLoggingTest
1212
public void Logger_Ok()
1313
{
1414
SocketLogging.LogError(new Exception());
15+
SocketLogging.LogInformation("Information");
16+
SocketLogging.LogWarning("Warning");
17+
SocketLogging.LogDebug("Debug");
1518
}
1619
}

test/UnitTestTcpSocket/TcpSocketFactoryTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5+
using BootstrapBlazor.Socket.Logging;
56
using Microsoft.Extensions.Logging;
67
using System.Buffers;
78
using System.Net;
@@ -43,6 +44,10 @@ public async Task GetOrCreate_Ok()
4344

4445
await client5.DisposeAsync();
4546
await factory.DisposeAsync();
47+
48+
SocketLogging.LogWarning("Warning");
49+
SocketLogging.LogDebug("Debug");
50+
SocketLogging.LogInformation("Information");
4651
}
4752

4853
[Fact]
@@ -670,6 +675,15 @@ public async Task TryConvertTo_Ok()
670675
Assert.Equal([1, 2, 3, 4, 5], entity.Header);
671676
Assert.Equal([3, 4], entity.Body);
672677

678+
// null
679+
Assert.Equal((byte)0x0, entity.Value16);
680+
681+
// null
682+
Assert.Equal((byte)0x0, entity.Value17);
683+
684+
// byte
685+
Assert.Equal(0x1, entity.Value15);
686+
673687
// string
674688
Assert.Equal("1", entity.Value1);
675689

@@ -1415,6 +1429,15 @@ class MockEntity
14151429
public string? Value14 { get; set; }
14161430

14171431
public string? Value13 { get; set; }
1432+
1433+
[DataPropertyConverter(Type = typeof(byte), Offset = 0, Length = 1)]
1434+
public byte Value15 { get; set; }
1435+
1436+
[DataPropertyConverter(Type = typeof(byte), ConverterType = typeof(MockNullConverter), Offset = 0, Length = 1)]
1437+
public byte Value16 { get; set; }
1438+
1439+
[DataPropertyConverter(Type = typeof(byte[]), Offset = 0, Length = 1)]
1440+
public byte Value17 { get; set; }
14181441
}
14191442

14201443
class MockSocketDataConverter : DataConverter<MockEntity>
@@ -1433,6 +1456,14 @@ protected override bool Parse(ReadOnlyMemory<byte> data, MockEntity entity)
14331456
}
14341457
}
14351458

1459+
class MockNullConverter : IDataPropertyConverter
1460+
{
1461+
public object? Convert(ReadOnlyMemory<byte> data)
1462+
{
1463+
return null;
1464+
}
1465+
}
1466+
14361467
class FooConverter(string name) : IDataPropertyConverter
14371468
{
14381469
public object? Convert(ReadOnlyMemory<byte> data)

test/UnitTestTcpSocket/TcpSocketPropertyConverterTest.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,15 @@ public void DoubleConverter_Ok()
6969
var actual = converter.Convert(new byte[] { 0x1F, 0x85, 0xEB, 0x51, 0xB8, 0x1E, 0x09, 0x40 });
7070
Assert.Equal(3.14, actual);
7171
}
72+
73+
[Fact]
74+
public void ByteConverter_Ok()
75+
{
76+
var converter = new DataByteConverter();
77+
var actual = converter.Convert(new byte[] { 0xFF });
78+
Assert.Equal((byte)0xFF, actual);
79+
80+
actual = converter.Convert(Array.Empty<byte>());
81+
Assert.Equal((byte)0x0, actual);
82+
}
7283
}

test/UnitTestTcpSocket/UnitTestTcpSocket.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17+
<ProjectReference Include="..\..\src\extensions\BootstrapBlazor.Socket\BootstrapBlazor.Socket.csproj" />
1718
<ProjectReference Include="..\..\src\extensions\BootstrapBlazor.TcpSocket\BootstrapBlazor.TcpSocket.csproj" />
1819
</ItemGroup>
1920

0 commit comments

Comments
 (0)