-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathopenai_errors_test.go
More file actions
70 lines (64 loc) · 1.93 KB
/
Copy pathopenai_errors_test.go
File metadata and controls
70 lines (64 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package intercept_test
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/aibridge/intercept"
"github.com/coder/coder/v2/aibridge/keypool"
)
func TestResponseErrorFromKeyPool(t *testing.T) {
t.Parallel()
tests := []struct {
name string
keyPoolErr *keypool.Error
expectedStatus int
expectedRetryAfter time.Duration
}{
{
name: "nil_returns_nil",
keyPoolErr: nil,
},
{
// Rate-limited with no cooldown: 429, no Retry-After.
name: "rate_limited_zero_retry_after",
keyPoolErr: &keypool.Error{Kind: keypool.ErrorKindRateLimited},
expectedStatus: http.StatusTooManyRequests,
expectedRetryAfter: 0,
},
{
// Rate-limited with cooldown: 429, Retry-After set.
name: "rate_limited_with_retry_after",
keyPoolErr: &keypool.Error{Kind: keypool.ErrorKindRateLimited, RetryAfter: 5 * time.Second},
expectedStatus: http.StatusTooManyRequests,
expectedRetryAfter: 5 * time.Second,
},
{
// Permanent: 502 api_error.
name: "permanent_returns_502",
keyPoolErr: &keypool.Error{Kind: keypool.ErrorKindPermanent},
expectedStatus: http.StatusBadGateway,
},
{
// Auth-failure exhaustion: 502, no Retry-After.
name: "unauthorized_returns_502_without_retry_after",
keyPoolErr: &keypool.Error{Kind: keypool.ErrorKindUnauthorized, RetryAfter: 60 * time.Second},
expectedStatus: http.StatusBadGateway,
expectedRetryAfter: 0,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
got := intercept.ResponseErrorFromKeyPool(tc.keyPoolErr)
if tc.keyPoolErr == nil {
assert.Nil(t, got)
return
}
require.NotNil(t, got)
assert.Equal(t, tc.expectedStatus, got.StatusCode)
assert.Equal(t, tc.expectedRetryAfter, got.RetryAfter)
})
}
}