2019-10-09 19:59:19 +03:00
|
|
|
package ants
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewWorkerStack(t *testing.T) {
|
|
|
|
size := 100
|
|
|
|
q := newWorkerStack(size)
|
|
|
|
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 TestWorkerStack(t *testing.T) {
|
2019-10-09 22:20:10 +03:00
|
|
|
q := newWorkerArray(arrayType(-1), 0)
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
expired := time.Now()
|
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
err := q.insert(&goWorker{recycleTime: expired})
|
2019-10-09 19:59:19 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Enqueue 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 {
|
|
|
|
t.Fatalf("Enqueue error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.len() != 12 {
|
|
|
|
t.Fatalf("Len 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSearch(t *testing.T) {
|
|
|
|
q := newWorkerStack(0)
|
|
|
|
|
|
|
|
// 1
|
|
|
|
expiry1 := time.Now()
|
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
2019-10-09 19:59:19 +03:00
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
index := q.binarySearch(0, q.len()-1, time.Now())
|
2019-10-09 19:59:19 +03:00
|
|
|
if index != 0 {
|
|
|
|
t.Fatalf("should is 0")
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
index = q.binarySearch(0, q.len()-1, expiry1)
|
2019-10-09 19:59:19 +03:00
|
|
|
if index != -1 {
|
|
|
|
t.Fatalf("should is -1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2
|
|
|
|
expiry2 := time.Now()
|
2019-10-09 22:02:04 +03:00
|
|
|
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
2019-10-09 19:59:19 +03:00
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
index = q.binarySearch(0, q.len()-1, expiry1)
|
2019-10-09 19:59:19 +03:00
|
|
|
if index != -1 {
|
|
|
|
t.Fatalf("should is -1")
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
index = q.binarySearch(0, q.len()-1, expiry2)
|
2019-10-09 19:59:19 +03:00
|
|
|
if index != 0 {
|
|
|
|
t.Fatalf("should is 0")
|
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
index = q.binarySearch(0, q.len()-1, time.Now())
|
2019-10-09 19:59:19 +03:00
|
|
|
if index != 1 {
|
|
|
|
t.Fatalf("should is 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
// more
|
|
|
|
for i := 0; i < 5; i++ {
|
2019-10-09 22:02:04 +03:00
|
|
|
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
expiry3 := time.Now()
|
|
|
|
|
2019-10-09 22:02:04 +03:00
|
|
|
_ = q.insert(&goWorker{recycleTime: expiry3})
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2019-10-09 22:02:04 +03:00
|
|
|
_ = q.insert(&goWorker{recycleTime: time.Now()})
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
2019-10-20 13:22:13 +03:00
|
|
|
index = q.binarySearch(0, q.len()-1, expiry3)
|
2019-10-09 19:59:19 +03:00
|
|
|
if index != 7 {
|
|
|
|
t.Fatalf("should is 7")
|
|
|
|
}
|
|
|
|
}
|