feat: begin working on connection management

This commit is contained in:
Stevan Freeborn
2025-05-13 23:51:14 -05:00
parent df9b3cd7fc
commit 35d6ee5503
7 changed files with 376 additions and 38 deletions
@@ -1,14 +1,14 @@
namespace StevesBot.Worker.Tests.Unit;
public class DiscordGatewayClientOptionsTests
public class DiscordClientOptionsTests
{
[Fact]
public void Constructor_WhenCalledWithoutParameters_ItShouldCreateInstance()
{
var options = new DiscordGatewayClientOptions();
var options = new DiscordClientOptions();
options.Should().NotBeNull();
options.Should().BeOfType<DiscordGatewayClientOptions>();
options.Should().BeOfType<DiscordClientOptions>();
options.ApiUrl.Should().Be(string.Empty);
options.AppToken.Should().Be(string.Empty);
options.Intents.Should().Be(0);
@@ -21,7 +21,7 @@ public class DiscordGatewayClientOptionsTests
var appToken = "test-token";
var intents = 123;
var options = new DiscordGatewayClientOptions
var options = new DiscordClientOptions
{
ApiUrl = apiUrl,
AppToken = appToken,
@@ -29,7 +29,7 @@ public class DiscordGatewayClientOptionsTests
};
options.Should().NotBeNull();
options.Should().BeOfType<DiscordGatewayClientOptions>();
options.Should().BeOfType<DiscordClientOptions>();
options.ApiUrl.Should().Be(apiUrl);
options.AppToken.Should().Be(appToken);
options.Intents.Should().Be(intents);
@@ -0,0 +1,98 @@
namespace StevesBot.Worker.Tests.Unit;
public sealed class DiscordGatewayClientTests : IDisposable
{
private readonly Mock<IDiscordRestClient> _mockDiscordRestClient = new();
private readonly Mock<IWebSocketFactory> _mockWebSocketFactory = new();
private readonly Mock<ILogger<DiscordGatewayClient>> _mockLogger = new();
private readonly DiscordClientOptions _options = new();
private readonly DiscordGatewayClient _discordGatewayClient;
public DiscordGatewayClientTests()
{
_discordGatewayClient = new DiscordGatewayClient(
_options,
_mockWebSocketFactory.Object,
_mockLogger.Object,
_mockDiscordRestClient.Object
);
}
[Fact]
public async Task ConnectAsync_WhenCalled_ItShouldConnectToGateway()
{
_mockDiscordRestClient
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync("wss://gateway.discord.gg");
var mockWebSocket = new Mock<IWebSocket>();
_mockWebSocketFactory
.Setup(static x => x.Create())
.Returns(mockWebSocket.Object);
await _discordGatewayClient.ConnectAsync(CancellationToken.None);
mockWebSocket
.Verify(
static x => x.ConnectAsync(It.IsAny<Uri>(), It.IsAny<CancellationToken>()),
Times.Once
);
}
[Fact]
public async Task ConnectAsync_WhenCalledAndWebSocketIsClosed_ItShouldStopReceivingMessages()
{
_mockDiscordRestClient
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync("wss://gateway.discord.gg");
var mockWebSocket = new Mock<IWebSocket>();
mockWebSocket
.Setup(static x => x.State)
.Returns(WebSocketState.Closed);
_mockWebSocketFactory
.Setup(static x => x.Create())
.Returns(mockWebSocket.Object);
await _discordGatewayClient.ConnectAsync(CancellationToken.None);
mockWebSocket.Verify(
static x => x.ReceiveAsync(It.IsAny<ArraySegment<byte>>(), It.IsAny<CancellationToken>()),
Times.Never
);
}
[Fact]
public async Task ConnectAsync_WhenCalledAndCancellationIsRequested_ItShouldStopReceivingMessages()
{
_mockDiscordRestClient
.Setup(static x => x.GetGatewayUrlAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync("wss://gateway.discord.gg");
var mockWebSocket = new Mock<IWebSocket>();
_mockWebSocketFactory
.Setup(static x => x.Create())
.Returns(mockWebSocket.Object);
using var cts = new CancellationTokenSource();
await _discordGatewayClient.ConnectAsync(cts.Token);
await Task.Delay(100);
await cts.CancelAsync();
mockWebSocket.Verify(static x => x.State, Times.AtMostOnce);
mockWebSocket.Verify(
static x => x.ReceiveAsync(It.IsAny<ArraySegment<byte>>(), It.IsAny<CancellationToken>()),
Times.AtMostOnce
);
}
public void Dispose()
{
_discordGatewayClient.Dispose();
}
}
@@ -0,0 +1,34 @@
namespace StevesBot.Worker.Tests.Unit;
public class DiscordRestClientExceptionTests
{
[Fact]
public void Constructor_WhenCalledWithNoParameters_ItShouldCreateInstance()
{
var exception = new DiscordRestClientException();
exception.Should().NotBeNull();
}
[Fact]
public void Constructor_WhenCalledWithMessage_ItShouldCreateInstance()
{
var message = "Test message";
var exception = new DiscordRestClientException(message);
exception.Should().NotBeNull();
exception.Message.Should().Be(message);
}
[Fact]
public void Constructor_WhenCalledWithMessageAndInnerException_ItShouldCreateInstance()
{
var message = "Test message";
var innerException = new Exception("Inner exception");
var exception = new DiscordRestClientException(message, innerException);
exception.Should().NotBeNull();
exception.Message.Should().Be(message);
exception.InnerException.Should().Be(innerException);
}
}
@@ -1,17 +1,9 @@
using System.Net;
using Microsoft.Extensions.Logging;
using Moq;
using RichardSzalay.MockHttp;
namespace StevesBot.Worker.Tests.Unit;
public sealed class DiscordRestClientTests : IDisposable
{
private const string BaseUrl = "https://discord.com/api/v10";
private static string GatewayUrl => $"{BaseUrl}/gateway";
private static string GatewayEndpoint => $"{BaseUrl}/gateway";
private readonly Mock<ILogger<DiscordRestClient>> _loggerMock = new();
private readonly MockHttpMessageHandler _mockHttpMessageHandler;
@@ -29,7 +21,7 @@ public sealed class DiscordRestClientTests : IDisposable
public async Task GetGatewayUrlAsync_WhenRequestFails_ItShouldThrowException()
{
_mockHttpMessageHandler
.When(GatewayUrl)
.When(GatewayEndpoint)
.Respond(HttpStatusCode.InternalServerError);
var act = async () => await _discordRestClient.GetGatewayUrlAsync(CancellationToken.None);
@@ -41,7 +33,7 @@ public sealed class DiscordRestClientTests : IDisposable
public async Task GetGatewayUrlAsync_WhenResponseIsNull_ItShouldThrowException()
{
_mockHttpMessageHandler
.When(GatewayUrl)
.When(GatewayEndpoint)
.Respond(HttpStatusCode.OK, "application/json", "null");
var act = async () => await _discordRestClient.GetGatewayUrlAsync(CancellationToken.None);
@@ -58,7 +50,7 @@ public sealed class DiscordRestClientTests : IDisposable
}}";
_mockHttpMessageHandler
.When(GatewayUrl)
.When(GatewayEndpoint)
.Respond(HttpStatusCode.OK, "application/json", jsonResponse);
var result = await _discordRestClient.GetGatewayUrlAsync(CancellationToken.None);