diff --git a/docs/decisioni.md b/docs/decisioni.md index 5250fe9..b3fa08e 100644 --- a/docs/decisioni.md +++ b/docs/decisioni.md @@ -377,3 +377,4 @@ Ogni scelta tecnica presa strada facendo, con data e motivo (specifica §15). | 2026-11-16 | Con `--accesso-di-prova` il primo controllo della sessione all'avvio non segnala nulla (`RefreshAsync` con `mayReport: false`) | La sessione mancante è quella che l'accesso automatico sta per aprire, non una disconnessione | | 2026-11-16 | «Pagina iniziale» vale finché dura il permesso di chi l'ha premuta: la finestra di Windows e la vista remota passano un gettone che si annulla con il loro permesso, e la connessione guarda di nuovo, con il turno della pagina in mano, che Alberto guidi ancora; senza turno rinuncia invece di prendere la pagina. Nella vista remota la pressione corre a parte, una per volta, e intanto clic e tasti si scartano | L'attesa (fine della sessione, turno della pagina) può durare mezzo minuto: Alberto poteva aver chiuso la finestra e Febe navigava senza permesso e fuori dal Governatore (regole 7 e 8); nella vista l'attesa nel ciclo di ricezione bloccava i comandi e poi li riversava sulla pagina nuova | | 2026-11-16 | `Repaint` della finestra «Browser di Febe» chiede solo di ridisegnare (`RDW_INVALIDATE \| RDW_ALLCHILDREN`), senza `RDW_UPDATENOW` | Le finestre figlie sono del processo del browser di WebView2: un processo bloccato avrebbe fermato il thread dell'interfaccia in un ridisegno sincrono | +| 2026-11-16 | Le porte libere delle prove (`LocalTestSite.FreePort`) vengono da 20000–29999, ciascuna una volta sola per processo e provata prima di darla, non più dalla porta 0 del sistema | Nella CI 350 (#52) Chromium non è partito perché la porta di debug, presa dall'intervallo effimero di Linux e chiusa prima di usarla, era stata nel frattempo occupata (come per #46: la causa, non un tempo più lungo) | diff --git a/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs b/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs index 2a94fc4..2d66934 100644 --- a/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs +++ b/tests/Febe.Tests/Browser/TestSupport/LocalTestSite.cs @@ -49,13 +49,42 @@ public sealed class LocalTestSite : IDisposable public string CdnUrl(string path) => $"http://{CdnHost}{path}"; + // Ports handed out by FreePort come from 20000-29999: below the ephemeral ranges of Linux (32768-60999) and Windows + // (49152-65535), so an outgoing connection of another test cannot take one between the probe and the moment the + // test binds it (a port handed out by the system's port 0 could, CI run 350 of #52). Each port is handed out once + // per process; the start depends on the process id, so two test processes on one machine rarely meet. + private const int FirstTestPort = 20000; + private const int TestPorts = 10000; + private static int _nextPort = Environment.ProcessId * 7919 % TestPorts; + + /// + /// A port of 127.0.0.1 that is free now, never handed out before in this process and outside the system's ephemeral + /// range. The caller binds it soon after; nothing can reserve it in between, but no other test of this process gets + /// it and no outgoing connection is given it. + /// public static int FreePort() { - var probe = new TcpListener(IPAddress.Loopback, 0); - probe.Start(); - var port = ((IPEndPoint)probe.LocalEndpoint).Port; - probe.Stop(); - return port; + for (var attempt = 0; attempt < TestPorts; attempt++) + { + var port = FirstTestPort + (int)((uint)Interlocked.Increment(ref _nextPort) % TestPorts); + var probe = new TcpListener(IPAddress.Loopback, port); + try + { + probe.Start(); + } + catch (SocketException) + { + continue; + } + finally + { + probe.Stop(); + } + + return port; + } + + throw new InvalidOperationException("Nessuna porta libera fra 20000 e 29999."); } private int _disposed; diff --git a/tests/Febe.Tests/Desktop/DesktopCompositionTests.cs b/tests/Febe.Tests/Desktop/DesktopCompositionTests.cs index 9b28c33..cbdad9d 100644 --- a/tests/Febe.Tests/Desktop/DesktopCompositionTests.cs +++ b/tests/Febe.Tests/Desktop/DesktopCompositionTests.cs @@ -336,12 +336,5 @@ public sealed class DesktopCompositionTests StringAssert.Contains(log, "Windows non ha legato Edge a Febe"); } - private static int FreePort() - { - var listener = new System.Net.Sockets.TcpListener(IPAddress.Loopback, 0); - listener.Start(); - var port = ((IPEndPoint)listener.LocalEndpoint).Port; - listener.Stop(); - return port; - } + private static int FreePort() => Febe.Tests.Browser.TestSupport.LocalTestSite.FreePort(); }