From c022cf96ed97132040003f2ca2214e9a3df4dc76 Mon Sep 17 00:00:00 2001 From: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com> Date: Wed, 20 Nov 2024 22:01:26 +0000 Subject: [PATCH] .Net: Add unit test to confirm 429 exception handling (#9772) ### Motivation and Context Issue #9666 ### Description ### Contribution Checklist - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone :smile: --- .../KernelFunctionFromPromptTests.cs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs b/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs index ff45e19c62d7..72dc5199dafb 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs @@ -3,6 +3,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Http; using System.Runtime.CompilerServices; using System.Text.Json; using System.Threading; @@ -843,6 +845,27 @@ public async Task InvokePromptAsyncWithChatCompletionReturnsMultipleResultsAsync } } + [Fact] + public async Task InvokePromptAsyncWithChatCompletionPropagatesTooManyRequestsAsync() + { + // Arrange + using var messageHandlerStub = new HttpMessageHandlerStub(); + using var response = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests); + messageHandlerStub.ResponseToReturn = response; + using var httpClient = new HttpClient(messageHandlerStub, false); + var chatCompletion = new OpenAIChatCompletionService(modelId: "any", apiKey: "any", httpClient: httpClient); + + KernelBuilder builder = new(); + builder.Services.AddTransient((sp) => chatCompletion); + Kernel kernel = builder.Build(); + + // Act + var exception = await Assert.ThrowsAsync(async () => await kernel.InvokePromptAsync("Prompt")); + + // Assert + Assert.Equal(HttpStatusCode.TooManyRequests, exception.StatusCode); + } + [Fact] public async Task InvokePromptAsyncWithPromptFunctionInTemplateAndSingleResultAsync() {