130 lines
7.7 KiB
C#
130 lines
7.7 KiB
C#
using AutoForge.Application.Features.DisplayUnits;
|
|
using AutoForge.Application.Features.Vehicles.Diagnostics;
|
|
using AutoForge.Application.Features.Vehicles.Profiles;
|
|
using AutoForge.Application.Features.Vehicles.Simulation;
|
|
using AutoForge.Application.Features.Vehicles.Telemetry.Recording;
|
|
using AutoForge.Application.Features.Vehicles.Telemetry.Replay;
|
|
using AutoForge.Core.ExecutionTargets;
|
|
using AutoForge.Core.Simulation;
|
|
using AutoForge.Core.Vehicles;
|
|
using AutoForge.Diagnostics;
|
|
using AutoForge.Simulation;
|
|
using AutoForge.UI;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace AutoForge.IntegrationTests;
|
|
|
|
public sealed partial class TelemetryReplayTests
|
|
{
|
|
[Fact]
|
|
public async Task ReplayCoexistsWithRealLiveSource_WithoutReadsModelCallsOrRecordingWorkspaceRebind()
|
|
{
|
|
using var fixture = await ReplayFixture.CreateAsync();
|
|
var source = new CountingDynamicsFactory(
|
|
new DynamicsDiagnosticTransportFactory(SyntheticDiagnosticExamples.CreateDefinitions()));
|
|
var services = new ServiceCollection();
|
|
services.AddAutoForgeVehicleProfiles();
|
|
services.AddAutoForgeDiagnostics([source]);
|
|
services.AddAutoForgeDynamicsConfigurations();
|
|
services.AddAutoForgeUi();
|
|
services.AddSingleton<ITelemetryArchiveStore>(fixture.Store);
|
|
services.AddSingleton<TimeProvider>(fixture.Clock);
|
|
services.AddSingleton<IReplayWaiter>(fixture.Waiter);
|
|
services.AddSingleton<IDisplayUnitPreferenceStore, IsolatedDisplayUnitPreferenceStore>();
|
|
using var provider = services.BuildServiceProvider();
|
|
provider.GetRequiredService<IVehicleProfileService>().Import(
|
|
File.ReadAllText(Path.Combine(AppContext.BaseDirectory, "Fixtures", "synthetic-truck-baseline.json")));
|
|
var configuration = provider.GetRequiredService<IDynamicsConfigurationService>().CreateExample(ProfileId, 1);
|
|
await using var live = await provider.GetRequiredService<IDiagnosticSessionService>().OpenDynamicsAsync(
|
|
ProfileId, 1, source.Descriptor.Target.ProviderIdentifier, ExecutionTargetKind.Simulation, configuration);
|
|
await live.DynamicsControl!.StepAsync(7);
|
|
var workspace = provider.GetRequiredService<TelemetryRecordingWorkspace>();
|
|
workspace.Start(live);
|
|
var endpoint = live.ExpectedEndpoints.Single(item => item.ModuleIdentifier == ModuleA);
|
|
Assert.True((await live.ReadSignalsAsync(endpoint)).IsSuccess);
|
|
workspace.Stop();
|
|
var oldRecording = TelemetryArchiveCodec.Serialize(workspace.CurrentCapture!);
|
|
var oldTrace = live.Trace.Entries.Count;
|
|
var oldStatus = live.DynamicsControl.Status;
|
|
var sourceCalls = source.TransportCalls;
|
|
var modelCalls = source.ModelCalls;
|
|
var factory = provider.GetRequiredService<ITelemetryReplayFactory>();
|
|
await using var player = factory.Create();
|
|
try
|
|
{
|
|
await player.LoadAsync(fixture.CaptureId);
|
|
Configure(player);
|
|
player.SeekTime(TimeSpan.FromSeconds(4).Ticks);
|
|
player.Previous();
|
|
player.First();
|
|
var preference = provider.GetRequiredService<DisplayUnitPreferenceService>();
|
|
Assert.True(preference.TrySet(new DisplayUnitSelection(DisplayUnitSystem.Metric, SpeedDisplayMode.Road)));
|
|
Assert.True(preference.TrySet(DisplayUnitSelection.Default));
|
|
await player.PlayAsync();
|
|
var wait = await fixture.Waiter.TakeAsync();
|
|
var stop = player.StopAsync();
|
|
wait.Release();
|
|
await stop.WaitAsync(Deadline);
|
|
Assert.Equal(sourceCalls, source.TransportCalls);
|
|
Assert.Equal(modelCalls, source.ModelCalls);
|
|
Assert.Equal(oldTrace, live.Trace.Entries.Count);
|
|
Assert.Equal(oldStatus, live.DynamicsControl.Status);
|
|
Assert.Equal(oldRecording, TelemetryArchiveCodec.Serialize(workspace.CurrentCapture!));
|
|
Assert.NotEqual(workspace.CurrentCapture!.Header.CaptureId, player.Snapshot.Context!.CaptureId);
|
|
Assert.Equal(0, fixture.Store.SaveCalls);
|
|
Assert.Equal(fixture.Bytes, await File.ReadAllBytesAsync(fixture.ArchivePath));
|
|
Assert.True((await live.ReadSignalsAsync(endpoint)).IsSuccess);
|
|
Assert.Equal(sourceCalls + 1, source.TransportCalls);
|
|
Assert.Equal(modelCalls, source.ModelCalls);
|
|
Assert.Equal(oldRecording, TelemetryArchiveCodec.Serialize(workspace.CurrentCapture!));
|
|
}
|
|
finally
|
|
{
|
|
fixture.Waiter.ReleaseAll();
|
|
await player.StopAsync();
|
|
}
|
|
}
|
|
|
|
private sealed class CountingDynamicsFactory(IDynamicsDiagnosticTransportFactory inner) : IDynamicsDiagnosticTransportFactory
|
|
{
|
|
public int TransportCalls { get; private set; }
|
|
public int ModelCalls { get; private set; }
|
|
public DiagnosticProviderDescriptor Descriptor => inner.Descriptor;
|
|
public IDiagnosticTransport OpenTransport(VehicleProfile profile, DiagnosticSessionIdentity identity) =>
|
|
throw new InvalidOperationException("This counting source requires an explicit dynamics configuration.");
|
|
public DynamicsSessionBinding OpenDynamicsTransport(VehicleProfile profile, DiagnosticSessionIdentity identity, DynamicsConfiguration configuration)
|
|
{
|
|
var binding = inner.OpenDynamicsTransport(profile, identity, configuration);
|
|
return new(new CountingTransport(binding.Transport, () => TransportCalls++),
|
|
new CountingControl(binding.Control, () => ModelCalls++));
|
|
}
|
|
}
|
|
|
|
private sealed class CountingTransport(IDiagnosticTransport inner, Action count) : IDiagnosticTransport
|
|
{
|
|
public IReadOnlyList<DiagnosticEndpoint> ExpectedEndpoints => inner.ExpectedEndpoints;
|
|
public ValueTask<TransportReply> SendAsync(ReadOnlyMemory<byte> request, CancellationToken cancellationToken)
|
|
{
|
|
count();
|
|
return inner.SendAsync(request, cancellationToken);
|
|
}
|
|
public ValueTask DisposeAsync() => inner.DisposeAsync();
|
|
}
|
|
|
|
private sealed class CountingControl(IDynamicsControl inner, Action count) : IDynamicsControl
|
|
{
|
|
public DynamicsRunStatus Status => inner.Status;
|
|
public double PacingFactor => inner.PacingFactor;
|
|
private Task<DynamicsRunStatus> Invoke(Func<Task<DynamicsRunStatus>> action) { count(); return action(); }
|
|
public Task<DynamicsRunStatus> StartAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.StartAsync(cancellationToken));
|
|
public Task<DynamicsRunStatus> PauseAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.PauseAsync(cancellationToken));
|
|
public Task<DynamicsRunStatus> ResumeAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.ResumeAsync(cancellationToken));
|
|
public Task<DynamicsRunStatus> StopAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.StopAsync(cancellationToken));
|
|
public Task<DynamicsRunStatus> ResetAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.ResetAsync(cancellationToken));
|
|
public Task<DynamicsRunStatus> StepAsync(int stepCount = 1, CancellationToken cancellationToken = default) => Invoke(() => inner.StepAsync(stepCount, cancellationToken));
|
|
public Task<DynamicsRunStatus> SetPacingFactorAsync(double factor, CancellationToken cancellationToken = default) => Invoke(() => inner.SetPacingFactorAsync(factor, cancellationToken));
|
|
public Task<DynamicsRunStatus> RunScenarioAsync(CancellationToken cancellationToken = default) => Invoke(() => inner.RunScenarioAsync(cancellationToken));
|
|
public ValueTask DisposeAsync() => inner.DisposeAsync();
|
|
}
|
|
}
|