Skip to content

Commit

Permalink
.Net: Add unit test to confirm 429 exception handling (#9772)
Browse files Browse the repository at this point in the history
### Motivation and Context

Issue #9666 

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] 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 😄
  • Loading branch information
markwallace-microsoft authored Nov 20, 2024
1 parent 4bd9abf commit c022cf9
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IChatCompletionService>((sp) => chatCompletion);
Kernel kernel = builder.Build();

// Act
var exception = await Assert.ThrowsAsync<HttpOperationException>(async () => await kernel.InvokePromptAsync("Prompt"));

// Assert
Assert.Equal(HttpStatusCode.TooManyRequests, exception.StatusCode);
}

[Fact]
public async Task InvokePromptAsyncWithPromptFunctionInTemplateAndSingleResultAsync()
{
Expand Down

0 comments on commit c022cf9

Please sign in to comment.