Files
llm-intelligence/scripts/official_pricing_import_common_test.go

50 lines
1.2 KiB
Go
Raw Normal View History

//go:build llm_script
package main
import (
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)
func TestFetchRawPricingPageRetriesTransientStatus(t *testing.T) {
var attempts int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
current := atomic.AddInt32(&attempts, 1)
if current == 1 {
http.Error(w, "temporary", http.StatusServiceUnavailable)
return
}
_, _ = w.Write([]byte("ok"))
}))
defer server.Close()
client := &http.Client{Timeout: 2 * time.Second}
body, err := fetchRawPricingPage(server.URL, "", client)
if err != nil {
t.Fatalf("fetchRawPricingPage returned error: %v", err)
}
if body != "ok" {
t.Fatalf("body = %q, want ok", body)
}
if got := atomic.LoadInt32(&attempts); got != 2 {
t.Fatalf("attempts = %d, want 2", got)
}
}
func TestIsRetriablePricingFetchErrorRecognizesEOF(t *testing.T) {
if !isRetriablePricingFetchError(errString("unexpected EOF")) {
t.Fatalf("expected EOF to be retriable")
}
if isRetriablePricingFetchError(errString("bad request")) {
t.Fatalf("expected bad request to be non-retriable")
}
}
type errString string
func (e errString) Error() string { return string(e) }