Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ To use Gool, you need to define:
- Handler function: ```handler func(A) R```, and
- Argument: ```arg A```

You can also specify the number of workers ```numWorkers``` and the task queue size ```cap``` when creating a new pool.
With types ```A``` and ```R``` being arbitrary types.

With types ```A``` and ```R``` being arbitrary types.
You can also specify the number of workers ```numWorkers``` and the task queue size ```cap``` when creating a new pool.
75 changes: 60 additions & 15 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package gool

import (
"runtime"
"testing"
)

Expand All @@ -41,11 +42,11 @@ func BenchmarkNormalGoroutine(b *testing.B) {
}

func BenchmarkPool(b *testing.B) {
p := NewPool[interface{}, interface{}](4, 100)
p := NewPool[any, any](4, 100)
defer p.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Submit(func(interface{}) interface{} {
p.Submit(func(any) any {
for k := 0; k < 100; k++ {
_ = k
}
Expand All @@ -56,8 +57,8 @@ func BenchmarkPool(b *testing.B) {

func TestPool_Submit(t *testing.T) {
type args struct {
handler func(interface{}) interface{}
args interface{}
handler func(any) any
args any
}
tests := []struct {
name string
Expand All @@ -66,7 +67,7 @@ func TestPool_Submit(t *testing.T) {
{
name: "test",
args: args{
handler: func(arg interface{}) interface{} {
handler: func(arg any) any {
for k := 0; k < 100; k++ {
_ = k
}
Expand All @@ -78,16 +79,16 @@ func TestPool_Submit(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewPool[interface{}, interface{}](10, 100)
p := NewPool[any, any](10, 100)
p.Submit(tt.args.handler, tt.args.args)
})
}
}

func TestPool_AsyncSubmit(t *testing.T) {
type args struct {
handler func(interface{}) interface{}
args interface{}
handler func(any) any
args any
}
tests := []struct {
name string
Expand All @@ -96,7 +97,7 @@ func TestPool_AsyncSubmit(t *testing.T) {
{
name: "test",
args: args{
handler: func(arg interface{}) interface{} {
handler: func(arg any) any {
for k := 0; k < 100; k++ {
_ = k
}
Expand All @@ -107,15 +108,15 @@ func TestPool_AsyncSubmit(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewPool[interface{}, interface{}](10, 100)
p := NewPool[any, any](10, 100)
p.AsyncSubmit(tt.args.handler, tt.args.args)
})
}
}
func TestPool_Map(t *testing.T) {
type args struct {
handler func(interface{}) interface{}
args []interface{}
handler func(any) any
args []any
}
tests := []struct {
name string
Expand All @@ -124,21 +125,65 @@ func TestPool_Map(t *testing.T) {
{
name: "test",
args: args{
handler: func(arg interface{}) interface{} {
handler: func(arg any) any {
for k := 0; k < 100; k++ {
_ = k
}
return nil
},
args: []interface{}{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
args: []any{nil, nil, nil, nil, nil, nil, nil, nil, nil, nil},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewPool[interface{}, interface{}](10, 100)
p := NewPool[any, any](10, 100)
defer p.Close()
p.Map(tt.args.handler, tt.args.args)
})
}
}

func TestNewPool(t *testing.T) {
type args struct {
numWorkers int
cap int
}
type testCase[A any, R any] struct {
name string
args args
want *Pool[A, R]
}
tests := []testCase[any, any]{
{
name: "test_num_workers_0",
args: args{
numWorkers: 0,
cap: 100,
},
want: &Pool[any, any]{
numWorkers: runtime.NumCPU(),
taskChan: make(chan task[any, any], 100),
},
},
{
name: "test_cap_0",
args: args{
numWorkers: 10,
cap: 0,
},
want: &Pool[any, any]{
numWorkers: 10,
taskChan: make(chan task[any, any], 20),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewPool[any, any](tt.args.numWorkers, tt.args.cap); cap(got.taskChan) != cap(tt.want.taskChan) ||
got.numWorkers != tt.want.numWorkers {
t.Errorf("NewPool() = %v, want %v", got, tt.want)
}
})
}
}