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