77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package llm
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/genai"
|
|
)
|
|
|
|
func TestGenerateContentWithRetry(t *testing.T) {
|
|
mockClient := &mockModelClient{}
|
|
var slept time.Duration
|
|
service := &GeminiService{
|
|
client: mockClient,
|
|
modelName: "test-model",
|
|
sleep: func(d time.Duration) {
|
|
slept += d
|
|
},
|
|
}
|
|
|
|
attempts := 0
|
|
mockClient.GenerateContentFunc = func(ctx context.Context, model string, parts []*genai.Content, config *genai.GenerateContentConfig) (*genai.GenerateContentResponse, error) {
|
|
attempts++
|
|
if attempts <= 2 {
|
|
return nil, genai.APIError{Code: 503, Message: "Model overloaded"}
|
|
}
|
|
return &genai.GenerateContentResponse{}, nil
|
|
}
|
|
|
|
_, err := service.generateContentWithRetry(context.Background(), "test prompt", nil)
|
|
|
|
if err != nil {
|
|
t.Fatalf("Expected generateContentWithRetry to succeed, but it failed: %v", err)
|
|
}
|
|
|
|
if mockClient.callCount != 3 {
|
|
t.Errorf("Expected GenerateContent to be called 3 times, but it was called %d times", mockClient.callCount)
|
|
}
|
|
|
|
expectedSleep := 3 * time.Second // 1s + 2s
|
|
if slept != expectedSleep {
|
|
t.Errorf("Expected to sleep for %v, but slept for %v", expectedSleep, slept)
|
|
}
|
|
}
|
|
|
|
func TestGenerateContentWithRetry_NonRecoverableError(t *testing.T) {
|
|
mockClient := &mockModelClient{}
|
|
var slept time.Duration
|
|
service := &GeminiService{
|
|
client: mockClient,
|
|
modelName: "test-model",
|
|
sleep: func(d time.Duration) {
|
|
slept += d
|
|
},
|
|
}
|
|
|
|
mockClient.GenerateContentFunc = func(ctx context.Context, model string, parts []*genai.Content, config *genai.GenerateContentConfig) (*genai.GenerateContentResponse, error) {
|
|
return nil, errors.New("a non-recoverable error")
|
|
}
|
|
|
|
_, err := service.generateContentWithRetry(context.Background(), "test prompt", nil)
|
|
|
|
if err == nil {
|
|
t.Fatal("Expected generateContentWithRetry to fail, but it succeeded")
|
|
}
|
|
|
|
if mockClient.callCount != 1 {
|
|
t.Errorf("Expected GenerateContent to be called 1 time, but it was called %d times", mockClient.callCount)
|
|
}
|
|
|
|
if slept != 0 {
|
|
t.Errorf("Expected not to sleep, but slept for %v", slept)
|
|
}
|
|
}
|