149 lines
3.4 KiB
Go
149 lines
3.4 KiB
Go
package admission
|
|
|
|
import (
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
)
|
|
|
|
// TestLimiter_Allow checks token exhaustion and refill with deterministic time.
|
|
func TestLimiter_Allow(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
elapsed time.Duration
|
|
want bool
|
|
}{
|
|
{"Negative: exhausted bucket", 0, false},
|
|
{"Negative: partial token", time.Millisecond * 500, false},
|
|
{"Positive: refilled token", time.Second, true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
// Arrange.
|
|
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
|
if !limiter.Allow(time.Now()) {
|
|
t.Fatal("fixture token missing")
|
|
}
|
|
time.Sleep(tc.elapsed)
|
|
|
|
// Act.
|
|
got := limiter.Allow(time.Now())
|
|
|
|
// Assert.
|
|
if got != tc.want {
|
|
t.Fatalf("Allow after %s=%v, want=%v", tc.elapsed, got, tc.want)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestLimiter_Acquire checks available, occupied and released execution slots independently.
|
|
func TestLimiter_Acquire(t *testing.T) {
|
|
t.Run("Positive: free slot", func(t *testing.T) {
|
|
// Arrange.
|
|
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
|
|
|
want := true
|
|
|
|
// Act.
|
|
got := limiter.Acquire()
|
|
if got {
|
|
t.Cleanup(limiter.Release)
|
|
}
|
|
|
|
// Assert.
|
|
if got != want {
|
|
t.Fatalf("Acquire=%v, want=%v", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("Negative: occupied slot", func(t *testing.T) {
|
|
// Arrange.
|
|
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
|
|
|
if !limiter.Acquire() {
|
|
t.Fatal("fixture slot unavailable")
|
|
}
|
|
|
|
t.Cleanup(limiter.Release)
|
|
|
|
want := false
|
|
|
|
// Act.
|
|
got := limiter.Acquire()
|
|
if got {
|
|
t.Cleanup(limiter.Release)
|
|
}
|
|
|
|
// Assert.
|
|
if got != want {
|
|
t.Fatalf("Acquire=%v, want=%v", got, want)
|
|
}
|
|
})
|
|
|
|
t.Run("Positive: released slot", func(t *testing.T) {
|
|
// Arrange.
|
|
limiter := newTestLimiter(t, Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 1})
|
|
|
|
if !limiter.Acquire() {
|
|
t.Fatal("fixture slot unavailable")
|
|
}
|
|
|
|
limiter.Release()
|
|
|
|
want := true
|
|
|
|
// Act.
|
|
got := limiter.Acquire()
|
|
if got {
|
|
t.Cleanup(limiter.Release)
|
|
}
|
|
|
|
// Assert.
|
|
if got != want {
|
|
t.Fatalf("Acquire=%v, want=%v", got, want)
|
|
}
|
|
})
|
|
}
|
|
|
|
// newTestLimiter creates a limiter or fails the test on invalid settings.
|
|
func newTestLimiter(t *testing.T, options Options) *Limiter {
|
|
t.Helper()
|
|
limiter, err := New(options)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return limiter
|
|
}
|
|
|
|
// TestLimiter_New rejects each nonpositive limit independently.
|
|
func TestLimiter_New(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
options Options
|
|
}{
|
|
{"Negative: zero concurrency", Options{MaxInFlight: 0, RequestsPerSecond: 1, Burst: 1}},
|
|
{"Negative: negative concurrency", Options{MaxInFlight: -1, RequestsPerSecond: 1, Burst: 1}},
|
|
{"Negative: zero refill rate", Options{MaxInFlight: 1, RequestsPerSecond: 0, Burst: 1}},
|
|
{"Negative: negative refill rate", Options{MaxInFlight: 1, RequestsPerSecond: -1, Burst: 1}},
|
|
{"Negative: zero burst", Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: 0}},
|
|
{"Negative: negative burst", Options{MaxInFlight: 1, RequestsPerSecond: 1, Burst: -1}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
// Arrange.
|
|
options := tc.options
|
|
|
|
// Act.
|
|
limiter, err := New(options)
|
|
|
|
// Assert.
|
|
if err == nil || limiter != nil {
|
|
t.Fatalf("invalid options accepted: %v, %v", limiter, err)
|
|
}
|
|
})
|
|
}
|
|
}
|