Skip to content

Commit

Permalink
Leverage tool testify to refine unit-test code
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jan 8, 2020
1 parent c7ddae7 commit ea787e5
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 207 deletions.
174 changes: 46 additions & 128 deletions ants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"sync/atomic"
"testing"
"time"

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

const (
Expand Down Expand Up @@ -238,41 +240,28 @@ func TestPanicHandler(t *testing.T) {
atomic.AddInt64(&panicCounter, 1)
t.Logf("catch panic with PanicHandler: %v", p)
}))
if err != nil {
t.Fatalf("create new pool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create new pool failed: %v", err)
defer p0.Release()
wg.Add(1)
_ = p0.Submit(func() {
panic("Oops!")
})
wg.Wait()
c := atomic.LoadInt64(&panicCounter)
if c != 1 {
t.Errorf("panic handler didn't work, panicCounter: %d", c)
}
if p0.Running() != 0 {
t.Errorf("pool should be empty after panic")
}

assert.EqualValuesf(t, 1, c, "panic handler didn't work, panicCounter: %d", c)
assert.EqualValues(t, 0, p0.Running(), "pool should be empty after panic")
p1, err := NewPoolWithFunc(10, func(p interface{}) { panic(p) }, WithPanicHandler(func(p interface{}) {
defer wg.Done()
atomic.AddInt64(&panicCounter, 1)
}))
if err != nil {
t.Fatalf("create new pool with func failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create new pool with func failed: %v", err)
defer p1.Release()
wg.Add(1)
_ = p1.Invoke("Oops!")
wg.Wait()
c = atomic.LoadInt64(&panicCounter)
if c != 2 {
t.Errorf("panic handler didn't work, panicCounter: %d", c)
}
if p1.Running() != 0 {
t.Errorf("pool should be empty after panic")
}
assert.EqualValuesf(t, 2, c, "panic handler didn't work, panicCounter: %d", c)
assert.EqualValues(t, 0, p1.Running(), "pool should be empty after panic")
}

func TestPanicHandlerPreMalloc(t *testing.T) {
Expand All @@ -283,48 +272,33 @@ func TestPanicHandlerPreMalloc(t *testing.T) {
atomic.AddInt64(&panicCounter, 1)
t.Logf("catch panic with PanicHandler: %v", p)
}))
if err != nil {
t.Fatalf("create new pool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create new pool failed: %v", err)
defer p0.Release()
wg.Add(1)
_ = p0.Submit(func() {
panic("Oops!")
})
wg.Wait()
c := atomic.LoadInt64(&panicCounter)
if c != 1 {
t.Errorf("panic handler didn't work, panicCounter: %d", c)
}
if p0.Running() != 0 {
t.Errorf("pool should be empty after panic")
}

assert.EqualValuesf(t, 1, c, "panic handler didn't work, panicCounter: %d", c)
assert.EqualValues(t, 0, p0.Running(), "pool should be empty after panic")
p1, err := NewPoolWithFunc(10, func(p interface{}) { panic(p) }, WithPanicHandler(func(p interface{}) {
defer wg.Done()
atomic.AddInt64(&panicCounter, 1)
}))
if err != nil {
t.Fatalf("create new pool with func failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create new pool with func failed: %v", err)
defer p1.Release()
wg.Add(1)
_ = p1.Invoke("Oops!")
wg.Wait()
c = atomic.LoadInt64(&panicCounter)
if c != 2 {
t.Errorf("panic handler didn't work, panicCounter: %d", c)
}
if p1.Running() != 0 {
t.Errorf("pool should be empty after panic")
}
assert.EqualValuesf(t, 2, c, "panic handler didn't work, panicCounter: %d", c)
assert.EqualValues(t, 0, p1.Running(), "pool should be empty after panic")
}

func TestPoolPanicWithoutHandler(t *testing.T) {
p0, err := NewPool(10)
if err != nil {
t.Fatalf("create new pool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create new pool failed: %v", err)
defer p0.Release()
_ = p0.Submit(func() {
panic("Oops!")
Expand All @@ -333,18 +307,14 @@ func TestPoolPanicWithoutHandler(t *testing.T) {
p1, err := NewPoolWithFunc(10, func(p interface{}) {
panic(p)
})
if err != nil {
t.Fatalf("create new pool with func failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create new pool with func failed: %v", err)
defer p1.Release()
_ = p1.Invoke("Oops!")
}

func TestPoolPanicWithoutHandlerPreMalloc(t *testing.T) {
p0, err := NewPool(10, WithPreAlloc(true))
if err != nil {
t.Fatalf("create new pool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create new pool failed: %v", err)
defer p0.Release()
_ = p0.Submit(func() {
panic("Oops!")
Expand All @@ -353,70 +323,50 @@ func TestPoolPanicWithoutHandlerPreMalloc(t *testing.T) {
p1, err := NewPoolWithFunc(10, func(p interface{}) {
panic(p)
})
if err != nil {
t.Fatalf("create new pool with func failed: %s", err.Error())
}

assert.NoErrorf(t, err, "create new pool with func failed: %v", err)

defer p1.Release()
_ = p1.Invoke("Oops!")
}

func TestPurge(t *testing.T) {
p, err := NewPool(10)
if err != nil {
t.Fatalf("create TimingPool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
defer p.Release()
_ = p.Submit(demoFunc)
time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 {
t.Error("all p should be purged")
}
assert.EqualValues(t, 0, p.Running(), "all p should be purged")
p1, err := NewPoolWithFunc(10, demoPoolFunc)
if err != nil {
t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create TimingPoolWithFunc failed: %v", err)
defer p1.Release()
_ = p1.Invoke(1)
time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 {
t.Error("all p should be purged")
}
assert.EqualValues(t, 0, p.Running(), "all p should be purged")
}

func TestPurgePreMalloc(t *testing.T) {
p, err := NewPool(10, WithPreAlloc(true))
if err != nil {
t.Fatalf("create TimingPool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
defer p.Release()
_ = p.Submit(demoFunc)
time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 {
t.Error("all p should be purged")
}
assert.EqualValues(t, 0, p.Running(), "all p should be purged")
p1, err := NewPoolWithFunc(10, demoPoolFunc)
if err != nil {
t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create TimingPoolWithFunc failed: %v", err)
defer p1.Release()
_ = p1.Invoke(1)
time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 {
t.Error("all p should be purged")
}
assert.EqualValues(t, 0, p.Running(), "all p should be purged")
}

func TestNonblockingSubmit(t *testing.T) {
poolSize := 10
p, err := NewPool(poolSize, WithNonblocking(true))
if err != nil {
t.Fatalf("create TimingPool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
defer p.Release()
for i := 0; i < poolSize-1; i++ {
if err := p.Submit(longRunningFunc); err != nil {
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Submit(longRunningFunc), "nonblocking submit when pool is not full shouldn't return error")
}
ch := make(chan struct{})
ch1 := make(chan struct{})
Expand All @@ -425,40 +375,28 @@ func TestNonblockingSubmit(t *testing.T) {
close(ch1)
}
// p is full now.
if err := p.Submit(f); err != nil {
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
if err := p.Submit(demoFunc); err == nil || err != ErrPoolOverload {
t.Fatalf("nonblocking submit when pool is full should get an ErrPoolOverload")
}
assert.NoError(t, p.Submit(f), "nonblocking submit when pool is not full shouldn't return error")
assert.EqualError(t, p.Submit(demoFunc), ErrPoolOverload.Error(), "nonblocking submit when pool is full should get an ErrPoolOverload")
// interrupt f to get an available worker
close(ch)
<-ch1
if err := p.Submit(demoFunc); err != nil {
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Submit(demoFunc), "nonblocking submit when pool is not full shouldn't return error")
}

func TestMaxBlockingSubmit(t *testing.T) {
poolSize := 10
p, err := NewPool(poolSize, WithMaxBlockingTasks(1))
if err != nil {
t.Fatalf("create TimingPool failed: %s", err.Error())
}
assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
defer p.Release()
for i := 0; i < poolSize-1; i++ {
if err := p.Submit(longRunningFunc); err != nil {
t.Fatalf("submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Submit(longRunningFunc), "submit when pool is not full shouldn't return error")
}
ch := make(chan struct{})
f := func() {
<-ch
}
// p is full now.
if err := p.Submit(f); err != nil {
t.Fatalf("submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Submit(f), "submit when pool is not full shouldn't return error")
var wg sync.WaitGroup
wg.Add(1)
errCh := make(chan error, 1)
Expand All @@ -471,9 +409,7 @@ func TestMaxBlockingSubmit(t *testing.T) {
}()
time.Sleep(1 * time.Second)
// already reached max blocking limit
if err := p.Submit(demoFunc); err != ErrPoolOverload {
t.Fatalf("blocking submit when pool reach max blocking submit should return ErrPoolOverload")
}
assert.EqualError(t, p.Submit(demoFunc), ErrPoolOverload.Error(), "blocking submit when pool reach max blocking submit should return ErrPoolOverload")
// interrupt f to make blocking submit successful.
close(ch)
wg.Wait()
Expand All @@ -491,48 +427,32 @@ func TestNonblockingSubmitWithFunc(t *testing.T) {
longRunningPoolFunc(i)
close(ch1)
}, WithNonblocking(true))
if err != nil {
t.Fatalf("create TimingPool failed: %s", err.Error())
}
assert.NoError(t, err, "create TimingPool failed: %v", err)
defer p.Release()
for i := 0; i < poolSize-1; i++ {
if err := p.Invoke(nil); err != nil {
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error")
}
ch := make(chan struct{})
// p is full now.
if err := p.Invoke(ch); err != nil {
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
if err := p.Invoke(nil); err == nil || err != ErrPoolOverload {
t.Fatalf("nonblocking submit when pool is full should get an ErrPoolOverload")
}
assert.NoError(t, p.Invoke(ch), "nonblocking submit when pool is not full shouldn't return error")
assert.EqualError(t, p.Invoke(nil), ErrPoolOverload.Error(), "nonblocking submit when pool is full should get an ErrPoolOverload")
// interrupt f to get an available worker
close(ch)
<-ch1
if err := p.Invoke(nil); err != nil {
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error")
}

func TestMaxBlockingSubmitWithFunc(t *testing.T) {
poolSize := 10
p, err := NewPoolWithFunc(poolSize, longRunningPoolFunc, WithMaxBlockingTasks(1))
if err != nil {
t.Fatalf("create TimingPool failed: %s", err.Error())
}
assert.NoError(t, err, "create TimingPool failed: %v", err)
defer p.Release()
for i := 0; i < poolSize-1; i++ {
if err := p.Invoke(Param); err != nil {
t.Fatalf("submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Invoke(Param), "submit when pool is not full shouldn't return error")
}
ch := make(chan struct{})
// p is full now.
if err := p.Invoke(ch); err != nil {
t.Fatalf("submit when pool is not full shouldn't return error")
}
assert.NoError(t, p.Invoke(ch), "submit when pool is not full shouldn't return error")
var wg sync.WaitGroup
wg.Add(1)
errCh := make(chan error, 1)
Expand All @@ -545,9 +465,7 @@ func TestMaxBlockingSubmitWithFunc(t *testing.T) {
}()
time.Sleep(1 * time.Second)
// already reached max blocking limit
if err := p.Invoke(Param); err != ErrPoolOverload {
t.Fatalf("blocking submit when pool reach max blocking submit should return ErrPoolOverload: %v", err)
}
assert.EqualErrorf(t, p.Invoke(Param), ErrPoolOverload.Error(), "blocking submit when pool reach max blocking submit should return ErrPoolOverload: %v", err)
// interrupt one func to make blocking submit successful.
close(ch)
wg.Wait()
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/panjf2000/ants/v2

go 1.13

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.2.7 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit ea787e5

Please sign in to comment.