secert updated
This commit is contained in:
@@ -3,6 +3,7 @@ package utils
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -73,16 +74,70 @@ func TestGeminiEmbedderSendsTheRequestTheAPIExpects(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEmbedderSurfacesProviderErrors(t *testing.T) {
|
||||
// Used to assert this with a 429, which is now the one status that is
|
||||
// deliberately NOT passed through — see the two tests below. The rule it was
|
||||
// written for still holds for every other failure: a provider that explains
|
||||
// itself should not have that explanation swallowed.
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(429)
|
||||
w.Write([]byte(`{"error":{"message":"Rate limit reached"}}`))
|
||||
w.WriteHeader(400)
|
||||
w.Write([]byte(`{"error":{"message":"input exceeds the maximum token count"}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
e, _ := NewEmbedder(config.EmbeddingConfig{Provider: "openai", Model: "m", APIKey: "k", BaseURL: srv.URL})
|
||||
_, err := e.Embed(context.Background(), "x")
|
||||
if err == nil || !strings.Contains(err.Error(), "429") || !strings.Contains(err.Error(), "Rate limit") {
|
||||
t.Fatalf("want a 429 with the provider's message, got %v", err)
|
||||
if err == nil || !strings.Contains(err.Error(), "400") || !strings.Contains(err.Error(), "maximum token count") {
|
||||
t.Fatalf("want a 400 with the provider's message, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestARateLimitIsWaitedOutRatherThanReported(t *testing.T) {
|
||||
// The common case on a free tier: the quota clears in under a second, so
|
||||
// the right answer is a slightly slower success rather than an error.
|
||||
var calls int
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
if calls == 1 {
|
||||
w.WriteHeader(429)
|
||||
w.Write([]byte(`{"error":{"message":"Rate limit reached. Please try again in 20ms."}}`))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{"data":[{"embedding":[0.5,0.25]}]}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
e, _ := NewEmbedder(config.EmbeddingConfig{Provider: "openai", Model: "m", APIKey: "k", BaseURL: srv.URL})
|
||||
vector, err := e.Embed(context.Background(), "x")
|
||||
if err != nil {
|
||||
t.Fatalf("a 20ms quota wait became an error: %v", err)
|
||||
}
|
||||
if len(vector) != 2 {
|
||||
t.Fatalf("the retry did not return the answer: %v", vector)
|
||||
}
|
||||
if calls != 2 {
|
||||
t.Fatalf("expected one retry, saw %d calls", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPersistentRateLimitIsReportedWithoutTheProvidersDetails(t *testing.T) {
|
||||
// When waiting does not help, the person is told to try again — not handed
|
||||
// our organisation id and the tokens-per-minute arithmetic behind it.
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(429)
|
||||
w.Write([]byte(`{"error":{"message":"Rate limit reached for model X in organization org_01m38 on tokens per minute (TPM): Limit 8000. Please try again in 20ms."}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
e, _ := NewEmbedder(config.EmbeddingConfig{Provider: "openai", Model: "m", APIKey: "k", BaseURL: srv.URL})
|
||||
_, err := e.Embed(context.Background(), "x")
|
||||
|
||||
if !errors.Is(err, ErrBusy) {
|
||||
t.Fatalf("a persistent quota was not reported as busy: %v", err)
|
||||
}
|
||||
for _, leaked := range []string{"org_01m38", "TPM", "8000"} {
|
||||
if strings.Contains(err.Error(), leaked) {
|
||||
t.Fatalf("%q reached the caller: %s", leaked, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user