Skip to content

Commit

Permalink
Add Unit Tests (#5)
Browse files Browse the repository at this point in the history
* Add unit tests.

Signed-off-by: txaty <xtianae@connect.ust.hk>

* Add unit tests.

Signed-off-by: txaty <xtianae@connect.ust.hk>

---------

Signed-off-by: txaty <xtianae@connect.ust.hk>
  • Loading branch information
txaty authored Mar 9, 2023
1 parent 00b9352 commit 94e672c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
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)
}
})
}
}

0 comments on commit 94e672c

Please sign in to comment.