2022-05-07 17:43:25 +03:00
|
|
|
//go:build !windows
|
2019-11-15 08:35:32 +03:00
|
|
|
// +build !windows
|
|
|
|
|
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 TestNewWorkerStack(t *testing.T) {
|
|
|
|
size := 100
|
|
|
|
q := newWorkerStack(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 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
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 5, q.len(), "Len error")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
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 {
|
2019-11-15 08:35:32 +03:00
|
|
|
t.Fatal("Enqueue 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 {
|
2019-11-15 08:35:32 +03:00
|
|
|
t.Fatal("Enqueue error")
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 12, q.len(), "Len error")
|
2019-10-20 13:22:13 +03:00
|
|
|
q.retrieveExpiry(time.Second)
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 6, q.len(), "Len error")
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|
|
|
|
|
2020-04-08 08:16:14 +03:00
|
|
|
// It seems that something wrong with time.Now() on Windows, not sure whether it is a bug on Windows,
|
|
|
|
// so exclude this test from Windows platform temporarily.
|
2019-10-09 19:59:19 +03:00
|
|
|
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
|
|
|
|
2020-01-08 05:53:23 +03:00
|
|
|
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")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, -1, q.binarySearch(0, q.len()-1, expiry1), "index should be -1")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 0, q.binarySearch(0, q.len()-1, expiry2), "index should be 0")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 1, q.binarySearch(0, q.len()-1, time.Now()), "index should be 1")
|
2019-10-09 19:59:19 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-01-08 05:53:23 +03:00
|
|
|
assert.EqualValues(t, 7, q.binarySearch(0, q.len()-1, expiry3), "index should be 7")
|
2019-10-09 19:59:19 +03:00
|
|
|
}
|