67 lines
2.5 KiB
Go
67 lines
2.5 KiB
Go
package summaryqa
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
type quota55Repo struct {
|
|
fixtureRepo
|
|
admissionErr error
|
|
admissions, cancellations, releases int
|
|
}
|
|
type quota55Permit struct{ r *quota55Repo }
|
|
|
|
func (p quota55Permit) Release() { p.r.releases++ }
|
|
func (r *quota55Repo) AcquireQAProviderCall(context.Context, string, string, string) (ProviderPermit, error) {
|
|
r.admissions++
|
|
if r.admissionErr != nil {
|
|
return nil, r.admissionErr
|
|
}
|
|
return quota55Permit{r}, nil
|
|
}
|
|
func (r *quota55Repo) CancelQAQuestion(context.Context, string, string, string) error {
|
|
r.cancellations++
|
|
return nil
|
|
}
|
|
|
|
type quota55PanicProvider struct{}
|
|
|
|
func (quota55PanicProvider) Capabilities() Capabilities { return Capabilities{} }
|
|
func (quota55PanicProvider) Answer(context.Context, StatelessRequest) (AnswerResult, error) {
|
|
panic("PRIVATE_PROVIDER_PAYLOAD")
|
|
}
|
|
func TestTicket55DeniedAdmissionIsRetryableWithoutFinishingOrCallingProvider(t *testing.T) {
|
|
r := "a55Repo{fixtureRepo: fixtureRepo{work: Work{Turn: Turn{ID: "turn"}}}, admissionErr: &RateLimitError{RetryAfterSeconds: 60}}
|
|
p := &fixtureProvider{answer: "answer"}
|
|
s := New(r, &fixtureResolver{p: p})
|
|
_, err := s.Ask(context.Background(), "owner", "summary", Question{RequestID: "00000000-0000-0000-0000-000000005501", Passage: "p", Question: "q"})
|
|
if !errors.Is(err, ErrRateLimited) || r.admissions != 1 || r.cancellations != 1 || r.finishes != 0 || len(p.requests) != 0 {
|
|
t.Fatal("quota denial became a provider call or permanent turn", err)
|
|
}
|
|
r.work.Replay = true
|
|
r.work.Turn = Turn{Answer: "stored"}
|
|
_, err = s.Ask(context.Background(), "owner", "summary", Question{RequestID: "00000000-0000-0000-0000-000000005501", Passage: "p", Question: "q"})
|
|
if err != nil || r.admissions != 1 {
|
|
t.Fatal("idempotent replay reached quota admission", err)
|
|
}
|
|
}
|
|
func TestTicket55PermitReleasedAndProviderPanicSanitized(t *testing.T) {
|
|
r := "a55Repo{fixtureRepo: fixtureRepo{work: Work{Turn: Turn{ID: "turn"}}}}
|
|
s := New(r, &fixtureResolver{p: quota55PanicProvider{}})
|
|
var err error
|
|
panicked := false
|
|
func() {
|
|
defer func() {
|
|
if recover() != nil {
|
|
panicked = true
|
|
}
|
|
}()
|
|
_, err = s.Ask(context.Background(), "owner", "summary", Question{RequestID: "00000000-0000-0000-0000-000000005501", Passage: "p", Question: "q"})
|
|
}()
|
|
if panicked || !errors.Is(err, ErrProvider) || r.releases != 1 || r.finishes != 1 || r.code != "qa_provider_failed" {
|
|
t.Fatalf("provider panic escaped or leaked a permit: panic=%v released=%d finished=%d error=%v", panicked, r.releases, r.finishes, err)
|
|
}
|
|
}
|