Files
ucvl-zonghengjia-open-source/windows/TailnetPingConfirmationSelfTest.cs

37 lines
2.3 KiB
C#

namespace UCVL.VPNPlus;
internal static class TailnetPingConfirmationSelfTest
{
internal static async Task<Dictionary<string, bool>> CheckAsync()
{
var checks = new Dictionary<string, bool>();
var calls = 0;
var reachable = await TailnetPingConfirmation.ProbeAsync(_ => Task.FromResult(++calls == 2), CancellationToken.None);
checks["single_lost_ping_followed_by_success_is_reachable"] = reachable && calls == 2;
calls = 0;
reachable = await TailnetPingConfirmation.ProbeAsync(_ => Task.FromResult(++calls == 3), CancellationToken.None);
checks["two_lost_pings_followed_by_success_do_not_switch"] = reachable && calls == 3;
calls = 0;
reachable = await TailnetPingConfirmation.ProbeAsync(_ => { calls++; return Task.FromResult(false); }, CancellationToken.None);
checks["three_failed_icmp_attempts_are_one_failed_round"] = !reachable && calls == 3;
calls = 0;
reachable = await TailnetPingConfirmation.ProbeAsync(_ => ++calls == 1
? Task.FromException<bool>(new IOException("fixture ping failure")) : Task.FromResult(true), CancellationToken.None);
checks["one_ping_exception_does_not_force_fallback"] = reachable && calls == 2;
var pending = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
reachable = await TailnetPingConfirmation.ProbeAsync(_ => pending.Task, CancellationToken.None, TimeSpan.FromMilliseconds(30));
checks["uncooperative_ping_callback_has_bounded_deadline"] = !reachable;
using var caller = new CancellationTokenSource(); caller.Cancel();
var cancelled = false;
try { await TailnetPingConfirmation.ProbeAsync(_ => Task.FromResult(true), caller.Token); }
catch (OperationCanceledException error) { cancelled = error.CancellationToken == caller.Token; }
checks["caller_cancellation_is_not_a_failed_ping"] = cancelled;
using var race = new CancellationTokenSource();
cancelled = false;
try { await TailnetPingConfirmation.ProbeAsync(_ => { race.Cancel(); return Task.FromResult(true); }, race.Token); }
catch (OperationCanceledException error) { cancelled = error.CancellationToken == race.Token; }
checks["cancelled_successful_ping_cannot_publish_old_result"] = cancelled;
return checks;
}
}