7bd3cdfd14
The pull on connect tried four times (now, then after 15 s, 60 s and 5 min, the same for every device) and then left the device on what it held until the next reconnect. A fleet whose pulls timed out together asked again together. It now asks again after a pause that doubles from 10 s up to 10 minutes, a random point in the upper half of each, for as long as the session lasts; it stops once answered, or when a pushed set newer than the one it started with has arrived meanwhile. After four unanswered tries it says so once in the log and goes on asking. The request and its sealing are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
4.5 KiB
C#
126 lines
4.5 KiB
C#
using Agent.Configuration;
|
|
using Agent.Messaging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shared.Contracts.Messages;
|
|
using Shared.Contracts.Models;
|
|
using Shared.Infrastructure.Configuration;
|
|
using Shared.Infrastructure.Constants;
|
|
using Xunit;
|
|
|
|
namespace Agent.Tests;
|
|
|
|
/// <summary>
|
|
/// The settings pull on connect: a missed answer is asked again after a pause that doubles (from
|
|
/// 10 s up to 10 minutes), a random upper half of it; it keeps asking until answered, and stops
|
|
/// when a pushed set newer than the one it started with arrived meanwhile.
|
|
/// </summary>
|
|
public class AgentSettingsClientTests
|
|
{
|
|
private const string ClientId = "client-pull0001";
|
|
|
|
private readonly InMemoryRegistryStore registry = new();
|
|
|
|
public AgentSettingsClientTests()
|
|
=> registry.SetString(Branding.AgentRuntimeRegistryKey, AgentRuntimeKeys.ClientId, ClientId);
|
|
|
|
private ManagedSettingsApplier Applier()
|
|
{
|
|
var loader = new AgentConfigurationLoader(registry, allowEnvironment: false);
|
|
return new ManagedSettingsApplier(loader, loader.Load(), ClientId, NullLogger.Instance);
|
|
}
|
|
|
|
private static AgentSettingsPayload Set(long version)
|
|
=> new(ClientId, version, [new AgentSettingValue("AllowRegistryAccess", "false", "Lockdown")]);
|
|
|
|
[Fact]
|
|
public void ThePauseDoublesUpToTenMinutesAndIsARandomUpperHalfOfIt()
|
|
{
|
|
var random = new Random(7);
|
|
for (var attempt = 0; attempt < 12; attempt++)
|
|
{
|
|
var ceiling = Math.Min(600, 10 * Math.Pow(2, attempt));
|
|
var pauses = Enumerable.Range(0, 200).Select(_ => AgentSettingsClient.RetryDelay(attempt, random).TotalSeconds).ToList();
|
|
Assert.All(pauses, p => Assert.InRange(p, ceiling / 2, ceiling));
|
|
|
|
// Spread over the range, not all at one point (a fleet that missed together asks apart).
|
|
Assert.True(pauses.Max() - pauses.Min() > ceiling / 4, $"attempt {attempt}: {pauses.Min()}..{pauses.Max()}");
|
|
}
|
|
|
|
Assert.Equal(TimeSpan.FromMinutes(10), AgentSettingsClient.MaxRetryDelay);
|
|
Assert.InRange(AgentSettingsClient.RetryDelay(1000, random).TotalSeconds, 300, 600);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AMissedAnswerIsAskedAgainUntilOneComesAndThenApplied()
|
|
{
|
|
var applier = Applier();
|
|
var tries = 0;
|
|
var pauses = new List<TimeSpan>();
|
|
var client = new AgentSettingsClient(
|
|
_ => ++tries switch
|
|
{
|
|
1 => throw new TimeoutException("no reply in 20 s"),
|
|
<= 7 => Task.FromResult<AgentSettingsPayload?>(null),
|
|
_ => Task.FromResult<AgentSettingsPayload?>(Set(42)),
|
|
},
|
|
applier, NullLogger.Instance,
|
|
(pause, _) =>
|
|
{
|
|
pauses.Add(pause);
|
|
return Task.CompletedTask;
|
|
},
|
|
new Random(3));
|
|
|
|
// Past the point where it says the server does not answer, it keeps asking.
|
|
Assert.Equal(8, await client.RunAsync(TestContext.Current.CancellationToken));
|
|
Assert.Equal(42, applier.Version);
|
|
Assert.Equal(7, pauses.Count);
|
|
for (var i = 0; i < pauses.Count; i++)
|
|
{
|
|
var ceiling = Math.Min(600, 10 * Math.Pow(2, i));
|
|
Assert.InRange(pauses[i].TotalSeconds, ceiling / 2, ceiling);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task APushNewerThanWhatItStartedWithEndsTheAsking()
|
|
{
|
|
var applier = Applier();
|
|
applier.Apply(Set(10));
|
|
var tries = 0;
|
|
var client = new AgentSettingsClient(
|
|
_ =>
|
|
{
|
|
tries++;
|
|
return Task.FromResult<AgentSettingsPayload?>(null);
|
|
},
|
|
applier, NullLogger.Instance,
|
|
(_, _) =>
|
|
{
|
|
// A push arrives while it waits.
|
|
applier.Apply(Set(11));
|
|
return Task.CompletedTask;
|
|
});
|
|
|
|
Assert.Equal(1, await client.RunAsync(TestContext.Current.CancellationToken));
|
|
Assert.Equal(1, tries);
|
|
Assert.Equal(11, applier.Version);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheSessionEndingStopsTheAsking()
|
|
{
|
|
using var end = new CancellationTokenSource();
|
|
var client = new AgentSettingsClient(
|
|
_ => Task.FromResult<AgentSettingsPayload?>(null),
|
|
Applier(), NullLogger.Instance,
|
|
(_, token) =>
|
|
{
|
|
end.Cancel();
|
|
return Task.Delay(Timeout.Infinite, token);
|
|
});
|
|
|
|
Assert.Equal(1, await client.RunAsync(end.Token));
|
|
}
|
|
}
|