Files
Febe/tools/Febe.SitoDiProva/SiteControl.cs
T

112 lines
3.5 KiB
C#

using System.Collections.Concurrent;
namespace Febe.SitoDiProva;
/// <summary>The stop cases the site can play (spec §14.2), switched by the tests through <c>/_controllo</c>.</summary>
public enum StopMode
{
/// <summary>Everything works.</summary>
Normal,
/// <summary>Data requests get HTTP 429 (Rallentamento).</summary>
TooManyRequests,
/// <summary>Pages are redirected to <c>/verifica</c>; data requests get 403 and the page moves there (Verifica).</summary>
Challenge,
/// <summary>The session cookie is removed and pages move to <c>/sessione-scaduta</c> (Disconnessione).</summary>
SessionEnded,
/// <summary>Data requests return empty pages.</summary>
EmptyPages,
/// <summary>Data requests return a shape no reader knows (the site «changed»).</summary>
UnknownFormat,
}
/// <summary>Switches and counters of the test site. Thread-safe.</summary>
public sealed class SiteControl
{
private readonly Lock _gate = new();
private readonly ConcurrentQueue<string> _dataRequests = new();
private StopMode _mode = StopMode.Normal;
private int _armAfter;
/// <summary>How long a photo story stays on screen before the viewer moves on by itself.</summary>
public TimeSpan StoryDuration { get; set; } = TimeSpan.FromSeconds(5);
/// <summary>Validity of the CDN addresses handed out from now on.</summary>
public TimeSpan AddressLifetime { get; set; } = TimeSpan.FromHours(6);
/// <summary>Paths with query of every data request, in arrival order (for the tests).</summary>
public IReadOnlyList<string> DataRequests => [.. _dataRequests];
/// <summary>Number of files served by the CDN.</summary>
public int CdnFilesServed => _cdnFiles;
/// <summary>Number of CDN requests refused because the address had expired.</summary>
public int CdnExpired => _cdnExpired;
private int _cdnFiles;
private int _cdnExpired;
public StopMode Mode
{
get
{
lock (_gate)
{
return _armAfter > 0 ? StopMode.Normal : _mode;
}
}
}
/// <summary>Sets a stop case; with <paramref name="afterDataRequests"/> it starts after that many more data requests.</summary>
public void Set(StopMode mode, int afterDataRequests = 0)
{
lock (_gate)
{
_mode = mode;
_armAfter = Math.Max(0, afterDataRequests);
}
}
/// <summary>Back to normal; counters cleared; default story duration and address lifetime.</summary>
public void Reset()
{
lock (_gate)
{
_mode = StopMode.Normal;
_armAfter = 0;
}
_dataRequests.Clear();
StoryDuration = TimeSpan.FromSeconds(5);
AddressLifetime = TimeSpan.FromHours(6);
Interlocked.Exchange(ref _cdnFiles, 0);
Interlocked.Exchange(ref _cdnExpired, 0);
}
public void ClearRequests() => _dataRequests.Clear();
/// <summary>Records a data request and returns the mode that applies to it.</summary>
internal StopMode OnDataRequest(string pathAndQuery)
{
_dataRequests.Enqueue(pathAndQuery);
lock (_gate)
{
if (_armAfter > 0)
{
_armAfter--;
return StopMode.Normal;
}
return _mode;
}
}
internal void OnCdnServed() => Interlocked.Increment(ref _cdnFiles);
internal void OnCdnExpired() => Interlocked.Increment(ref _cdnExpired);
}