2019-10-09 19:59:19 +03:00
|
|
|
package ants
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewLoopQueue(t *testing.T) {
|
|
|
|
size := 100
|
2019-10-09 22:02:04 +03:00
|
|
|
q := newWorkerLoopQueue(size)
|
2019-10-09 19:59:19 +03:00
|
|
|
if q.len() != 0 {
|
|
|
|
t.Fatalf("Len error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !q.isEmpty() {
|
|
|
|
t.Fatalf("IsEmpty error")
|
|
|
|
}
|
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
if q.detach() != nil {
|
2019-10-09 19:59:19 +03:00
|
|
|
t.Fatalf("Dequeue error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.len() != 5 {
|
|
|
|
t.Fatalf("Len error")
|
|
|
|
}
|
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
v := q.detach()
|
2019-10-09 19:59:19 +03:00
|
|
|
t.Log(v)
|
|
|
|
|
|
|
|
if q.len() != 4 {
|
|
|
|
t.Fatalf("Len error")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.len() != 10 {
|
|
|
|
t.Fatalf("Len error")
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Fatalf("Enqueue error")
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
q.retrieveExpiry(time.Second)
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
if q.len() != 6 {
|
|
|
|
t.Fatalf("Len error: %d", q.len())
|
|
|
|
}
|
|
|
|
}
|