73 рядки
1.7 KiB
Go
73 рядки
1.7 KiB
Go
package telegram
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
ta "github.com/mymmrac/telego/telegoapi"
|
|
)
|
|
|
|
func TestNewCallerHandlesRateLimits(t *testing.T) {
|
|
caller := newCaller()
|
|
|
|
if caller.RateLimit != ta.RetryRateLimitWaitOrAbort {
|
|
t.Fatalf("RateLimit = %v, want RetryRateLimitWaitOrAbort", caller.RateLimit)
|
|
}
|
|
if caller.MaxAttempts < 2 {
|
|
t.Fatalf("MaxAttempts = %d, want at least 2", caller.MaxAttempts)
|
|
}
|
|
if caller.MaxDelay != apiMaxDelay || caller.MaxDelay <= 0 {
|
|
t.Fatalf("MaxDelay = %v, want %v", caller.MaxDelay, apiMaxDelay)
|
|
}
|
|
if caller.StartDelay <= 0 || caller.StartDelay > caller.MaxDelay {
|
|
t.Fatalf("StartDelay = %v, MaxDelay = %v", caller.StartDelay, caller.MaxDelay)
|
|
}
|
|
if caller.ExponentBase <= 1 {
|
|
t.Fatalf("ExponentBase = %v, want > 1", caller.ExponentBase)
|
|
}
|
|
if caller.Caller == nil {
|
|
t.Fatal("Caller is nil")
|
|
}
|
|
}
|
|
|
|
func TestRetryCallerWaitsForRetryAfter(t *testing.T) {
|
|
requests := 0
|
|
inner := callerFunc(func() error {
|
|
requests++
|
|
return &ta.Error{
|
|
ErrorCode: 429,
|
|
Parameters: &ta.ResponseParameters{
|
|
RetryAfter: 1,
|
|
},
|
|
}
|
|
})
|
|
caller := &ta.RetryCaller{
|
|
Caller: inner,
|
|
MaxAttempts: 2,
|
|
ExponentBase: 2,
|
|
StartDelay: time.Millisecond,
|
|
MaxDelay: time.Second,
|
|
RateLimit: ta.RetryRateLimitWaitOrAbort,
|
|
}
|
|
|
|
_, err := caller.Call(t.Context(), "https://example.invalid", &ta.RequestData{BodyRaw: []byte("{}")})
|
|
if err == nil {
|
|
t.Fatal("Call() error = nil, want retry exhaustion")
|
|
}
|
|
if requests != 2 {
|
|
t.Fatalf("Call() made %d requests, want 2", requests)
|
|
}
|
|
}
|
|
|
|
type callerFunc func() error
|
|
|
|
func (f callerFunc) Call(
|
|
_ context.Context, _ string, _ *ta.RequestData,
|
|
) (*ta.Response, error) {
|
|
if err := f(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &ta.Response{Ok: true}, nil
|
|
}
|