2024-01-06 01:50:20 +08:00
|
|
|
package pool
|
|
|
|
|
|
|
|
import (
|
2024-01-28 18:16:04 +08:00
|
|
|
"errors"
|
2024-01-06 01:50:20 +08:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTaskPool_Stop(t *testing.T) {
|
|
|
|
pool := NewTaskPool(5, 10)
|
|
|
|
pool.Start()
|
|
|
|
|
|
|
|
lck := sync.Mutex{}
|
|
|
|
counter := 0
|
|
|
|
|
|
|
|
for i := 1; i <= 10; i++ {
|
2024-02-15 12:53:57 +08:00
|
|
|
f := func(i int) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
2024-01-06 01:50:20 +08:00
|
|
|
lck.Lock()
|
|
|
|
t.Log("task", i, "locked")
|
|
|
|
counter += i
|
|
|
|
t.Log("task", i, "unlocked")
|
|
|
|
lck.Unlock()
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(i*100) * time.Millisecond)
|
|
|
|
t.Log("task", i, "finished")
|
2024-01-28 18:16:04 +08:00
|
|
|
|
2024-02-15 12:53:57 +08:00
|
|
|
return nil, nil
|
2024-01-06 01:50:20 +08:00
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
pool.AddTask(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
|
|
|
|
if counter != 55 {
|
|
|
|
t.Error("some tasks were not executed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTaskPool_WaitForTask(t *testing.T) {
|
|
|
|
pool := NewTaskPool(10, 10)
|
|
|
|
pool.Start()
|
|
|
|
|
|
|
|
counter := 0
|
|
|
|
|
|
|
|
for i := 1; i <= 10; i++ {
|
2024-02-15 12:53:57 +08:00
|
|
|
f := func(i int) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
2024-01-06 01:50:20 +08:00
|
|
|
counter += 1
|
|
|
|
t.Log("task", i, "finished")
|
2024-01-28 22:02:26 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2024-02-15 12:53:57 +08:00
|
|
|
return i, nil
|
2024-01-06 01:50:20 +08:00
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
id := pool.AddTask(f)
|
|
|
|
|
2024-01-28 18:16:04 +08:00
|
|
|
ret := pool.WaitForTask(id)
|
2024-01-06 01:50:20 +08:00
|
|
|
if counter != 1 {
|
|
|
|
t.Errorf("Counter mismatch: expected %d, got %d, task %d", 1, counter, id)
|
|
|
|
}
|
2024-02-15 12:53:57 +08:00
|
|
|
if ret.Error != nil {
|
|
|
|
t.Errorf("Return error: %v, task %d", ret.Error, id)
|
|
|
|
}
|
|
|
|
if ret.Value.(int) != i {
|
|
|
|
t.Errorf("Return value mismatch: expected %d, got %v, task %d", i, ret, id)
|
2024-01-28 18:16:04 +08:00
|
|
|
}
|
2024-01-06 01:50:20 +08:00
|
|
|
counter -= 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
}
|
2024-01-06 21:03:30 +08:00
|
|
|
|
2024-01-28 22:02:26 +08:00
|
|
|
func TestTaskPool_DoubleWait(t *testing.T) {
|
|
|
|
pool := NewTaskPool(1, 1)
|
|
|
|
pool.Start()
|
|
|
|
|
2024-02-15 12:53:57 +08:00
|
|
|
f := func() (interface{}, error) {
|
2024-01-28 22:02:26 +08:00
|
|
|
t.Log("task invoked")
|
2024-02-15 12:53:57 +08:00
|
|
|
return nil, nil
|
2024-01-28 22:02:26 +08:00
|
|
|
}
|
|
|
|
id := pool.AddTask(f)
|
|
|
|
|
|
|
|
ret := pool.WaitForTask(id)
|
2024-02-15 12:53:57 +08:00
|
|
|
if ret.Error != nil {
|
2024-01-28 22:02:26 +08:00
|
|
|
t.Errorf("task returned error: %v", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
ret2 := pool.WaitForTask(id)
|
2024-02-15 12:53:57 +08:00
|
|
|
if ret2.Error == nil {
|
2024-01-28 22:02:26 +08:00
|
|
|
t.Errorf("2nd wait returned nil")
|
2024-02-15 12:53:57 +08:00
|
|
|
} else if !errors.Is(ret2.Error, &ErrTaskNotFound{}) {
|
2024-01-28 22:02:26 +08:00
|
|
|
t.Errorf("2nd wait returned wrong error: %v", ret2)
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
}
|
|
|
|
|
2024-01-06 21:03:30 +08:00
|
|
|
func TestTaskPool_One(t *testing.T) {
|
|
|
|
pool := NewTaskPool(1, 1)
|
|
|
|
pool.Start()
|
|
|
|
|
|
|
|
lck := sync.Mutex{}
|
|
|
|
counter := 0
|
|
|
|
|
2024-02-15 12:53:57 +08:00
|
|
|
ids := make([]uint64, 0)
|
2024-01-06 21:03:30 +08:00
|
|
|
for i := 1; i <= 10; i++ {
|
2024-02-15 12:53:57 +08:00
|
|
|
f := func(i int) func() (interface{}, error) {
|
|
|
|
return func() (interface{}, error) {
|
2024-01-06 21:03:30 +08:00
|
|
|
lck.Lock()
|
|
|
|
t.Log("task", i, "locked")
|
|
|
|
counter += i
|
|
|
|
t.Log("task", i, "unlocked")
|
|
|
|
lck.Unlock()
|
|
|
|
|
|
|
|
time.Sleep(time.Duration(i*10) * time.Millisecond)
|
|
|
|
t.Log("task", i, "finished")
|
2024-01-28 18:16:04 +08:00
|
|
|
|
2024-02-15 12:53:57 +08:00
|
|
|
return nil, nil
|
2024-01-06 21:03:30 +08:00
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
id := pool.AddTask(f)
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range ids {
|
2024-01-28 18:16:04 +08:00
|
|
|
_ = pool.WaitForTask(id)
|
2024-01-06 21:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if counter != 55 {
|
|
|
|
t.Error("some tasks were not executed")
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.Stop()
|
|
|
|
}
|