Files
Leonid PershinandClaude Opus 5 44fb0d3a5f Gate network parsers on a working proxy and remember what worked
The pool now warms up from what the previous run learned instead of starting
cold every launch. Startup probes the remembered proxies first, stops as soon
as ProxyMinimumLive of them answer, and writes the survivors to
proxies.state.json after the warm-up and again on shutdown. Only proxies that
ever answered are stored: the feed republishes a few thousand dead addresses
every five minutes, and "was dead an hour ago" says almost nothing.

Remembered state is a hint, not a verdict. A restored proxy sorts first in the
warm-up queue but is not counted live until it answers in this session -
otherwise a launch a week later would report live proxies it had never spoken
to, the warm-up would skip the very entries it exists to re-check, and the
parser gate would open on week-old evidence.

That gate is the other half: a parser declaring RequiresNetwork will not run
while the pool has nothing live. The Parse page disables the run button and
shows a banner that leads to the Proxies page. Parsers that work on pasted text
are never gated - they have nothing to route, and blocking them would make the
app useless whenever the public lists are down. Two new settings cover the
escape hatch and the target: "allow network parsers without a proxy" and how
many live proxies to find at startup.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-13 19:11:51 +03:00

104 lines
3.4 KiB
C#

using AvParser.Core.Proxies;
using AvParser.Infrastructure.Proxies;
namespace AvParser.UI.Tests.Fakes;
/// <summary>A remembered-state store that keeps everything in memory.</summary>
internal sealed class FakeProxyStateStore : IProxyStateStore
{
public Dictionary<string, ProxyStateRecord> State { get; } = new(StringComparer.Ordinal);
public int Saves { get; private set; }
public Task<IReadOnlyDictionary<string, ProxyStateRecord>> LoadAsync(
CancellationToken cancellationToken = default
) => Task.FromResult<IReadOnlyDictionary<string, ProxyStateRecord>>(State);
public Task SaveAsync(IEnumerable<ProxyEntry> entries, CancellationToken cancellationToken = default)
{
Saves++;
return Task.CompletedTask;
}
}
/// <summary>A feed whose contents the test supplies.</summary>
internal sealed class FakeProxySource(IEnumerable<ProxyEndpoint>? endpoints = null) : IProxySource
{
public string Id => "fake-feed";
public string DisplayName => "Fake feed";
public ProxySourceKind Kind => ProxySourceKind.Feed;
public List<ProxyEndpoint> Endpoints { get; } = [.. endpoints ?? []];
public Task<IReadOnlyList<ProxyEndpoint>> GetProxiesAsync(CancellationToken cancellationToken = default) =>
Task.FromResult<IReadOnlyList<ProxyEndpoint>>(Endpoints.ToArray());
}
/// <summary>An in-memory stand-in for the user's editable list.</summary>
internal sealed class FakeMutableProxySource : IMutableProxySource
{
public string Id => "fake-custom";
public string DisplayName => "Fake custom list";
public ProxySourceKind Kind => ProxySourceKind.Custom;
public List<ProxyEndpoint> Endpoints { get; } = [];
public Task<IReadOnlyList<ProxyEndpoint>> GetProxiesAsync(CancellationToken cancellationToken = default) =>
Task.FromResult<IReadOnlyList<ProxyEndpoint>>(Endpoints.ToArray());
public Task<int> AddAsync(IEnumerable<ProxyEndpoint> endpoints, CancellationToken cancellationToken = default)
{
var added = 0;
foreach (var endpoint in endpoints)
{
if (Endpoints.Any(existing => string.Equals(existing.Key, endpoint.Key, StringComparison.Ordinal)))
{
continue;
}
Endpoints.Add(endpoint);
added++;
}
return Task.FromResult(added);
}
public Task<bool> RemoveAsync(ProxyEndpoint endpoint, CancellationToken cancellationToken = default) =>
Task.FromResult(
Endpoints.RemoveAll(existing => string.Equals(existing.Key, endpoint.Key, StringComparison.Ordinal)) > 0
);
public Task ClearAsync(CancellationToken cancellationToken = default)
{
Endpoints.Clear();
return Task.CompletedTask;
}
}
/// <summary>A probe whose verdict the test decides.</summary>
internal sealed class FakeProxyProbe : IProxyProbe
{
private readonly Dictionary<string, bool> _verdicts = new(StringComparer.Ordinal);
public FakeProxyProbe Set(ProxyEndpoint endpoint, bool alive)
{
_verdicts[endpoint.Key] = alive;
return this;
}
public Task<ProxyProbeResult> ProbeAsync(
ProxyEndpoint endpoint,
ProxyOptions options,
CancellationToken cancellationToken = default
) =>
Task.FromResult(
_verdicts.TryGetValue(endpoint.Key, out var alive) && alive
? ProxyProbeResult.Success(TimeSpan.FromMilliseconds(20))
: ProxyProbeResult.Failure("dead")
);
}