traffic-service-review.go.txt 1.7 KB

12345678910111213141516171819202122232425262728293031
  1. package service
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. coderws "github.com/coder/websocket"
  7. )
  8. func TestLatestReviewWSFailedDoneCounts429(t *testing.T) {
  9. cache := &trafficCacheStub{acquired:true}
  10. inner := &trafficFrameStub{payload:[]byte(`{"type":"response.done","response":{"status":"failed","error":{"code":"rate_limit_exceeded"}}}`)}
  11. policy := DefaultAccountTrafficPolicy(); policy.AdaptiveEnabled=true; policy.AdaptiveMode="automatic"
  12. frame:=&accountTrafficFrameConn{inner:inner,control:NewAccountTrafficService(cache),plan:AccountTrafficPlan{AccountID:42,Policy:policy},ctx:context.Background()}
  13. require.NoError(t,frame.WriteFrame(context.Background(),coderws.MessageText,[]byte(`{"type":"response.create"}`)))
  14. _,_,err:=frame.ReadFrame(context.Background());require.NoError(t,err)
  15. require.Equal(t,429,cache.status,"failed response.done contains a real rate limit; adaptation must see it")
  16. }
  17. func TestLatestReviewDisabledPoliciesIgnoreInactiveValues(t *testing.T) {
  18. _,err:=ParseAccountTrafficPolicy(map[string]any{AccountTrafficPolicyKey:map[string]any{"strict_rpm_enabled":false,"rpm":0,"adaptive_enabled":false}})
  19. require.NoError(t,err,"disabling the limiter must not require repairing an inactive hidden RPM field")
  20. }
  21. func TestLatestReviewObserveDoesNotDenyStaleAdmission(t *testing.T) {
  22. cache:=&trafficCacheStub{acquired:false}
  23. p:=DefaultAccountTrafficPolicy();p.AdaptiveEnabled=true
  24. _,permit,err:=NewAccountTrafficService(cache).Begin(context.Background(),AccountTrafficPlan{AccountID:42,Policy:p})
  25. if permit!=nil { permit.Finish(0) }
  26. require.NoError(t,err,"recommendation-only mode must not block requests when the cached plan changes")
  27. }