You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.0 KiB
C#
74 lines
2.0 KiB
C#
using Xunit;
|
|
using Moq;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Haoliang.Core.Services;
|
|
using Haoliang.Models.Device;
|
|
|
|
namespace Haoliang.Tests;
|
|
|
|
public class PingServiceTests
|
|
{
|
|
private readonly PingService _pingService;
|
|
private readonly Mock<ILogger<PingService>> _loggerMock;
|
|
|
|
public PingServiceTests()
|
|
{
|
|
_loggerMock = new Mock<ILogger<PingService>>();
|
|
_pingService = new PingService(_loggerMock.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PingAsync_Success_ReturnsSuccessResult()
|
|
{
|
|
var result = await _pingService.PingAsync(1, "127.0.0.1");
|
|
|
|
result.Should().NotBeNull();
|
|
result.DeviceId.Should().Be(1);
|
|
result.IpAddress.Should().Be("127.0.0.1");
|
|
result.Timestamp.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(5));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PingAsync_InvalidIp_ReturnsFailureResult()
|
|
{
|
|
var result = await _pingService.PingAsync(999, "invalid-ip");
|
|
|
|
result.Should().NotBeNull();
|
|
result.DeviceId.Should().Be(999);
|
|
result.Success.Should().BeFalse();
|
|
result.ErrorMessage.Should().NotBeNullOrEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task IsReachableAsync_ValidIp_ReturnsTrue()
|
|
{
|
|
var result = await _pingService.IsReachableAsync("127.0.0.1");
|
|
|
|
result.Should().BeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task IsReachableAsync_InvalidIp_ReturnsFalse()
|
|
{
|
|
var result = await _pingService.IsReachableAsync("10.255.255.1", TimeSpan.FromSeconds(1));
|
|
|
|
result.Should().BeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PingAllAsync_MultipleDevices_ReturnsAllResults()
|
|
{
|
|
var devices = new List<(int DeviceId, string IpAddress)>
|
|
{
|
|
(1, "127.0.0.1"),
|
|
(2, "127.0.0.1")
|
|
};
|
|
|
|
var results = await _pingService.PingAllAsync(devices);
|
|
|
|
results.Should().HaveCount(2);
|
|
results.Should().Contain(r => r.DeviceId == 1);
|
|
results.Should().Contain(r => r.DeviceId == 2);
|
|
}
|
|
} |