github.com/gogf/gf/v2@v2.7.4/os/grpool/grpool_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package grpool_test 8 9 import ( 10 "context" 11 "sync" 12 "testing" 13 "time" 14 15 "github.com/gogf/gf/v2/container/garray" 16 "github.com/gogf/gf/v2/os/grpool" 17 "github.com/gogf/gf/v2/test/gtest" 18 ) 19 20 func Test_Basic(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 var ( 23 err error 24 wg = sync.WaitGroup{} 25 array = garray.NewArray(true) 26 size = 100 27 ) 28 wg.Add(size) 29 for i := 0; i < size; i++ { 30 err = grpool.Add(ctx, func(ctx context.Context) { 31 array.Append(1) 32 wg.Done() 33 }) 34 t.AssertNil(err) 35 } 36 wg.Wait() 37 38 time.Sleep(100 * time.Millisecond) 39 40 t.Assert(array.Len(), size) 41 t.Assert(grpool.Jobs(), 0) 42 t.Assert(grpool.Size(), 0) 43 }) 44 } 45 46 func Test_Limit1(t *testing.T) { 47 gtest.C(t, func(t *gtest.T) { 48 var ( 49 wg = sync.WaitGroup{} 50 array = garray.NewArray(true) 51 size = 100 52 pool = grpool.New(10) 53 ) 54 wg.Add(size) 55 for i := 0; i < size; i++ { 56 pool.Add(ctx, func(ctx context.Context) { 57 array.Append(1) 58 wg.Done() 59 }) 60 } 61 wg.Wait() 62 t.Assert(array.Len(), size) 63 }) 64 } 65 66 func Test_Limit2(t *testing.T) { 67 gtest.C(t, func(t *gtest.T) { 68 var ( 69 err error 70 wg = sync.WaitGroup{} 71 array = garray.NewArray(true) 72 size = 100 73 pool = grpool.New(1) 74 ) 75 wg.Add(size) 76 for i := 0; i < size; i++ { 77 err = pool.Add(ctx, func(ctx context.Context) { 78 defer wg.Done() 79 array.Append(1) 80 }) 81 t.AssertNil(err) 82 } 83 wg.Wait() 84 t.Assert(array.Len(), size) 85 }) 86 } 87 88 func Test_Limit3(t *testing.T) { 89 gtest.C(t, func(t *gtest.T) { 90 var ( 91 array = garray.NewArray(true) 92 size = 1000 93 pool = grpool.New(100) 94 ) 95 t.Assert(pool.Cap(), 100) 96 for i := 0; i < size; i++ { 97 pool.Add(ctx, func(ctx context.Context) { 98 array.Append(1) 99 time.Sleep(2 * time.Second) 100 }) 101 } 102 time.Sleep(time.Second) 103 t.Assert(pool.Size(), 100) 104 t.Assert(pool.Jobs(), 900) 105 t.Assert(array.Len(), 100) 106 pool.Close() 107 time.Sleep(2 * time.Second) 108 t.Assert(pool.Size(), 0) 109 t.Assert(pool.Jobs(), 900) 110 t.Assert(array.Len(), 100) 111 t.Assert(pool.IsClosed(), true) 112 t.AssertNE(pool.Add(ctx, func(ctx context.Context) {}), nil) 113 }) 114 } 115 116 func Test_AddWithRecover(t *testing.T) { 117 gtest.C(t, func(t *gtest.T) { 118 var ( 119 err error 120 array = garray.NewArray(true) 121 ) 122 err = grpool.AddWithRecover(ctx, func(ctx context.Context) { 123 array.Append(1) 124 panic(1) 125 }, func(ctx context.Context, err error) { 126 array.Append(1) 127 }) 128 t.AssertNil(err) 129 err = grpool.AddWithRecover(ctx, func(ctx context.Context) { 130 panic(1) 131 array.Append(1) 132 }, nil) 133 t.AssertNil(err) 134 135 time.Sleep(500 * time.Millisecond) 136 137 t.Assert(array.Len(), 2) 138 }) 139 }