Skip to content

Commit

Permalink
Testing: restore coverage and make tests run in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jun 26, 2024
1 parent 5dc87d1 commit 5d41662
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ratelimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func peekCheckLimited(t *testing.T, limiter *Limiter, shouldbe, stringer bool) {

// this test exists here for coverage, we are simulating the debug channel overflowing and then invoking println().
func Test_debugPrintf(t *testing.T) {
t.Parallel()
limiter := NewLimiter(1, 1)
_ = limiter.DebugChannel()
for n := 0; n < 50; n++ {
Expand Down Expand Up @@ -127,6 +128,7 @@ func Test_ResetItem(t *testing.T) {
}

func Test_NewDefaultLimiter(t *testing.T) {
t.Parallel()
limiter := NewDefaultLimiter()
limiter.Check(dummyTicker)
peekCheckLimited(t, limiter, false, false)
Expand All @@ -137,6 +139,7 @@ func Test_NewDefaultLimiter(t *testing.T) {
}

func Test_CheckAndPeekStringer(t *testing.T) {
t.Parallel()
limiter := NewDefaultLimiter()
limiter.CheckStringer(dummyTicker)
peekCheckLimited(t, limiter, false, true)
Expand All @@ -147,6 +150,7 @@ func Test_CheckAndPeekStringer(t *testing.T) {
}

func Test_NewLimiter(t *testing.T) {
t.Parallel()
limiter := NewLimiter(5, 1)
limiter.Check(dummyTicker)
peekCheckLimited(t, limiter, false, false)
Expand All @@ -155,6 +159,7 @@ func Test_NewLimiter(t *testing.T) {
}

func Test_NewDefaultStrictLimiter(t *testing.T) {
t.Parallel()
limiter := NewDefaultStrictLimiter()
// ctx, cancel := context.WithCancel(context.Background())
// go watchDebug(ctx, limiter, t)
Expand All @@ -170,6 +175,7 @@ func Test_NewDefaultStrictLimiter(t *testing.T) {
}

func Test_NewStrictLimiter(t *testing.T) {
t.Parallel()
limiter := NewStrictLimiter(5, 1)
// ctx, cancel := context.WithCancel(context.Background())
// go watchDebug(ctx, limiter, t)
Expand All @@ -196,6 +202,7 @@ func Test_NewStrictLimiter(t *testing.T) {
}

func Test_NewHardcoreLimiter(t *testing.T) {
t.Parallel()
limiter := NewHardcoreLimiter(1, 5)
ctx, cancel := context.WithCancel(context.Background())
go watchDebug(ctx, limiter, t)
Expand Down Expand Up @@ -324,16 +331,19 @@ testloop:
}

func Test_ConcurrentShouldNotLimit(t *testing.T) {
t.Parallel()
concurrentTest(t, 50, 20, 20, false)
concurrentTest(t, 50, 50, 50, false)
}

func Test_ConcurrentShouldLimit(t *testing.T) {
t.Parallel()
concurrentTest(t, 50, 21, 20, true)
concurrentTest(t, 50, 51, 50, true)
}

func Test_debugChannelOverflow(t *testing.T) {
t.Parallel()
limiter := NewDefaultLimiter()
_ = limiter.DebugChannel()
for n := 0; n != 78; n++ {
Expand All @@ -348,6 +358,19 @@ func Test_debugChannelOverflow(t *testing.T) {
}
}

func TestDebugPrintfTypeAssertion(t *testing.T) {
t.Parallel()
limiter := NewDefaultLimiter()
limiter.SetDebug(true)
asdf := new(atomic.Int64)
asdf.Store(5)
limiter.debugChannel = make(chan string, 1)
limiter.debugPrintf("test %d %d", 1, asdf)
if <-limiter.debugChannel != "test 1 5" {
t.Fatalf("failed to type assert atomic.Int64")
}
}

func BenchmarkCheck(b *testing.B) {
b.StopTimer()
limiter := NewDefaultLimiter()
Expand Down

0 comments on commit 5d41662

Please sign in to comment.