34 lines
989 B
Go
34 lines
989 B
Go
package recharge
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRequestGateHonorsCancellation(t *testing.T) {
|
|
gate := &requestGate{}
|
|
if err := gate.wait(context.Background()); err != nil {
|
|
t.Fatalf("first wait failed: %v", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
|
defer cancel()
|
|
if err := gate.wait(ctx); !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("wait error = %v, want deadline exceeded", err)
|
|
}
|
|
}
|
|
|
|
func TestClientSharesGatePerAccountAndEndpoint(t *testing.T) {
|
|
first := NewClient(Config{BaseURL: "http://example.com", PwdKey: "account"})
|
|
second := NewClient(Config{BaseURL: "http://example.com", PwdKey: "account"})
|
|
|
|
if first.requestGate("/findCard") != second.requestGate("/findCard") {
|
|
t.Fatal("same account and endpoint must share the rate limiter")
|
|
}
|
|
if first.requestGate("/findCard") == first.requestGate("/changeState") {
|
|
t.Fatal("different endpoints must not share the rate limiter")
|
|
}
|
|
}
|