Skip to content

Commit

Permalink
add initial throttler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1pkg committed Jul 25, 2020
1 parent 63de18b commit 388c014
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ require (
github.com/segmentio/kafka-go v0.3.7
github.com/shirou/gopsutil v2.20.6+incompatible
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.6.1 // indirect
github.com/stretchr/testify v1.6.1
github.com/twinj/uuid v1.0.0 // indirect
github.com/valyala/fasthttp v1.15.0
github.com/valyala/fasttemplate v1.2.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ github.com/streadway/amqp v1.0.0 h1:kuuDrUJFZL1QYL9hUNuCxNObNzB0bV/ZG5jV3RWAQgo=
github.com/streadway/amqp v1.0.0/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
73 changes: 73 additions & 0 deletions throttlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package gohalt

import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestThrottlerEcho(t *testing.T) {
table := map[string]struct {
thr techo
ctx context.Context
err error
}{
"Throttler echo should not throttle on nil input": {
thr: NewThrottlerEcho(nil),
},
"Throttler echo should throttle on not nil input": {
thr: NewThrottlerEcho(errors.New("error")),
err: errors.New("error"),
},
}
for tname, tcase := range table {
t.Run(tname, func(t *testing.T) {
assert.Equal(t, tcase.err, tcase.thr.Acquire(tcase.ctx))
assert.Equal(t, tcase.err, tcase.thr.Release(tcase.ctx))
})
}
}

func TestThrottlerWait(t *testing.T) {
table := map[string]struct {
thr twait
ctx context.Context
err error
}{
"Throttler wait should sleep for millisecond": {
thr: NewThrottlerWait(time.Millisecond),
},
"Throttler wait should sleep for nanosecond": {
thr: NewThrottlerWait(time.Nanosecond),
},
}
for tname, tcase := range table {
t.Run(tname, func(t *testing.T) {
ts := time.Now()
assert.Equal(t, tcase.err, tcase.thr.Acquire(tcase.ctx))
assert.Greater(t, int64(time.Since(ts)), int64(tcase.thr.duration))
assert.Equal(t, tcase.err, tcase.thr.Release(tcase.ctx))
})
}
}

func TestThrottlerPanic(t *testing.T) {
table := map[string]struct {
thr tpanic
ctx context.Context
err error
}{
"Throttler panic should panic": {
thr: NewThrottlerPanic(),
},
}
for tname, tcase := range table {
t.Run(tname, func(t *testing.T) {
assert.Panics(t, func() { _ = tcase.thr.Acquire(tcase.ctx) })
assert.Equal(t, tcase.err, tcase.thr.Release(tcase.ctx))
})
}
}

0 comments on commit 388c014

Please sign in to comment.