Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions tests/Cosmos.Network.Http.Tests/HttpLoopbackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ private sealed class TestServer : IDisposable
private readonly List<Exception> _errors = new List<Exception>();
private int _taken;
private int _handled;
private volatile bool _restarting;

internal TestServer(bool https = false, SslProtocols protocols = SslProtocols.Tls12)
{
Expand Down Expand Up @@ -55,13 +56,35 @@ internal TestServer(bool https = false, SslProtocols protocols = SslProtocols.Tl

internal void Start() => _listener.Start();

// Stop then Start with the serving thread held back: seeing the Stop between the two, it would return from
// GetContext, close the listener and leave Start to throw.
internal void Restart()
{
_restarting = true;
try
{
_listener.Stop();
_listener.Start();
}
finally
{
_restarting = false;
}
}

private void Serve()
{
while (_listener.IsListening)
{
HttpListenerContext context = _listener.GetContext();
if (context == null)
{
if (_restarting)
{
SpinWait.SpinUntil(() => !_restarting);
continue;
}

break;
}

Expand Down Expand Up @@ -818,8 +841,7 @@ public void Listener_StopThenStart_KeepsServing()
Assert.AreEqual("Hello, Cosmos", client.GetString(server.Url("/hello")));

// From this thread, before the serving one acts on the Stop: undone.
server.Stop();
server.Start();
server.Restart();

Thread.Sleep(200);
Assert.AreEqual("Hello, Cosmos", client.GetString(server.Url("/hello")));
Expand Down
Loading