github.com/gogf/gf@v1.16.9/os/grpool/grpool_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  // go test *.go -bench=".*" -count=1
     8  
     9  package grpool_test
    10  
    11  import (
    12  	"sync"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/gogf/gf/container/garray"
    17  	"github.com/gogf/gf/os/grpool"
    18  	"github.com/gogf/gf/test/gtest"
    19  )
    20  
    21  func Test_Basic(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		wg := sync.WaitGroup{}
    24  		array := garray.NewArray(true)
    25  		size := 100
    26  		wg.Add(size)
    27  		for i := 0; i < size; i++ {
    28  			grpool.Add(func() {
    29  				array.Append(1)
    30  				wg.Done()
    31  			})
    32  		}
    33  		wg.Wait()
    34  		time.Sleep(100 * time.Millisecond)
    35  		t.Assert(array.Len(), size)
    36  		t.Assert(grpool.Jobs(), 0)
    37  		t.Assert(grpool.Size(), 0)
    38  	})
    39  }
    40  
    41  func Test_Limit1(t *testing.T) {
    42  	gtest.C(t, func(t *gtest.T) {
    43  		wg := sync.WaitGroup{}
    44  		array := garray.NewArray(true)
    45  		size := 100
    46  		pool := grpool.New(10)
    47  		wg.Add(size)
    48  		for i := 0; i < size; i++ {
    49  			pool.Add(func() {
    50  				array.Append(1)
    51  				wg.Done()
    52  			})
    53  		}
    54  		wg.Wait()
    55  		t.Assert(array.Len(), size)
    56  	})
    57  }
    58  
    59  func Test_Limit2(t *testing.T) {
    60  	gtest.C(t, func(t *gtest.T) {
    61  		var (
    62  			wg    = sync.WaitGroup{}
    63  			array = garray.NewArray(true)
    64  			size  = 100
    65  			pool  = grpool.New(1)
    66  		)
    67  		wg.Add(size)
    68  		for i := 0; i < size; i++ {
    69  			pool.Add(func() {
    70  				defer wg.Done()
    71  				array.Append(1)
    72  			})
    73  		}
    74  		wg.Wait()
    75  		t.Assert(array.Len(), size)
    76  	})
    77  }
    78  
    79  func Test_Limit3(t *testing.T) {
    80  	gtest.C(t, func(t *gtest.T) {
    81  		array := garray.NewArray(true)
    82  		size := 1000
    83  		pool := grpool.New(100)
    84  		t.Assert(pool.Cap(), 100)
    85  		for i := 0; i < size; i++ {
    86  			pool.Add(func() {
    87  				array.Append(1)
    88  				time.Sleep(2 * time.Second)
    89  			})
    90  		}
    91  		time.Sleep(time.Second)
    92  		t.Assert(pool.Size(), 100)
    93  		t.Assert(pool.Jobs(), 900)
    94  		t.Assert(array.Len(), 100)
    95  		pool.Close()
    96  		time.Sleep(2 * time.Second)
    97  		t.Assert(pool.Size(), 0)
    98  		t.Assert(pool.Jobs(), 900)
    99  		t.Assert(array.Len(), 100)
   100  		t.Assert(pool.IsClosed(), true)
   101  		t.AssertNE(pool.Add(func() {}), nil)
   102  	})
   103  }
   104  
   105  func Test_AddWithRecover(t *testing.T) {
   106  	gtest.C(t, func(t *gtest.T) {
   107  		array := garray.NewArray(true)
   108  		grpool.AddWithRecover(func() {
   109  			array.Append(1)
   110  			panic(1)
   111  		}, func(err error) {
   112  			array.Append(1)
   113  		})
   114  		grpool.AddWithRecover(func() {
   115  			panic(1)
   116  			array.Append(1)
   117  		})
   118  		time.Sleep(500 * time.Millisecond)
   119  		t.Assert(array.Len(), 2)
   120  	})
   121  }