forked from mirror/ants
Leverage tool testify to refine unit-test code
This commit is contained in:
parent
c7ddae76e4
commit
ea787e5c0b
174
ants_test.go
174
ants_test.go
|
@ -28,6 +28,8 @@ import (
|
|||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -238,9 +240,7 @@ 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() {
|
||||
|
@ -248,31 +248,20 @@ func TestPanicHandler(t *testing.T) {
|
|||
})
|
||||
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) {
|
||||
|
@ -283,9 +272,7 @@ 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() {
|
||||
|
@ -293,38 +280,25 @@ func TestPanicHandlerPreMalloc(t *testing.T) {
|
|||
})
|
||||
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!")
|
||||
|
@ -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!")
|
||||
|
@ -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{})
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
|
6
go.mod
6
go.mod
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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=
|
|
@ -3,22 +3,16 @@ package ants
|
|||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewLoopQueue(t *testing.T) {
|
||||
size := 100
|
||||
q := newWorkerLoopQueue(size)
|
||||
if q.len() != 0 {
|
||||
t.Fatalf("Len error")
|
||||
}
|
||||
|
||||
if !q.isEmpty() {
|
||||
t.Fatalf("IsEmpty error")
|
||||
}
|
||||
|
||||
if q.detach() != nil {
|
||||
t.Fatalf("Dequeue error")
|
||||
}
|
||||
assert.EqualValues(t, 0, q.len(), "Len error")
|
||||
assert.Equal(t, true, q.isEmpty(), "IsEmpty error")
|
||||
assert.Nil(t, q.detach(), "Dequeue error")
|
||||
}
|
||||
|
||||
func TestLoopQueue(t *testing.T) {
|
||||
|
@ -31,17 +25,10 @@ func TestLoopQueue(t *testing.T) {
|
|||
break
|
||||
}
|
||||
}
|
||||
|
||||
if q.len() != 5 {
|
||||
t.Fatalf("Len error")
|
||||
}
|
||||
|
||||
assert.EqualValues(t, 5, q.len(), "Len error")
|
||||
v := q.detach()
|
||||
t.Log(v)
|
||||
|
||||
if q.len() != 4 {
|
||||
t.Fatalf("Len error")
|
||||
}
|
||||
assert.EqualValues(t, 4, q.len(), "Len error")
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
|
@ -51,19 +38,11 @@ func TestLoopQueue(t *testing.T) {
|
|||
break
|
||||
}
|
||||
}
|
||||
|
||||
if q.len() != 10 {
|
||||
t.Fatalf("Len error")
|
||||
}
|
||||
assert.EqualValues(t, 10, q.len(), "Len error")
|
||||
|
||||
err := q.insert(&goWorker{recycleTime: time.Now()})
|
||||
if err == nil {
|
||||
t.Fatalf("Enqueue error")
|
||||
}
|
||||
assert.Error(t, err, "Enqueue, error")
|
||||
|
||||
q.retrieveExpiry(time.Second)
|
||||
|
||||
if q.len() != 6 {
|
||||
t.Fatalf("Len error: %d", q.len())
|
||||
}
|
||||
assert.EqualValuesf(t, 6, q.len(), "Len error: %d", q.len())
|
||||
}
|
||||
|
|
|
@ -5,22 +5,16 @@ package ants
|
|||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewWorkerStack(t *testing.T) {
|
||||
size := 100
|
||||
q := newWorkerStack(size)
|
||||
if q.len() != 0 {
|
||||
t.Fatal("Len error")
|
||||
}
|
||||
|
||||
if !q.isEmpty() {
|
||||
t.Fatal("IsEmpty error")
|
||||
}
|
||||
|
||||
if q.detach() != nil {
|
||||
t.Fatal("Dequeue error")
|
||||
}
|
||||
assert.EqualValues(t, 0, q.len(), "Len error")
|
||||
assert.Equal(t, true, q.isEmpty(), "IsEmpty error")
|
||||
assert.Nil(t, q.detach(), "Dequeue error")
|
||||
}
|
||||
|
||||
func TestWorkerStack(t *testing.T) {
|
||||
|
@ -32,9 +26,7 @@ func TestWorkerStack(t *testing.T) {
|
|||
break
|
||||
}
|
||||
}
|
||||
if q.len() != 5 {
|
||||
t.Fatal("Len error")
|
||||
}
|
||||
assert.EqualValues(t, 5, q.len(), "Len error")
|
||||
|
||||
expired := time.Now()
|
||||
|
||||
|
@ -51,16 +43,9 @@ func TestWorkerStack(t *testing.T) {
|
|||
t.Fatal("Enqueue error")
|
||||
}
|
||||
}
|
||||
|
||||
if q.len() != 12 {
|
||||
t.Fatal("Len error")
|
||||
}
|
||||
|
||||
assert.EqualValues(t, 12, q.len(), "Len error")
|
||||
q.retrieveExpiry(time.Second)
|
||||
|
||||
if q.len() != 6 {
|
||||
t.Fatal("Len error")
|
||||
}
|
||||
assert.EqualValues(t, 6, q.len(), "Len error")
|
||||
}
|
||||
|
||||
// It seems that something wrong with time.Now() on Windows, not sure whether it is a bug on Windows, so exclude this test
|
||||
|
@ -73,34 +58,18 @@ func TestSearch(t *testing.T) {
|
|||
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
|
||||
index := q.binarySearch(0, q.len()-1, time.Now())
|
||||
if index != 0 {
|
||||
t.Fatal("index should be 0")
|
||||
}
|
||||
|
||||
index = q.binarySearch(0, q.len()-1, expiry1)
|
||||
if index != -1 {
|
||||
t.Fatal("index should be -1")
|
||||
}
|
||||
assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, time.Now()), "index should be 0")
|
||||
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
||||
|
||||
// 2
|
||||
expiry2 := time.Now()
|
||||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
|
||||
index = q.binarySearch(0, q.len()-1, expiry1)
|
||||
if index != -1 {
|
||||
t.Fatal("index should be -1")
|
||||
}
|
||||
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
||||
|
||||
index = q.binarySearch(0, q.len()-1, expiry2)
|
||||
if index != 0 {
|
||||
t.Fatal("index should be 0")
|
||||
}
|
||||
assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, expiry2), "index should be 0")
|
||||
|
||||
index = q.binarySearch(0, q.len()-1, time.Now())
|
||||
if index != 1 {
|
||||
t.Fatal("index should be 1")
|
||||
}
|
||||
assert.EqualValues(t, 1, q.binarySearch(0, q.len()-1, time.Now()), "index should be 1")
|
||||
|
||||
// more
|
||||
for i := 0; i < 5; i++ {
|
||||
|
@ -115,8 +84,5 @@ func TestSearch(t *testing.T) {
|
|||
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
||||
}
|
||||
|
||||
index = q.binarySearch(0, q.len()-1, expiry3)
|
||||
if index != 7 {
|
||||
t.Fatal("index should be 7")
|
||||
}
|
||||
assert.EqualValues(t, 7, q.binarySearch(0, q.len()-1, expiry3), "index should be 7")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue