Refactor and enhance tests for call graph extractors and connection management
- Updated JavaScriptCallGraphExtractorTests to improve naming conventions and test cases for Azure Functions, CLI commands, and socket handling. - Modified NodeCallGraphExtractorTests to correctly assert exceptions for null inputs. - Enhanced WitnessModalComponent tests in Angular to use Jasmine spies and improved assertions for path visualization and signature verification. - Added ConnectionState property for tracking connection establishment time in Router.Common. - Implemented validation for HelloPayload in ConnectionManager to ensure required fields are present. - Introduced RabbitMqContainerFixture method for restarting RabbitMQ container during tests. - Added integration tests for RabbitMq to verify connection recovery after broker restarts. - Created new BinaryCallGraphExtractorTests, GoCallGraphExtractorTests, and PythonCallGraphExtractorTests for comprehensive coverage of binary, Go, and Python call graph extraction functionalities. - Developed ConnectionManagerTests to validate connection handling, including rejection of invalid hello messages and proper cleanup on client disconnects.
This commit is contained in:
@@ -22,6 +22,11 @@ public sealed class ConnectionState
|
||||
/// </summary>
|
||||
public InstanceHealthStatus Status { get; set; } = InstanceHealthStatus.Unknown;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the UTC timestamp when this connection was established.
|
||||
/// </summary>
|
||||
public DateTime ConnectedAtUtc { get; init; } = DateTime.UtcNow;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the UTC timestamp of the last heartbeat.
|
||||
/// </summary>
|
||||
|
||||
@@ -58,6 +58,17 @@ internal sealed class ConnectionManager : IHostedService
|
||||
|
||||
private Task HandleHelloReceivedAsync(ConnectionState connectionState, HelloPayload payload)
|
||||
{
|
||||
if (!TryValidateHelloPayload(payload, out var validationError))
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Rejecting HELLO for connection {ConnectionId}: {Error}",
|
||||
connectionState.ConnectionId,
|
||||
validationError);
|
||||
|
||||
_connectionRegistry.RemoveChannel(connectionState.ConnectionId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"Connection registered: {ConnectionId} from {ServiceName}/{Version} with {EndpointCount} endpoints, {SchemaCount} schemas",
|
||||
connectionState.ConnectionId,
|
||||
@@ -78,6 +89,99 @@ internal sealed class ConnectionManager : IHostedService
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private static bool TryValidateHelloPayload(HelloPayload payload, out string error)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(payload.Instance.ServiceName))
|
||||
{
|
||||
error = "Instance.ServiceName is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(payload.Instance.Version))
|
||||
{
|
||||
error = "Instance.Version is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(payload.Instance.Region))
|
||||
{
|
||||
error = "Instance.Region is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(payload.Instance.InstanceId))
|
||||
{
|
||||
error = "Instance.InstanceId is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
var seen = new HashSet<(string Method, string Path)>(new EndpointKeyComparer());
|
||||
|
||||
foreach (var endpoint in payload.Endpoints)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(endpoint.Method))
|
||||
{
|
||||
error = "Endpoint.Method is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(endpoint.Path) || !endpoint.Path.StartsWith('/'))
|
||||
{
|
||||
error = "Endpoint.Path must start with '/'";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!string.Equals(endpoint.ServiceName, payload.Instance.ServiceName, StringComparison.OrdinalIgnoreCase) ||
|
||||
!string.Equals(endpoint.Version, payload.Instance.Version, StringComparison.Ordinal))
|
||||
{
|
||||
error = "Endpoint.ServiceName/Version must match HelloPayload.Instance";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!seen.Add((endpoint.Method, endpoint.Path)))
|
||||
{
|
||||
error = $"Duplicate endpoint registration for {endpoint.Method} {endpoint.Path}";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (endpoint.SchemaInfo is not null)
|
||||
{
|
||||
if (endpoint.SchemaInfo.RequestSchemaId is not null &&
|
||||
!payload.Schemas.ContainsKey(endpoint.SchemaInfo.RequestSchemaId))
|
||||
{
|
||||
error = $"Endpoint schema reference missing: requestSchemaId='{endpoint.SchemaInfo.RequestSchemaId}'";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (endpoint.SchemaInfo.ResponseSchemaId is not null &&
|
||||
!payload.Schemas.ContainsKey(endpoint.SchemaInfo.ResponseSchemaId))
|
||||
{
|
||||
error = $"Endpoint schema reference missing: responseSchemaId='{endpoint.SchemaInfo.ResponseSchemaId}'";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
error = string.Empty;
|
||||
return true;
|
||||
}
|
||||
|
||||
private sealed class EndpointKeyComparer : IEqualityComparer<(string Method, string Path)>
|
||||
{
|
||||
public bool Equals((string Method, string Path) x, (string Method, string Path) y)
|
||||
{
|
||||
return string.Equals(x.Method, y.Method, StringComparison.OrdinalIgnoreCase) &&
|
||||
string.Equals(x.Path, y.Path, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public int GetHashCode((string Method, string Path) obj)
|
||||
{
|
||||
return HashCode.Combine(
|
||||
StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Method),
|
||||
StringComparer.OrdinalIgnoreCase.GetHashCode(obj.Path));
|
||||
}
|
||||
}
|
||||
|
||||
private Task HandleHeartbeatReceivedAsync(ConnectionState connectionState, HeartbeatPayload payload)
|
||||
{
|
||||
_logger.LogDebug(
|
||||
|
||||
@@ -80,6 +80,17 @@ public sealed class RabbitMqContainerFixture : RouterCollectionFixture, IAsyncDi
|
||||
};
|
||||
}
|
||||
|
||||
public async Task RestartAsync()
|
||||
{
|
||||
if (_container is null)
|
||||
{
|
||||
throw new InvalidOperationException("RabbitMQ container is not running.");
|
||||
}
|
||||
|
||||
await _container.StopAsync();
|
||||
await _container.StartAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task InitializeAsync()
|
||||
{
|
||||
|
||||
@@ -50,9 +50,11 @@ public sealed class RabbitMqIntegrationTests : IAsyncLifetime
|
||||
_fixture.GetLogger<RabbitMqTransportServer>());
|
||||
}
|
||||
|
||||
private RabbitMqTransportClient CreateClient(string? instanceId = null)
|
||||
private RabbitMqTransportClient CreateClient(string? instanceId = null, string? nodeId = null)
|
||||
{
|
||||
var options = _fixture.CreateOptions(instanceId: instanceId ?? $"svc-{Guid.NewGuid():N}"[..12]);
|
||||
var options = _fixture.CreateOptions(
|
||||
instanceId: instanceId ?? $"svc-{Guid.NewGuid():N}"[..12],
|
||||
nodeId: nodeId);
|
||||
return new RabbitMqTransportClient(
|
||||
Options.Create(options),
|
||||
_fixture.GetLogger<RabbitMqTransportClient>());
|
||||
@@ -137,8 +139,9 @@ public sealed class RabbitMqIntegrationTests : IAsyncLifetime
|
||||
public async Task ClientConnectAsync_SendsHelloFrame_ServerReceives()
|
||||
{
|
||||
// Arrange
|
||||
_server = CreateServer("gw-hello-test");
|
||||
_client = CreateClient("svc-hello-test");
|
||||
const string nodeId = "gw-hello-test";
|
||||
_server = CreateServer(nodeId);
|
||||
_client = CreateClient("svc-hello-test", nodeId: nodeId);
|
||||
|
||||
Frame? receivedFrame = null;
|
||||
string? receivedConnectionId = null;
|
||||
@@ -214,8 +217,9 @@ public sealed class RabbitMqIntegrationTests : IAsyncLifetime
|
||||
public async Task ServerReceivesHeartbeat_UpdatesLastHeartbeatUtc()
|
||||
{
|
||||
// Arrange
|
||||
_server = CreateServer("gw-heartbeat-test");
|
||||
_client = CreateClient("svc-heartbeat-test");
|
||||
const string nodeId = "gw-heartbeat-test";
|
||||
_server = CreateServer(nodeId);
|
||||
_client = CreateClient("svc-heartbeat-test", nodeId: nodeId);
|
||||
|
||||
var heartbeatReceived = new TaskCompletionSource<bool>();
|
||||
_server.OnFrame += (connectionId, frame) =>
|
||||
@@ -270,6 +274,119 @@ public sealed class RabbitMqIntegrationTests : IAsyncLifetime
|
||||
|
||||
#endregion
|
||||
|
||||
#region Connection Recovery Tests
|
||||
|
||||
[RabbitMqIntegrationFact]
|
||||
public async Task ConnectionRecovery_BrokerRestart_AllowsPublishingAndConsumingAgain()
|
||||
{
|
||||
// Arrange
|
||||
const string nodeId = "gw-recovery-test";
|
||||
const string instanceId = "svc-recovery-test";
|
||||
|
||||
_server = CreateServer(nodeId);
|
||||
_client = CreateClient(instanceId, nodeId: nodeId);
|
||||
|
||||
await _server.StartAsync(CancellationToken.None);
|
||||
|
||||
var instance = new InstanceDescriptor
|
||||
{
|
||||
InstanceId = instanceId,
|
||||
ServiceName = "test-service",
|
||||
Version = "1.0.0",
|
||||
Region = "us-east-1"
|
||||
};
|
||||
|
||||
await _client.ConnectAsync(instance, [], CancellationToken.None);
|
||||
|
||||
await EventuallyAsync(
|
||||
() => _server.ConnectionCount > 0,
|
||||
timeout: TimeSpan.FromSeconds(15));
|
||||
|
||||
// Act: force broker restart and wait for client/server recovery.
|
||||
await _fixture.RestartAsync();
|
||||
|
||||
var heartbeat = new HeartbeatPayload
|
||||
{
|
||||
InstanceId = instanceId,
|
||||
Status = InstanceHealthStatus.Healthy,
|
||||
InFlightRequestCount = 0,
|
||||
ErrorRate = 0,
|
||||
TimestampUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
var heartbeatReceived = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
_server.OnFrame += (_, frame) =>
|
||||
{
|
||||
if (frame.Type == FrameType.Heartbeat)
|
||||
{
|
||||
heartbeatReceived.TrySetResult(true);
|
||||
}
|
||||
};
|
||||
|
||||
await EventuallyAsync(
|
||||
async () =>
|
||||
{
|
||||
await _client.SendHeartbeatAsync(heartbeat, CancellationToken.None);
|
||||
return true;
|
||||
},
|
||||
timeout: TimeSpan.FromSeconds(30),
|
||||
swallowExceptions: true);
|
||||
|
||||
await heartbeatReceived.Task.WaitAsync(TimeSpan.FromSeconds(30));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static async Task EventuallyAsync(
|
||||
Func<bool> predicate,
|
||||
TimeSpan timeout,
|
||||
TimeSpan? pollInterval = null)
|
||||
{
|
||||
pollInterval ??= TimeSpan.FromMilliseconds(250);
|
||||
var deadline = DateTime.UtcNow + timeout;
|
||||
|
||||
while (DateTime.UtcNow < deadline)
|
||||
{
|
||||
if (predicate())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(pollInterval.Value);
|
||||
}
|
||||
|
||||
predicate().Should().BeTrue("condition should become true within {0}", timeout);
|
||||
}
|
||||
|
||||
private static async Task EventuallyAsync(
|
||||
Func<Task<bool>> predicate,
|
||||
TimeSpan timeout,
|
||||
bool swallowExceptions,
|
||||
TimeSpan? pollInterval = null)
|
||||
{
|
||||
pollInterval ??= TimeSpan.FromMilliseconds(500);
|
||||
var deadline = DateTime.UtcNow + timeout;
|
||||
|
||||
while (DateTime.UtcNow < deadline)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (await predicate())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch when (swallowExceptions)
|
||||
{
|
||||
// Retry
|
||||
}
|
||||
|
||||
await Task.Delay(pollInterval.Value);
|
||||
}
|
||||
|
||||
(await predicate()).Should().BeTrue("condition should become true within {0}", timeout);
|
||||
}
|
||||
|
||||
#region Queue Declaration Tests
|
||||
|
||||
[RabbitMqIntegrationFact]
|
||||
|
||||
Reference in New Issue
Block a user