Skip to content

Commit

Permalink
Fix local failure in GET_GracefulServerShutdown_AbortRequestsAfterHos…
Browse files Browse the repository at this point in the history
…tTimeout (#58747)
  • Loading branch information
JamesNK authored Nov 7, 2024
1 parent 6f8fb16 commit dc68e3b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,10 @@ public async Task GET_GracefulServerShutdown_AbortRequestsAfterHostTimeout(HttpP
var readAsyncTask = new TaskCompletionSource<Task>(TaskCreationOptions.RunContinuationsAsynchronously);
var requestAbortedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

// Wait 2.5 seconds in debug (local development) and 15 seconds in production (CI)
// Use half the default timeout to ensure the host shuts down before the test throws an error while waiting.
var shutdownTimeout = Microsoft.AspNetCore.InternalTesting.TaskExtensions.DefaultTimeoutTimeSpan / 2;

var builder = CreateHostBuilder(async context =>
{
context.RequestAborted.Register(() => requestAbortedTcs.SetResult());
Expand Down Expand Up @@ -2021,7 +2025,8 @@ public async Task GET_GracefulServerShutdown_AbortRequestsAfterHostTimeout(HttpP
listenOptions.Protocols = protocol;
listenOptions.UseHttps(TestResources.GetTestCertificate());
});
});
},
shutdownTimeout: shutdownTimeout);

using (var host = builder.Build())
using (var client = HttpHelpers.CreateClient())
Expand Down Expand Up @@ -2061,17 +2066,21 @@ await WaitForLogAsync(logs =>
}, "Check for initial GOAWAY frame sent on server initiated shutdown.");
}

Logger.LogInformation("Getting read task");
var readTask = await readAsyncTask.Task.DefaultTimeout();

// Assert
Logger.LogInformation("Waiting for error from read task");
var ex = await Assert.ThrowsAnyAsync<Exception>(() => readTask).DefaultTimeout();
while (ex.InnerException != null)

var rootException = ex;
while (rootException.InnerException != null)
{
ex = ex.InnerException;
rootException = rootException.InnerException;
}

Assert.IsType<ConnectionAbortedException>(ex);
Assert.Equal("The connection was aborted because the server is shutting down and request processing didn't complete within the time specified by HostOptions.ShutdownTimeout.", ex.Message);
Assert.IsType<ConnectionAbortedException>(rootException);
Assert.Equal("The connection was aborted because the server is shutting down and request processing didn't complete within the time specified by HostOptions.ShutdownTimeout.", rootException.Message);

await requestAbortedTcs.Task.DefaultTimeout();

Expand Down Expand Up @@ -2191,8 +2200,8 @@ public async Task ServerReset_InvalidErrorCode()
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
}

private IHostBuilder CreateHostBuilder(RequestDelegate requestDelegate, HttpProtocols? protocol = null, Action<KestrelServerOptions> configureKestrel = null)
private IHostBuilder CreateHostBuilder(RequestDelegate requestDelegate, HttpProtocols? protocol = null, Action<KestrelServerOptions> configureKestrel = null, TimeSpan? shutdownTimeout = null)
{
return HttpHelpers.CreateHostBuilder(AddTestLogging, requestDelegate, protocol, configureKestrel);
return HttpHelpers.CreateHostBuilder(AddTestLogging, requestDelegate, protocol, configureKestrel, shutdownTimeout: shutdownTimeout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static HttpMessageInvoker CreateClient(TimeSpan? idleTimeout = null, Time
return new HttpMessageInvoker(handler);
}

public static IHostBuilder CreateHostBuilder(Action<IServiceCollection> configureServices, RequestDelegate requestDelegate, HttpProtocols? protocol = null, Action<KestrelServerOptions> configureKestrel = null, bool? plaintext = null)
public static IHostBuilder CreateHostBuilder(Action<IServiceCollection> configureServices, RequestDelegate requestDelegate, HttpProtocols? protocol = null, Action<KestrelServerOptions> configureKestrel = null, bool? plaintext = null, TimeSpan? shutdownTimeout = null)
{
return new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
Expand Down Expand Up @@ -102,7 +102,7 @@ public static IHostBuilder CreateHostBuilder(Action<IServiceCollection> configur
}
else
{
o.ShutdownTimeout = TimeSpan.FromSeconds(5);
o.ShutdownTimeout = shutdownTimeout ?? TimeSpan.FromSeconds(5);
}
});
}
Expand Down

0 comments on commit dc68e3b

Please sign in to comment.