|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Reflection; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.Azure.WebJobs.Script.Description; |
| 11 | +using Microsoft.Azure.WebJobs.Script.Http; |
| 12 | +using Microsoft.Extensions.Logging.Abstractions; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace Microsoft.Azure.WebJobs.Script.Tests.Http |
| 16 | +{ |
| 17 | + public class RetryProxyHandlerTests |
| 18 | + { |
| 19 | + [Fact] |
| 20 | + public async Task SendAsync_RetriesToMax() |
| 21 | + { |
| 22 | + var inner = new TestHandler(); |
| 23 | + var handler = new RetryProxyHandler(inner, NullLogger.Instance); |
| 24 | + var request = new HttpRequestMessage(); |
| 25 | + |
| 26 | + var response = typeof(RetryProxyHandler)! |
| 27 | + .GetMethod("SendAsync", BindingFlags.NonPublic | BindingFlags.Instance)! |
| 28 | + .Invoke(handler, new object[] { request, CancellationToken.None }) |
| 29 | + as Task<HttpResponseMessage>; |
| 30 | + |
| 31 | + var result = await response.ContinueWith(t => t); |
| 32 | + |
| 33 | + Assert.True(result.IsFaulted); |
| 34 | + Assert.True(result.Exception.InnerException is HttpRequestException); |
| 35 | + Assert.Equal(RetryProxyHandler.MaxRetries, inner.Attempts); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public async Task SendAsync_StopsRetriesWhenScriptInvocationResultIsFaulted() |
| 40 | + { |
| 41 | + var inner = new TestHandler(); |
| 42 | + var handler = new RetryProxyHandler(inner, NullLogger.Instance); |
| 43 | + var request = new HttpRequestMessage(); |
| 44 | + |
| 45 | + // Create a faulted TaskCompletionSource for ScriptInvocationResult |
| 46 | + var faultedResultSource = new TaskCompletionSource<ScriptInvocationResult>(); |
| 47 | + var invocationException = new InvalidOperationException("Function invocation failed"); |
| 48 | + faultedResultSource.SetException(invocationException); |
| 49 | + |
| 50 | + // Create ScriptInvocationContext with faulted result source |
| 51 | + var scriptInvocationContext = new ScriptInvocationContext |
| 52 | + { |
| 53 | + ExecutionContext = new ExecutionContext |
| 54 | + { |
| 55 | + InvocationId = Guid.NewGuid() |
| 56 | + }, |
| 57 | + ResultSource = faultedResultSource |
| 58 | + }; |
| 59 | + |
| 60 | + // Add the context to the request options |
| 61 | + request.Options.TryAdd(ScriptConstants.HttpProxyScriptInvocationContext, scriptInvocationContext); |
| 62 | + |
| 63 | + var response = typeof(RetryProxyHandler)! |
| 64 | + .GetMethod("SendAsync", BindingFlags.NonPublic | BindingFlags.Instance)! |
| 65 | + .Invoke(handler, new object[] { request, CancellationToken.None }) |
| 66 | + as Task<HttpResponseMessage>; |
| 67 | + |
| 68 | + var result = await response.ContinueWith(t => t); |
| 69 | + |
| 70 | + // Verify that the task is faulted due to the ScriptInvocationResult being faulted |
| 71 | + Assert.True(result.IsFaulted); |
| 72 | + |
| 73 | + // Verify that no retries were attempted since the result source was already faulted |
| 74 | + Assert.Equal(0, inner.Attempts); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task SendAsync_TaskCanceledException_ThrowsOperationCanceledException_WhenCancellationRequested() |
| 79 | + { |
| 80 | + var cts = new CancellationTokenSource(); |
| 81 | + cts.Cancel(); |
| 82 | + |
| 83 | + var inner = new TaskCanceledTestHandler(); |
| 84 | + var handler = new RetryProxyHandler(inner, NullLogger.Instance); |
| 85 | + var request = new HttpRequestMessage(); |
| 86 | + |
| 87 | + var sendAsync = typeof(RetryProxyHandler)! |
| 88 | + .GetMethod("SendAsync", BindingFlags.NonPublic | BindingFlags.Instance)!; |
| 89 | + |
| 90 | + var task = (Task<HttpResponseMessage>)sendAsync.Invoke(handler, new object[] { request, cts.Token }); |
| 91 | + |
| 92 | + var ex = await Assert.ThrowsAsync<OperationCanceledException>(() => task); |
| 93 | + Assert.True(cts.Token.IsCancellationRequested); |
| 94 | + Assert.Equal(cts.Token, ex.CancellationToken); |
| 95 | + } |
| 96 | + |
| 97 | + private class TaskCanceledTestHandler : HttpMessageHandler |
| 98 | + { |
| 99 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 100 | + { |
| 101 | + throw new TaskCanceledException(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private class TestHandler : HttpMessageHandler |
| 106 | + { |
| 107 | + public int Attempts { get; set; } |
| 108 | + |
| 109 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, |
| 110 | + CancellationToken cancellationToken) |
| 111 | + { |
| 112 | + Attempts++; |
| 113 | + |
| 114 | + throw new HttpRequestException(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments