Files
gf/os/grpool/grpool_z_unit_test.go

140 lines
2.8 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-05-31 22:54:57 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package grpool_test
import (
"context"
2019-05-31 22:54:57 +08:00
"sync"
"testing"
"time"
2019-06-24 15:01:12 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/os/grpool"
"github.com/gogf/gf/v2/test/gtest"
2019-05-31 22:54:57 +08:00
)
func Test_Basic(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
err error
wg = sync.WaitGroup{}
array = garray.NewArray(true)
size = 100
)
2019-05-31 22:54:57 +08:00
wg.Add(size)
for i := 0; i < size; i++ {
err = grpool.Add(ctx, func(ctx context.Context) {
2019-05-31 22:54:57 +08:00
array.Append(1)
wg.Done()
})
t.AssertNil(err)
2019-05-31 22:54:57 +08:00
}
wg.Wait()
time.Sleep(100 * time.Millisecond)
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), size)
t.Assert(grpool.Jobs(), 0)
t.Assert(grpool.Size(), 0)
2019-05-31 22:54:57 +08:00
})
2019-06-01 15:11:32 +08:00
}
func Test_Limit1(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
wg = sync.WaitGroup{}
array = garray.NewArray(true)
size = 100
pool = grpool.New(10)
)
2019-06-01 15:11:32 +08:00
wg.Add(size)
for i := 0; i < size; i++ {
pool.Add(ctx, func(ctx context.Context) {
2019-06-01 15:11:32 +08:00
array.Append(1)
wg.Done()
})
}
wg.Wait()
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), size)
2019-06-01 15:11:32 +08:00
})
}
2019-05-31 22:54:57 +08:00
2019-06-01 15:11:32 +08:00
func Test_Limit2(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
err error
wg = sync.WaitGroup{}
array = garray.NewArray(true)
size = 100
pool = grpool.New(1)
)
2019-06-01 15:11:32 +08:00
wg.Add(size)
for i := 0; i < size; i++ {
err = pool.Add(ctx, func(ctx context.Context) {
defer wg.Done()
2019-06-01 15:11:32 +08:00
array.Append(1)
})
t.AssertNil(err)
2019-06-01 15:11:32 +08:00
}
wg.Wait()
2020-03-19 22:56:12 +08:00
t.Assert(array.Len(), size)
2019-06-01 15:11:32 +08:00
})
}
func Test_Limit3(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
var (
array = garray.NewArray(true)
size = 1000
pool = grpool.New(100)
)
2020-03-19 22:56:12 +08:00
t.Assert(pool.Cap(), 100)
2019-05-31 22:54:57 +08:00
for i := 0; i < size; i++ {
pool.Add(ctx, func(ctx context.Context) {
2019-05-31 22:54:57 +08:00
array.Append(1)
2019-06-19 09:06:52 +08:00
time.Sleep(2 * time.Second)
2019-05-31 22:54:57 +08:00
})
}
time.Sleep(time.Second)
2020-03-19 22:56:12 +08:00
t.Assert(pool.Size(), 100)
t.Assert(pool.Jobs(), 900)
t.Assert(array.Len(), 100)
2019-05-31 22:54:57 +08:00
pool.Close()
2019-06-19 09:06:52 +08:00
time.Sleep(2 * time.Second)
2020-03-19 22:56:12 +08:00
t.Assert(pool.Size(), 0)
t.Assert(pool.Jobs(), 900)
t.Assert(array.Len(), 100)
t.Assert(pool.IsClosed(), true)
t.AssertNE(pool.Add(ctx, func(ctx context.Context) {}), nil)
})
}
2019-06-24 15:01:12 +08:00
func Test_AddWithRecover(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
err error
array = garray.NewArray(true)
)
err = grpool.AddWithRecover(ctx, func(ctx context.Context) {
array.Append(1)
panic(1)
2022-06-20 20:34:59 +08:00
}, func(ctx context.Context, err error) {
array.Append(1)
})
t.AssertNil(err)
err = grpool.AddWithRecover(ctx, func(ctx context.Context) {
panic(1)
array.Append(1)
}, nil)
t.AssertNil(err)
time.Sleep(500 * time.Millisecond)
t.Assert(array.Len(), 2)
2019-05-31 22:54:57 +08:00
})
2019-06-19 09:06:52 +08:00
}