/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Components/test/testassets/Components.TestServer/AutoPauseTestEndpoints.cs
137 строк
6 KB
Ilona Tomkowicz
Circuit can be paused by Blazor when inactivity is detected (#67098)
07 июл 2026, 14:07
Не верифицирован
07 июл 2026, 14:07
ce86c1a
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. namespace TestServer; // Endpoints for AutoPauseDeferralTests that launch download deterministically on TSC. internal static class AutoPauseTestEndpoints { public static void MapAutoPauseTestEndpoints(this IEndpointRouteBuilder endpoints) { endpoints.MapGet("/autopause-test/download/{token}", async (string token, HttpContext context, AutoPauseTestStreamGate gate) => { context.Response.ContentType = "application/octet-stream"; context.Response.Headers["Cache-Control"] = "no-store"; context.Response.Headers["Content-Disposition"] = $"attachment; filename=\"autopause-{token}.bin\""; // Emit an initial chunk so the client observes the download has started. var prefix = new byte[1024]; new Random(42).NextBytes(prefix); await context.Response.Body.WriteAsync(prefix, context.RequestAborted); await context.Response.Body.FlushAsync(context.RequestAborted); gate.MarkStarted(token); try { // Block here until the test releases the gate (or aborts). using var timeout = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted); timeout.CancelAfter(TimeSpan.FromSeconds(30)); await gate.WaitAsync(token, timeout.Token); } catch (OperationCanceledException) { gate.MarkCompleted(token); return; } // Emit a small trailer so the client observes a clean EOF. var trailer = new byte[64]; await context.Response.Body.WriteAsync(trailer, context.RequestAborted); gate.MarkCompleted(token); }); endpoints.MapPost("/autopause-test/release/{token}", (string token, AutoPauseTestStreamGate gate) => { var released = gate.Release(token); return Results.Ok(new { released }); }); endpoints.MapGet("/autopause-test/started/{token}", (string token, AutoPauseTestStreamGate gate) => Results.Json(new { started = gate.IsStarted(token) })); endpoints.MapGet("/autopause-test/completed/{token}", (string token, AutoPauseTestStreamGate gate) => Results.Json(new { completed = gate.IsCompleted(token) })); endpoints.MapPost("/autopause-test/upload/{token}", async (string token, HttpContext context, AutoPauseTestStreamGate gate) => { var buffer = new byte[4096]; var totalRead = 0; var firstRead = await context.Request.Body.ReadAsync(buffer, context.RequestAborted); if (firstRead == 0) { return Results.BadRequest(new { error = "empty body" }); } totalRead += firstRead; gate.MarkStarted(token); try { using var timeout = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted); timeout.CancelAfter(TimeSpan.FromSeconds(30)); await gate.WaitAsync(token, timeout.Token); } catch (OperationCanceledException) { gate.MarkCompleted(token); return Results.Ok(new { totalRead, aborted = true }); } int bytesRead; while ((bytesRead = await context.Request.Body.ReadAsync(buffer, context.RequestAborted)) > 0) { totalRead += bytesRead; } gate.MarkCompleted(token); return Results.Ok(new { totalRead }); }); endpoints.MapGet("/autopause-test/js-gate/{token}", async (string token, HttpContext context, AutoPauseTestStreamGate gate) => { gate.MarkStarted(token); try { using var timeout = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted); timeout.CancelAfter(TimeSpan.FromSeconds(30)); await gate.WaitAsync(token, timeout.Token); } catch (OperationCanceledException) { gate.MarkCompleted(token); return Results.Ok(new { aborted = true }); } gate.MarkCompleted(token); return Results.Ok(new { released = true }); }); endpoints.MapGet("/autopause-test/ws-gate/{token}", async (string token, HttpContext context, AutoPauseTestStreamGate gate) => { if (!context.WebSockets.IsWebSocketRequest) { context.Response.StatusCode = 400; return; } using var ws = await context.WebSockets.AcceptWebSocketAsync(); gate.MarkStarted(token); try { using var timeout = CancellationTokenSource.CreateLinkedTokenSource(context.RequestAborted); timeout.CancelAfter(TimeSpan.FromSeconds(30)); await gate.WaitAsync(token, timeout.Token); } catch (OperationCanceledException) { gate.MarkCompleted(token); await ws.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "timeout", default); return; } var payload = System.Text.Encoding.UTF8.GetBytes("delivered"); await ws.SendAsync(payload, System.Net.WebSockets.WebSocketMessageType.Text, true, default); gate.MarkCompleted(token); await ws.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "done", default); }); } }