Leverage tool testify to refine unit-test code

This commit is contained in:
Andy Pan 2020-01-08 10:53:23 +08:00
parent c7ddae76e4
commit ea787e5c0b
5 changed files with 92 additions and 207 deletions

View File

@ -28,6 +28,8 @@ import (
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
) )
const ( const (
@ -238,9 +240,7 @@ func TestPanicHandler(t *testing.T) {
atomic.AddInt64(&panicCounter, 1) atomic.AddInt64(&panicCounter, 1)
t.Logf("catch panic with PanicHandler: %v", p) t.Logf("catch panic with PanicHandler: %v", p)
})) }))
if err != nil { assert.NoErrorf(t, err, "create new pool failed: %v", err)
t.Fatalf("create new pool failed: %s", err.Error())
}
defer p0.Release() defer p0.Release()
wg.Add(1) wg.Add(1)
_ = p0.Submit(func() { _ = p0.Submit(func() {
@ -248,31 +248,20 @@ func TestPanicHandler(t *testing.T) {
}) })
wg.Wait() wg.Wait()
c := atomic.LoadInt64(&panicCounter) c := atomic.LoadInt64(&panicCounter)
if c != 1 { assert.EqualValuesf(t, 1, c, "panic handler didn't work, panicCounter: %d", c)
t.Errorf("panic handler didn't work, panicCounter: %d", c) assert.EqualValues(t, 0, p0.Running(), "pool should be empty after panic")
}
if p0.Running() != 0 {
t.Errorf("pool should be empty after panic")
}
p1, err := NewPoolWithFunc(10, func(p interface{}) { panic(p) }, WithPanicHandler(func(p interface{}) { p1, err := NewPoolWithFunc(10, func(p interface{}) { panic(p) }, WithPanicHandler(func(p interface{}) {
defer wg.Done() defer wg.Done()
atomic.AddInt64(&panicCounter, 1) atomic.AddInt64(&panicCounter, 1)
})) }))
if err != nil { assert.NoErrorf(t, err, "create new pool with func failed: %v", err)
t.Fatalf("create new pool with func failed: %s", err.Error())
}
defer p1.Release() defer p1.Release()
wg.Add(1) wg.Add(1)
_ = p1.Invoke("Oops!") _ = p1.Invoke("Oops!")
wg.Wait() wg.Wait()
c = atomic.LoadInt64(&panicCounter) c = atomic.LoadInt64(&panicCounter)
if c != 2 { assert.EqualValuesf(t, 2, c, "panic handler didn't work, panicCounter: %d", c)
t.Errorf("panic handler didn't work, panicCounter: %d", c) assert.EqualValues(t, 0, p1.Running(), "pool should be empty after panic")
}
if p1.Running() != 0 {
t.Errorf("pool should be empty after panic")
}
} }
func TestPanicHandlerPreMalloc(t *testing.T) { func TestPanicHandlerPreMalloc(t *testing.T) {
@ -283,9 +272,7 @@ func TestPanicHandlerPreMalloc(t *testing.T) {
atomic.AddInt64(&panicCounter, 1) atomic.AddInt64(&panicCounter, 1)
t.Logf("catch panic with PanicHandler: %v", p) t.Logf("catch panic with PanicHandler: %v", p)
})) }))
if err != nil { assert.NoErrorf(t, err, "create new pool failed: %v", err)
t.Fatalf("create new pool failed: %s", err.Error())
}
defer p0.Release() defer p0.Release()
wg.Add(1) wg.Add(1)
_ = p0.Submit(func() { _ = p0.Submit(func() {
@ -293,38 +280,25 @@ func TestPanicHandlerPreMalloc(t *testing.T) {
}) })
wg.Wait() wg.Wait()
c := atomic.LoadInt64(&panicCounter) c := atomic.LoadInt64(&panicCounter)
if c != 1 { assert.EqualValuesf(t, 1, c, "panic handler didn't work, panicCounter: %d", c)
t.Errorf("panic handler didn't work, panicCounter: %d", c) assert.EqualValues(t, 0, p0.Running(), "pool should be empty after panic")
}
if p0.Running() != 0 {
t.Errorf("pool should be empty after panic")
}
p1, err := NewPoolWithFunc(10, func(p interface{}) { panic(p) }, WithPanicHandler(func(p interface{}) { p1, err := NewPoolWithFunc(10, func(p interface{}) { panic(p) }, WithPanicHandler(func(p interface{}) {
defer wg.Done() defer wg.Done()
atomic.AddInt64(&panicCounter, 1) atomic.AddInt64(&panicCounter, 1)
})) }))
if err != nil { assert.NoErrorf(t, err, "create new pool with func failed: %v", err)
t.Fatalf("create new pool with func failed: %s", err.Error())
}
defer p1.Release() defer p1.Release()
wg.Add(1) wg.Add(1)
_ = p1.Invoke("Oops!") _ = p1.Invoke("Oops!")
wg.Wait() wg.Wait()
c = atomic.LoadInt64(&panicCounter) c = atomic.LoadInt64(&panicCounter)
if c != 2 { assert.EqualValuesf(t, 2, c, "panic handler didn't work, panicCounter: %d", c)
t.Errorf("panic handler didn't work, panicCounter: %d", c) assert.EqualValues(t, 0, p1.Running(), "pool should be empty after panic")
}
if p1.Running() != 0 {
t.Errorf("pool should be empty after panic")
}
} }
func TestPoolPanicWithoutHandler(t *testing.T) { func TestPoolPanicWithoutHandler(t *testing.T) {
p0, err := NewPool(10) p0, err := NewPool(10)
if err != nil { assert.NoErrorf(t, err, "create new pool failed: %v", err)
t.Fatalf("create new pool failed: %s", err.Error())
}
defer p0.Release() defer p0.Release()
_ = p0.Submit(func() { _ = p0.Submit(func() {
panic("Oops!") panic("Oops!")
@ -333,18 +307,14 @@ func TestPoolPanicWithoutHandler(t *testing.T) {
p1, err := NewPoolWithFunc(10, func(p interface{}) { p1, err := NewPoolWithFunc(10, func(p interface{}) {
panic(p) panic(p)
}) })
if err != nil { assert.NoErrorf(t, err, "create new pool with func failed: %v", err)
t.Fatalf("create new pool with func failed: %s", err.Error())
}
defer p1.Release() defer p1.Release()
_ = p1.Invoke("Oops!") _ = p1.Invoke("Oops!")
} }
func TestPoolPanicWithoutHandlerPreMalloc(t *testing.T) { func TestPoolPanicWithoutHandlerPreMalloc(t *testing.T) {
p0, err := NewPool(10, WithPreAlloc(true)) p0, err := NewPool(10, WithPreAlloc(true))
if err != nil { assert.NoErrorf(t, err, "create new pool failed: %v", err)
t.Fatalf("create new pool failed: %s", err.Error())
}
defer p0.Release() defer p0.Release()
_ = p0.Submit(func() { _ = p0.Submit(func() {
panic("Oops!") panic("Oops!")
@ -353,70 +323,50 @@ func TestPoolPanicWithoutHandlerPreMalloc(t *testing.T) {
p1, err := NewPoolWithFunc(10, func(p interface{}) { p1, err := NewPoolWithFunc(10, func(p interface{}) {
panic(p) 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() defer p1.Release()
_ = p1.Invoke("Oops!") _ = p1.Invoke("Oops!")
} }
func TestPurge(t *testing.T) { func TestPurge(t *testing.T) {
p, err := NewPool(10) p, err := NewPool(10)
if err != nil { assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
t.Fatalf("create TimingPool failed: %s", err.Error())
}
defer p.Release() defer p.Release()
_ = p.Submit(demoFunc) _ = p.Submit(demoFunc)
time.Sleep(3 * DefaultCleanIntervalTime) time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 { assert.EqualValues(t, 0, p.Running(), "all p should be purged")
t.Error("all p should be purged")
}
p1, err := NewPoolWithFunc(10, demoPoolFunc) p1, err := NewPoolWithFunc(10, demoPoolFunc)
if err != nil { assert.NoErrorf(t, err, "create TimingPoolWithFunc failed: %v", err)
t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error())
}
defer p1.Release() defer p1.Release()
_ = p1.Invoke(1) _ = p1.Invoke(1)
time.Sleep(3 * DefaultCleanIntervalTime) time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 { assert.EqualValues(t, 0, p.Running(), "all p should be purged")
t.Error("all p should be purged")
}
} }
func TestPurgePreMalloc(t *testing.T) { func TestPurgePreMalloc(t *testing.T) {
p, err := NewPool(10, WithPreAlloc(true)) p, err := NewPool(10, WithPreAlloc(true))
if err != nil { assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
t.Fatalf("create TimingPool failed: %s", err.Error())
}
defer p.Release() defer p.Release()
_ = p.Submit(demoFunc) _ = p.Submit(demoFunc)
time.Sleep(3 * DefaultCleanIntervalTime) time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 { assert.EqualValues(t, 0, p.Running(), "all p should be purged")
t.Error("all p should be purged")
}
p1, err := NewPoolWithFunc(10, demoPoolFunc) p1, err := NewPoolWithFunc(10, demoPoolFunc)
if err != nil { assert.NoErrorf(t, err, "create TimingPoolWithFunc failed: %v", err)
t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error())
}
defer p1.Release() defer p1.Release()
_ = p1.Invoke(1) _ = p1.Invoke(1)
time.Sleep(3 * DefaultCleanIntervalTime) time.Sleep(3 * DefaultCleanIntervalTime)
if p.Running() != 0 { assert.EqualValues(t, 0, p.Running(), "all p should be purged")
t.Error("all p should be purged")
}
} }
func TestNonblockingSubmit(t *testing.T) { func TestNonblockingSubmit(t *testing.T) {
poolSize := 10 poolSize := 10
p, err := NewPool(poolSize, WithNonblocking(true)) p, err := NewPool(poolSize, WithNonblocking(true))
if err != nil { assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
t.Fatalf("create TimingPool failed: %s", err.Error())
}
defer p.Release() defer p.Release()
for i := 0; i < poolSize-1; i++ { for i := 0; i < poolSize-1; i++ {
if err := p.Submit(longRunningFunc); err != nil { assert.NoError(t, p.Submit(longRunningFunc), "nonblocking submit when pool is not full shouldn't return error")
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
} }
ch := make(chan struct{}) ch := make(chan struct{})
ch1 := make(chan struct{}) ch1 := make(chan struct{})
@ -425,40 +375,28 @@ func TestNonblockingSubmit(t *testing.T) {
close(ch1) close(ch1)
} }
// p is full now. // p is full now.
if err := p.Submit(f); err != nil { assert.NoError(t, p.Submit(f), "nonblocking submit when pool is not full shouldn't return error")
t.Fatalf("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")
}
if err := p.Submit(demoFunc); err == nil || err != ErrPoolOverload {
t.Fatalf("nonblocking submit when pool is full should get an ErrPoolOverload")
}
// interrupt f to get an available worker // interrupt f to get an available worker
close(ch) close(ch)
<-ch1 <-ch1
if err := p.Submit(demoFunc); err != nil { assert.NoError(t, p.Submit(demoFunc), "nonblocking submit when pool is not full shouldn't return error")
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
} }
func TestMaxBlockingSubmit(t *testing.T) { func TestMaxBlockingSubmit(t *testing.T) {
poolSize := 10 poolSize := 10
p, err := NewPool(poolSize, WithMaxBlockingTasks(1)) p, err := NewPool(poolSize, WithMaxBlockingTasks(1))
if err != nil { assert.NoErrorf(t, err, "create TimingPool failed: %v", err)
t.Fatalf("create TimingPool failed: %s", err.Error())
}
defer p.Release() defer p.Release()
for i := 0; i < poolSize-1; i++ { for i := 0; i < poolSize-1; i++ {
if err := p.Submit(longRunningFunc); err != nil { assert.NoError(t, p.Submit(longRunningFunc), "submit when pool is not full shouldn't return error")
t.Fatalf("submit when pool is not full shouldn't return error")
}
} }
ch := make(chan struct{}) ch := make(chan struct{})
f := func() { f := func() {
<-ch <-ch
} }
// p is full now. // p is full now.
if err := p.Submit(f); err != nil { assert.NoError(t, p.Submit(f), "submit when pool is not full shouldn't return error")
t.Fatalf("submit when pool is not full shouldn't return error")
}
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
errCh := make(chan error, 1) errCh := make(chan error, 1)
@ -471,9 +409,7 @@ func TestMaxBlockingSubmit(t *testing.T) {
}() }()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
// already reached max blocking limit // already reached max blocking limit
if err := p.Submit(demoFunc); err != ErrPoolOverload { assert.EqualError(t, p.Submit(demoFunc), ErrPoolOverload.Error(), "blocking submit when pool reach max blocking submit should return ErrPoolOverload")
t.Fatalf("blocking submit when pool reach max blocking submit should return ErrPoolOverload")
}
// interrupt f to make blocking submit successful. // interrupt f to make blocking submit successful.
close(ch) close(ch)
wg.Wait() wg.Wait()
@ -491,48 +427,32 @@ func TestNonblockingSubmitWithFunc(t *testing.T) {
longRunningPoolFunc(i) longRunningPoolFunc(i)
close(ch1) close(ch1)
}, WithNonblocking(true)) }, WithNonblocking(true))
if err != nil { assert.NoError(t, err, "create TimingPool failed: %v", err)
t.Fatalf("create TimingPool failed: %s", err.Error())
}
defer p.Release() defer p.Release()
for i := 0; i < poolSize-1; i++ { for i := 0; i < poolSize-1; i++ {
if err := p.Invoke(nil); err != nil { assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error")
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
} }
ch := make(chan struct{}) ch := make(chan struct{})
// p is full now. // p is full now.
if err := p.Invoke(ch); err != nil { assert.NoError(t, p.Invoke(ch), "nonblocking submit when pool is not full shouldn't return error")
t.Fatalf("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")
}
if err := p.Invoke(nil); err == nil || err != ErrPoolOverload {
t.Fatalf("nonblocking submit when pool is full should get an ErrPoolOverload")
}
// interrupt f to get an available worker // interrupt f to get an available worker
close(ch) close(ch)
<-ch1 <-ch1
if err := p.Invoke(nil); err != nil { assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error")
t.Fatalf("nonblocking submit when pool is not full shouldn't return error")
}
} }
func TestMaxBlockingSubmitWithFunc(t *testing.T) { func TestMaxBlockingSubmitWithFunc(t *testing.T) {
poolSize := 10 poolSize := 10
p, err := NewPoolWithFunc(poolSize, longRunningPoolFunc, WithMaxBlockingTasks(1)) p, err := NewPoolWithFunc(poolSize, longRunningPoolFunc, WithMaxBlockingTasks(1))
if err != nil { assert.NoError(t, err, "create TimingPool failed: %v", err)
t.Fatalf("create TimingPool failed: %s", err.Error())
}
defer p.Release() defer p.Release()
for i := 0; i < poolSize-1; i++ { for i := 0; i < poolSize-1; i++ {
if err := p.Invoke(Param); err != nil { assert.NoError(t, p.Invoke(Param), "submit when pool is not full shouldn't return error")
t.Fatalf("submit when pool is not full shouldn't return error")
}
} }
ch := make(chan struct{}) ch := make(chan struct{})
// p is full now. // p is full now.
if err := p.Invoke(ch); err != nil { assert.NoError(t, p.Invoke(ch), "submit when pool is not full shouldn't return error")
t.Fatalf("submit when pool is not full shouldn't return error")
}
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
errCh := make(chan error, 1) errCh := make(chan error, 1)
@ -545,9 +465,7 @@ func TestMaxBlockingSubmitWithFunc(t *testing.T) {
}() }()
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
// already reached max blocking limit // already reached max blocking limit
if err := p.Invoke(Param); err != ErrPoolOverload { assert.EqualErrorf(t, p.Invoke(Param), ErrPoolOverload.Error(), "blocking submit when pool reach max blocking submit should return ErrPoolOverload: %v", err)
t.Fatalf("blocking submit when pool reach max blocking submit should return ErrPoolOverload: %v", err)
}
// interrupt one func to make blocking submit successful. // interrupt one func to make blocking submit successful.
close(ch) close(ch)
wg.Wait() wg.Wait()

6
go.mod
View File

@ -1,3 +1,9 @@
module github.com/panjf2000/ants/v2 module github.com/panjf2000/ants/v2
go 1.13 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
go.sum Normal file
View File

@ -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=

View File

@ -3,22 +3,16 @@ package ants
import ( import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
) )
func TestNewLoopQueue(t *testing.T) { func TestNewLoopQueue(t *testing.T) {
size := 100 size := 100
q := newWorkerLoopQueue(size) q := newWorkerLoopQueue(size)
if q.len() != 0 { assert.EqualValues(t, 0, q.len(), "Len error")
t.Fatalf("Len error") assert.Equal(t, true, q.isEmpty(), "IsEmpty error")
} assert.Nil(t, q.detach(), "Dequeue error")
if !q.isEmpty() {
t.Fatalf("IsEmpty error")
}
if q.detach() != nil {
t.Fatalf("Dequeue error")
}
} }
func TestLoopQueue(t *testing.T) { func TestLoopQueue(t *testing.T) {
@ -31,17 +25,10 @@ func TestLoopQueue(t *testing.T) {
break break
} }
} }
assert.EqualValues(t, 5, q.len(), "Len error")
if q.len() != 5 {
t.Fatalf("Len error")
}
v := q.detach() v := q.detach()
t.Log(v) t.Log(v)
assert.EqualValues(t, 4, q.len(), "Len error")
if q.len() != 4 {
t.Fatalf("Len error")
}
time.Sleep(time.Second) time.Sleep(time.Second)
@ -51,19 +38,11 @@ func TestLoopQueue(t *testing.T) {
break break
} }
} }
assert.EqualValues(t, 10, q.len(), "Len error")
if q.len() != 10 {
t.Fatalf("Len error")
}
err := q.insert(&goWorker{recycleTime: time.Now()}) err := q.insert(&goWorker{recycleTime: time.Now()})
if err == nil { assert.Error(t, err, "Enqueue, error")
t.Fatalf("Enqueue error")
}
q.retrieveExpiry(time.Second) q.retrieveExpiry(time.Second)
assert.EqualValuesf(t, 6, q.len(), "Len error: %d", q.len())
if q.len() != 6 {
t.Fatalf("Len error: %d", q.len())
}
} }

View File

@ -5,22 +5,16 @@ package ants
import ( import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
) )
func TestNewWorkerStack(t *testing.T) { func TestNewWorkerStack(t *testing.T) {
size := 100 size := 100
q := newWorkerStack(size) q := newWorkerStack(size)
if q.len() != 0 { assert.EqualValues(t, 0, q.len(), "Len error")
t.Fatal("Len error") assert.Equal(t, true, q.isEmpty(), "IsEmpty error")
} assert.Nil(t, q.detach(), "Dequeue error")
if !q.isEmpty() {
t.Fatal("IsEmpty error")
}
if q.detach() != nil {
t.Fatal("Dequeue error")
}
} }
func TestWorkerStack(t *testing.T) { func TestWorkerStack(t *testing.T) {
@ -32,9 +26,7 @@ func TestWorkerStack(t *testing.T) {
break break
} }
} }
if q.len() != 5 { assert.EqualValues(t, 5, q.len(), "Len error")
t.Fatal("Len error")
}
expired := time.Now() expired := time.Now()
@ -51,16 +43,9 @@ func TestWorkerStack(t *testing.T) {
t.Fatal("Enqueue error") t.Fatal("Enqueue error")
} }
} }
assert.EqualValues(t, 12, q.len(), "Len error")
if q.len() != 12 {
t.Fatal("Len error")
}
q.retrieveExpiry(time.Second) q.retrieveExpiry(time.Second)
assert.EqualValues(t, 6, q.len(), "Len error")
if q.len() != 6 {
t.Fatal("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 // 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()}) _ = q.insert(&goWorker{recycleTime: time.Now()})
index := q.binarySearch(0, q.len()-1, time.Now()) assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, time.Now()), "index should be 0")
if index != 0 { assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
t.Fatal("index should be 0")
}
index = q.binarySearch(0, q.len()-1, expiry1)
if index != -1 {
t.Fatal("index should be -1")
}
// 2 // 2
expiry2 := time.Now() expiry2 := time.Now()
_ = q.insert(&goWorker{recycleTime: time.Now()}) _ = q.insert(&goWorker{recycleTime: time.Now()})
index = q.binarySearch(0, q.len()-1, expiry1) assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
if index != -1 {
t.Fatal("index should be -1")
}
index = q.binarySearch(0, q.len()-1, expiry2) assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, expiry2), "index should be 0")
if index != 0 {
t.Fatal("index should be 0")
}
index = q.binarySearch(0, q.len()-1, time.Now()) assert.EqualValues(t, 1, q.binarySearch(0, q.len()-1, time.Now()), "index should be 1")
if index != 1 {
t.Fatal("index should be 1")
}
// more // more
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
@ -115,8 +84,5 @@ func TestSearch(t *testing.T) {
_ = q.insert(&goWorker{recycleTime: time.Now()}) _ = q.insert(&goWorker{recycleTime: time.Now()})
} }
index = q.binarySearch(0, q.len()-1, expiry3) assert.EqualValues(t, 7, q.binarySearch(0, q.len()-1, expiry3), "index should be 7")
if index != 7 {
t.Fatal("index should be 7")
}
} }