Vectrum
Sign In
35 lines · 822 Bytes · Shell
#!/usr/bin/env bash
set -euo pipefail
export GOPROXY=off GOFLAGS=-mod=mod
git checkout HEAD -- internal/ratelimit/bucket_test.go
go test ./... 2>&1
cat > internal/ratelimit/zz_verify_test.go <<'GO'
package ratelimit

import (
	"testing"
	"time"
)

// After a long idle period the bucket must refill to capacity and no further:
// the bug was that b.last was never advanced, so every call re-credited the
// entire elapsed time since construction.
func TestIdleDoesNotGrantUnlimitedTokens(t *testing.T) {
	now := time.Now()
	b := New(3, 1)
	b.now = func() time.Time { return now }
	b.last = now

	now = now.Add(time.Hour)
	allowed := 0
	for i := 0; i < 50; i++ {
		if b.Allow() {
			allowed++
		}
	}
	if allowed != 3 {
		t.Fatalf("after idling, %d requests were allowed; capacity is 3", allowed)
	}
}
GO
go test ./... 2>&1