Skip to content

Commit

Permalink
Added test cases for invalid ratelimiter configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Narasimha1997 committed Oct 10, 2021
1 parent faeedee commit d0157a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

<option value="file0">github.com/Narasimha1997/ratelimiter/attribute_limiter.go (93.6%)</option>

<option value="file1">github.com/Narasimha1997/ratelimiter/limiter.go (97.1%)</option>
<option value="file1">github.com/Narasimha1997/ratelimiter/limiter.go (100.0%)</option>

<option value="file2">github.com/Narasimha1997/ratelimiter/window.go (100.0%)</option>

Expand Down Expand Up @@ -304,7 +304,7 @@
return false, fmt.Errorf("function ShouldAllow called on an inactive instance")
}</span>

<span class="cov8" title="1">if l.limit == 0 || l.size &lt; time.Millisecond </span><span class="cov0" title="0">{
<span class="cov8" title="1">if l.limit == 0 || l.size &lt; time.Millisecond </span><span class="cov8" title="1">{
return false, fmt.Errorf("invalid limiter configuration")
}</span>

Expand Down Expand Up @@ -416,7 +416,7 @@
return false, fmt.Errorf("function ShouldAllow called on an inactive instance")
}</span>

<span class="cov8" title="1">if s.limit == 0 || s.size &lt; time.Millisecond </span><span class="cov0" title="0">{
<span class="cov8" title="1">if s.limit == 0 || s.size &lt; time.Millisecond </span><span class="cov8" title="1">{
return false, fmt.Errorf("invalid limiter configuration")
}</span>

Expand Down
12 changes: 12 additions & 0 deletions limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ import (
"time"
)

func TestInvalidLimiterConfiguration(t *testing.T) {
limiter := NewDefaultLimiter(10, time.Microsecond*800)
if _, err := limiter.ShouldAllow(3); err == nil {
t.Fatalf("ShouldAllow() failed, did not throw error when window size <= 1 millisecond")
}

limiter1 := NewSyncLimiter(0, 10*time.Second)
if _, err := limiter1.ShouldAllow(10); err == nil {
t.Fatalf("ShouldAllow() failed, did not throw error when limit == 0")
}
}

func TestLimiterAccuracy(t *testing.T) {

nRuns := 10
Expand Down

0 comments on commit d0157a8

Please sign in to comment.