intelligent_test_admission_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "fmt"
  6. "github.com/stretchr/testify/require"
  7. "net/http"
  8. "testing"
  9. "time"
  10. )
  11. func TestIntelligentAdmissionSharesCapacityAndReleases(t *testing.T) {
  12. calls := 0
  13. svc, _ := intelligentRunnerFixture(t, func(w http.ResponseWriter, r *http.Request) {
  14. calls++
  15. fmt.Fprint(w, "data: {\"type\":\"response.output_text.delta\",\"delta\":\"12\"}\n\ndata: {\"type\":\"response.completed\"}\n\n")
  16. })
  17. cache := &stubConcurrencyCacheForTest{acquireResult: false}
  18. svc.concurrencyService = NewConcurrencyService(cache)
  19. record := &IntelligentTestRecord{AccountID: 81, Input: "test", ConfigSnapshot: &IntelligentTestConfig{Model: "test"}}
  20. require.ErrorIs(t, svc.RunIntelligentTest(context.Background(), record), ErrIntelligentAccountBusy)
  21. require.Zero(t, calls)
  22. require.Empty(t, cache.releasedAccountIDs)
  23. cache.acquireResult = true
  24. require.NoError(t, svc.RunIntelligentTest(context.Background(), record))
  25. require.Equal(t, 1, calls)
  26. require.Equal(t, []int64{81}, cache.releasedAccountIDs)
  27. }
  28. func TestIntelligentAdmissionDefersAccountAndSelectedModelCooldown(t *testing.T) {
  29. calls := 0
  30. svc, repo := intelligentRunnerFixture(t, func(w http.ResponseWriter, r *http.Request) {
  31. calls++
  32. fmt.Fprint(w, "data: {\"type\":\"response.output_text.delta\",\"delta\":\"12\"}\n\ndata: {\"type\":\"response.completed\"}\n\n")
  33. })
  34. until := time.Now().Add(90 * time.Second)
  35. repo.account.RateLimitResetAt = &until
  36. r := &IntelligentTestRecord{AccountID: 81, Input: "test", ConfigSnapshot: &IntelligentTestConfig{Model: "test-model", Evaluator: "exact_answer", ExpectedAnswer: "12", TimeoutSeconds: 30}}
  37. worker := NewIntelligentTestService(nil, svc)
  38. worker.run(context.Background(), r)
  39. require.Equal(t, "queued", r.Status)
  40. require.NotNil(t, r.AvailableAt)
  41. require.WithinDuration(t, until, *r.AvailableAt, time.Second)
  42. require.Contains(t, r.QueueReason, "冷却")
  43. require.Zero(t, calls)
  44. repo.account.RateLimitResetAt = nil
  45. setAccountModelRateLimitSnapshot(repo.account, "test-model", until, "model cooldown", time.Now())
  46. require.ErrorIs(t, svc.RunIntelligentTest(context.Background(), r), ErrIntelligentAccountBusy)
  47. require.Zero(t, calls)
  48. r.ConfigSnapshot.Model = "other-model"
  49. require.NoError(t, svc.RunIntelligentTest(context.Background(), r))
  50. require.Equal(t, 1, calls)
  51. require.Zero(t, repo.writes)
  52. }