github.com/zhongdalu/gf@v1.0.0/g/os/grpool/grpool_unit_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/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/zhongdalu/gf/g/container/garray"
    17  	"github.com/zhongdalu/gf/g/os/grpool"
    18  	"github.com/zhongdalu/gf/g/test/gtest"
    19  )
    20  
    21  func Test_Basic(t *testing.T) {
    22  	gtest.Case(t, func() {
    23  		wg := sync.WaitGroup{}
    24  		array := garray.NewArray()
    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  		gtest.Assert(array.Len(), size)
    36  		gtest.Assert(grpool.Jobs(), 0)
    37  		gtest.Assert(grpool.Size(), 0)
    38  	})
    39  }
    40  
    41  func Test_Limit1(t *testing.T) {
    42  	gtest.Case(t, func() {
    43  		wg := sync.WaitGroup{}
    44  		array := garray.NewArray()
    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  		gtest.Assert(array.Len(), size)
    56  	})
    57  }
    58  
    59  func Test_Limit2(t *testing.T) {
    60  	gtest.Case(t, func() {
    61  		wg := sync.WaitGroup{}
    62  		array := garray.NewArray()
    63  		size := 100
    64  		pool := grpool.New(1)
    65  		wg.Add(size)
    66  		for i := 0; i < size; i++ {
    67  			pool.Add(func() {
    68  				array.Append(1)
    69  				wg.Done()
    70  			})
    71  		}
    72  		wg.Wait()
    73  		gtest.Assert(array.Len(), size)
    74  	})
    75  }
    76  
    77  func Test_Limit3(t *testing.T) {
    78  	gtest.Case(t, func() {
    79  		array := garray.NewArray()
    80  		size := 1000
    81  		pool := grpool.New(100)
    82  		gtest.Assert(pool.Cap(), 100)
    83  		for i := 0; i < size; i++ {
    84  			pool.Add(func() {
    85  				array.Append(1)
    86  				time.Sleep(2 * time.Second)
    87  			})
    88  		}
    89  		time.Sleep(time.Second)
    90  		gtest.Assert(pool.Size(), 100)
    91  		gtest.Assert(pool.Jobs(), 900)
    92  		gtest.Assert(array.Len(), 100)
    93  		pool.Close()
    94  		time.Sleep(2 * time.Second)
    95  		gtest.Assert(pool.Size(), 0)
    96  		gtest.Assert(pool.Jobs(), 900)
    97  		gtest.Assert(array.Len(), 100)
    98  		gtest.Assert(pool.IsClosed(), true)
    99  		gtest.AssertNE(pool.Add(func() {}), nil)
   100  
   101  	})
   102  }