2019-10-09 19:59:19 +03:00
|
|
|
package ants
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-01-08 05:53:23 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-10-09 19:59:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewLoopQueue(t *testing.T) {
|
|
|
|
size := 100
|
2019-10-09 22:02:04 +03:00
|
|
|
q := newWorkerLoopQueue(size)
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 0, q.len(), "Len error")
|
|
|
|
assert.Equal(t, true, q.isEmpty(), "IsEmpty error")
|
|
|
|
assert.Nil(t, q.detach(), "Dequeue error")
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoopQueue(t *testing.T) {
|
|
|
|
size := 10
|
2019-10-09 22:02:04 +03:00
|
|
|
q := newWorkerLoopQueue(size)
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
2019-10-09 22:02:04 +03:00
|
|
|
err := q.insert(&goWorker{recycleTime: time.Now()})
|
2019-10-09 19:59:19 +03:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 5, q.len(), "Len error")
|
2019-10-09 22:02:04 +03:00
|
|
|
v := q.detach()
|
2019-10-09 19:59:19 +03:00
|
|
|
t.Log(v)
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 4, q.len(), "Len error")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
for i := 0; i < 6; i++ {
|
2019-10-09 22:02:04 +03:00
|
|
|
err := q.insert(&goWorker{recycleTime: time.Now()})
|
2019-10-09 19:59:19 +03:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 10, q.len(), "Len error")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
err := q.insert(&goWorker{recycleTime: time.Now()})
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.Error(t, err, "Enqueue, error")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
q.retrieveExpiry(time.Second)
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValuesf(t, 6, q.len(), "Len error: %d", q.len())
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|