- Added InMemoryTransportOptions class for configuration settings including timeouts and latency. - Developed InMemoryTransportServer class to handle connections, frame processing, and event management. - Created ServiceCollectionExtensions for easy registration of InMemory transport services. - Established project structure and dependencies for InMemory transport library. - Implemented comprehensive unit tests for endpoint discovery, connection management, request/response flow, and streaming capabilities. - Ensured proper handling of cancellation, heartbeat, and hello frames within the transport layer.
118 lines
3.4 KiB
C#
118 lines
3.4 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using StellaOps.Router.Common.Enums;
|
|
using StellaOps.Router.Common.Models;
|
|
using StellaOps.Router.Transport.InMemory;
|
|
using Xunit;
|
|
|
|
namespace StellaOps.Router.Transport.InMemory.Tests;
|
|
|
|
public class HelloHeartbeatFlowTests
|
|
{
|
|
private readonly InMemoryConnectionRegistry _registry;
|
|
private readonly InMemoryTransportServer _server;
|
|
private readonly InMemoryTransportClient _client;
|
|
|
|
public HelloHeartbeatFlowTests()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddLogging();
|
|
services.AddInMemoryTransport();
|
|
|
|
var provider = services.BuildServiceProvider();
|
|
_registry = provider.GetRequiredService<InMemoryConnectionRegistry>();
|
|
_server = provider.GetRequiredService<InMemoryTransportServer>();
|
|
_client = provider.GetRequiredService<InMemoryTransportClient>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ConnectAsync_SendsHelloAndRegistersEndpoints()
|
|
{
|
|
// Arrange
|
|
var instance = new InstanceDescriptor
|
|
{
|
|
InstanceId = "inst-1",
|
|
ServiceName = "test-service",
|
|
Version = "1.0.0",
|
|
Region = "eu1"
|
|
};
|
|
|
|
var endpoints = new List<EndpointDescriptor>
|
|
{
|
|
new()
|
|
{
|
|
ServiceName = "test-service",
|
|
Version = "1.0.0",
|
|
Method = "GET",
|
|
Path = "/api/test"
|
|
}
|
|
};
|
|
|
|
// Act
|
|
await _server.StartAsync(CancellationToken.None);
|
|
await _client.ConnectAsync(instance, endpoints, CancellationToken.None);
|
|
|
|
// Assert
|
|
Assert.Equal(1, _registry.Count);
|
|
var connections = _registry.GetAllConnections();
|
|
Assert.Single(connections);
|
|
Assert.Equal("test-service", connections[0].Instance.ServiceName);
|
|
Assert.Equal(TransportType.InMemory, connections[0].TransportType);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SendHeartbeatAsync_SendsHeartbeatFrame()
|
|
{
|
|
// Arrange
|
|
var instance = new InstanceDescriptor
|
|
{
|
|
InstanceId = "inst-1",
|
|
ServiceName = "test-service",
|
|
Version = "1.0.0",
|
|
Region = "eu1"
|
|
};
|
|
|
|
await _server.StartAsync(CancellationToken.None);
|
|
await _client.ConnectAsync(instance, [], CancellationToken.None);
|
|
|
|
var heartbeat = new HeartbeatPayload
|
|
{
|
|
InstanceId = "inst-1",
|
|
Status = InstanceHealthStatus.Healthy
|
|
};
|
|
|
|
// Act
|
|
await _client.SendHeartbeatAsync(heartbeat, CancellationToken.None);
|
|
|
|
// Allow processing
|
|
await Task.Delay(100);
|
|
|
|
// Assert - no exception means heartbeat was sent successfully
|
|
Assert.Equal(1, _registry.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DisconnectAsync_RemovesConnection()
|
|
{
|
|
// Arrange
|
|
var instance = new InstanceDescriptor
|
|
{
|
|
InstanceId = "inst-1",
|
|
ServiceName = "test-service",
|
|
Version = "1.0.0",
|
|
Region = "eu1"
|
|
};
|
|
|
|
await _server.StartAsync(CancellationToken.None);
|
|
await _client.ConnectAsync(instance, [], CancellationToken.None);
|
|
Assert.Equal(1, _registry.Count);
|
|
|
|
// Act
|
|
await _client.DisconnectAsync();
|
|
|
|
// Assert
|
|
Assert.Equal(0, _registry.Count);
|
|
}
|
|
}
|