2019-05-31 22:54:57 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// go test *.go -bench=".*" -count=1
|
|
|
|
|
|
|
|
package grpool_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-06-24 15:01:12 +08:00
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/container/garray"
|
|
|
|
"github.com/gogf/gf/os/grpool"
|
|
|
|
"github.com/gogf/gf/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) {
|
2019-06-19 09:06:52 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2019-07-23 23:40:57 +08:00
|
|
|
array := garray.NewArray(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
size := 100
|
2019-05-31 22:54:57 +08:00
|
|
|
wg.Add(size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
grpool.Add(func() {
|
|
|
|
array.Append(1)
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2019-06-24 19:34:53 +08:00
|
|
|
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) {
|
2019-06-19 09:06:52 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2019-07-23 23:40:57 +08:00
|
|
|
array := garray.NewArray(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
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(func() {
|
|
|
|
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) {
|
2020-11-29 22:26:16 +08:00
|
|
|
var (
|
|
|
|
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++ {
|
|
|
|
pool.Add(func() {
|
2020-11-29 22:26:16 +08:00
|
|
|
defer wg.Done()
|
2019-06-01 15:11:32 +08:00
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
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) {
|
2019-07-23 23:40:57 +08:00
|
|
|
array := garray.NewArray(true)
|
2019-06-19 09:06:52 +08:00
|
|
|
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(func() {
|
|
|
|
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(func() {}), nil)
|
2020-08-12 23:53:05 +08:00
|
|
|
})
|
|
|
|
}
|
2019-06-24 15:01:12 +08:00
|
|
|
|
2020-08-12 23:53:05 +08:00
|
|
|
func Test_AddWithRecover(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
array := garray.NewArray(true)
|
|
|
|
grpool.AddWithRecover(func() {
|
|
|
|
array.Append(1)
|
|
|
|
panic(1)
|
|
|
|
}, func(err error) {
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
grpool.AddWithRecover(func() {
|
|
|
|
panic(1)
|
|
|
|
array.Append(1)
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|