-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d242ed6
commit a975433
Showing
5 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
test/E2ETests/E2EApps/E2EApp/Http/CancellationHttpFunctions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System; | ||
|
||
namespace Microsoft.Azure.Functions.Worker.E2EApp | ||
{ | ||
public class CancellationHttpFunctions(ILogger<CancellationHttpFunctions> logger) | ||
{ | ||
private readonly ILogger<CancellationHttpFunctions> _logger = logger; | ||
|
||
[Function(nameof(HttpWithCancellationTokenNotUsed))] | ||
public async Task<IActionResult> HttpWithCancellationTokenNotUsed( | ||
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req) | ||
{ | ||
_logger.LogInformation("HttpWithCancellationTokenNotUsed processed a request."); | ||
|
||
await SimulateWork(CancellationToken.None); | ||
|
||
return new OkObjectResult("Processing completed successfully."); | ||
} | ||
|
||
[Function(nameof(HttpWithCancellationTokenIgnored))] | ||
public async Task<IActionResult> HttpWithCancellationTokenIgnored( | ||
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req, | ||
CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("HttpWithCancellationTokenIgnored processed a request."); | ||
|
||
await SimulateWork(cancellationToken); | ||
|
||
return new OkObjectResult("Processing completed successfully."); | ||
} | ||
|
||
[Function(nameof(HttpWithCancellationTokenHandled))] | ||
public async Task<IActionResult> HttpWithCancellationTokenHandled( | ||
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, | ||
CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("HttpWithCancellationTokenHandled processed a request."); | ||
|
||
try | ||
{ | ||
await SimulateWork(cancellationToken); | ||
|
||
return new OkObjectResult("Processing completed successfully."); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
_logger.LogWarning("Request was cancelled."); | ||
|
||
// Take precautions like noting how far along you are with processing the batch | ||
await Task.Delay(1000); | ||
|
||
return new ObjectResult(new { statusCode = StatusCodes.Status499ClientClosedRequest, message = "Request was cancelled." }); | ||
} | ||
} | ||
|
||
private async Task SimulateWork(CancellationToken cancellationToken) | ||
{ | ||
_logger.LogInformation("Starting work..."); | ||
|
||
for (int i = 0; i < 5; i++) | ||
{ | ||
// Simulate work | ||
await Task.Delay(1000, cancellationToken); | ||
_logger.LogWarning($"Work iteration {i + 1} completed."); | ||
} | ||
|
||
_logger.LogInformation("Work completed."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
test/E2ETests/E2ETests/Helpers/CancellationEndToEndTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Azure.Functions.Tests.E2ETests | ||
{ | ||
[Collection(Constants.FunctionAppCollectionName)] | ||
public class CancellationEndToEndTests | ||
{ | ||
private readonly FunctionAppFixture _fixture; | ||
|
||
public CancellationEndToEndTests(FunctionAppFixture fixture, ITestOutputHelper testOutputHelper) | ||
{ | ||
_fixture = fixture; | ||
_fixture.TestLogs.UseTestLogger(testOutputHelper); | ||
} | ||
|
||
[Theory] | ||
[InlineData("HttpWithCancellationTokenNotUsed", "Work completed.", "Succeeded")] | ||
[InlineData("HttpWithCancellationTokenIgnored", "TaskCanceledException: A task was canceled", "Failed")] | ||
[InlineData("HttpWithCancellationTokenHandled", "Request was cancelled.", "Succeeded")] | ||
public async Task Functions_WithCancellationToken_BehaveAsExpected(string functionName, string expectedMessage, string invocationResult) | ||
{ | ||
using var cts = new CancellationTokenSource(); | ||
|
||
try | ||
{ | ||
var task = HttpHelpers.InvokeHttpTrigger(functionName, cancellationToken: cts.Token); | ||
|
||
await Task.Delay(3000); | ||
cts.Cancel(); | ||
|
||
var response = await task; | ||
} | ||
catch (Exception) | ||
{ | ||
IEnumerable<string> logs = null; | ||
await TestUtility.RetryAsync(() => | ||
{ | ||
logs = _fixture.TestLogs.CoreToolsLogs.Where(p => p.Contains($"Executed 'Functions.{functionName}' ({invocationResult}")); | ||
|
||
return Task.FromResult(logs.Count() >= 1); | ||
}); | ||
|
||
Assert.Contains(_fixture.TestLogs.CoreToolsLogs, log => log.Contains(expectedMessage, StringComparison.OrdinalIgnoreCase)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters