- 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.
129 lines
3.7 KiB
C#
129 lines
3.7 KiB
C#
using System.Reflection;
|
|
using StellaOps.Microservice;
|
|
using Xunit;
|
|
|
|
namespace StellaOps.Microservice.Tests;
|
|
|
|
// Test endpoint classes
|
|
[StellaEndpoint("GET", "/api/test")]
|
|
public class TestGetEndpoint : IStellaEndpoint<string>
|
|
{
|
|
public Task<string> HandleAsync(CancellationToken cancellationToken) => Task.FromResult("OK");
|
|
}
|
|
|
|
[StellaEndpoint("POST", "/api/create", SupportsStreaming = true, TimeoutSeconds = 60)]
|
|
public class TestPostEndpoint : IStellaEndpoint<TestRequest, TestResponse>
|
|
{
|
|
public Task<TestResponse> HandleAsync(TestRequest request, CancellationToken cancellationToken)
|
|
=> Task.FromResult(new TestResponse());
|
|
}
|
|
|
|
public record TestRequest;
|
|
public record TestResponse;
|
|
|
|
public class EndpointDiscoveryTests
|
|
{
|
|
[Fact]
|
|
public void StellaEndpointAttribute_StoresMethodAndPath()
|
|
{
|
|
// Arrange & Act
|
|
var attr = new StellaEndpointAttribute("POST", "/api/test");
|
|
|
|
// Assert
|
|
Assert.Equal("POST", attr.Method);
|
|
Assert.Equal("/api/test", attr.Path);
|
|
}
|
|
|
|
[Fact]
|
|
public void StellaEndpointAttribute_NormalizesMethod()
|
|
{
|
|
// Arrange & Act
|
|
var attr = new StellaEndpointAttribute("get", "/api/test");
|
|
|
|
// Assert
|
|
Assert.Equal("GET", attr.Method);
|
|
}
|
|
|
|
[Fact]
|
|
public void StellaEndpointAttribute_DefaultTimeoutIs30Seconds()
|
|
{
|
|
// Arrange & Act
|
|
var attr = new StellaEndpointAttribute("GET", "/api/test");
|
|
|
|
// Assert
|
|
Assert.Equal(30, attr.TimeoutSeconds);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReflectionDiscovery_FindsEndpointsInCurrentAssembly()
|
|
{
|
|
// Arrange
|
|
var options = new StellaMicroserviceOptions
|
|
{
|
|
ServiceName = "test-service",
|
|
Version = "1.0.0",
|
|
Region = "eu1"
|
|
};
|
|
var discovery = new ReflectionEndpointDiscoveryProvider(
|
|
options,
|
|
[Assembly.GetExecutingAssembly()]);
|
|
|
|
// Act
|
|
var endpoints = discovery.DiscoverEndpoints();
|
|
|
|
// Assert
|
|
Assert.True(endpoints.Count >= 2);
|
|
Assert.Contains(endpoints, e => e.Method == "GET" && e.Path == "/api/test");
|
|
Assert.Contains(endpoints, e => e.Method == "POST" && e.Path == "/api/create");
|
|
}
|
|
|
|
[Fact]
|
|
public void ReflectionDiscovery_SetsServiceNameAndVersion()
|
|
{
|
|
// Arrange
|
|
var options = new StellaMicroserviceOptions
|
|
{
|
|
ServiceName = "my-service",
|
|
Version = "2.0.0",
|
|
Region = "eu1"
|
|
};
|
|
var discovery = new ReflectionEndpointDiscoveryProvider(
|
|
options,
|
|
[Assembly.GetExecutingAssembly()]);
|
|
|
|
// Act
|
|
var endpoints = discovery.DiscoverEndpoints();
|
|
var endpoint = endpoints.First(e => e.Path == "/api/test");
|
|
|
|
// Assert
|
|
Assert.Equal("my-service", endpoint.ServiceName);
|
|
Assert.Equal("2.0.0", endpoint.Version);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReflectionDiscovery_SetsStreamingAndTimeout()
|
|
{
|
|
// Arrange
|
|
var options = new StellaMicroserviceOptions
|
|
{
|
|
ServiceName = "test",
|
|
Version = "1.0.0",
|
|
Region = "eu1"
|
|
};
|
|
var discovery = new ReflectionEndpointDiscoveryProvider(
|
|
options,
|
|
[Assembly.GetExecutingAssembly()]);
|
|
|
|
// Act
|
|
var endpoints = discovery.DiscoverEndpoints();
|
|
var postEndpoint = endpoints.First(e => e.Path == "/api/create");
|
|
var getEndpoint = endpoints.First(e => e.Path == "/api/test");
|
|
|
|
// Assert
|
|
Assert.True(postEndpoint.SupportsStreaming);
|
|
Assert.Equal(TimeSpan.FromSeconds(60), postEndpoint.DefaultTimeout);
|
|
Assert.False(getEndpoint.SupportsStreaming);
|
|
Assert.Equal(TimeSpan.FromSeconds(30), getEndpoint.DefaultTimeout);
|
|
}
|
|
}
|