github.com/songzhibin97/gkit@v1.2.13/goroutine/goroutine_test.go (about)

     1  package goroutine
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/songzhibin97/gkit/log"
    10  )
    11  
    12  func TestNewGoroutine(t *testing.T) {
    13  	g := NewGoroutine(context.Background(), SetMax(10), SetLogger(log.DefaultLogger))
    14  	for i := 0; i < 20; i++ {
    15  		i := i
    16  		fmt.Println(g.AddTask(func() {
    17  			//if rand.Int31n(10) > 5 {
    18  			//	panic(i)
    19  			//}
    20  			fmt.Println("start:", i)
    21  			time.Sleep(5 * time.Second)
    22  			fmt.Println("end:", i)
    23  		}))
    24  		g.Trick()
    25  		if i == 7 {
    26  			g.ChangeMax(5)
    27  		}
    28  	}
    29  	_ = g.Shutdown()
    30  }
    31  
    32  func TestSetIdle(t *testing.T) {
    33  	g := NewGoroutine(context.Background(), SetMax(1000), SetIdle(10), SetCheckTime(time.Second), SetLogger(log.DefaultLogger))
    34  	for i := 0; i < 10000; i++ {
    35  		g.AddTask(func() {
    36  			func(i int) {
    37  				time.Sleep(time.Second)
    38  				// t.Log("close", i)
    39  			}(i)
    40  		})
    41  	}
    42  	for i := 0; i < 20; i++ {
    43  		g.Trick()
    44  		time.Sleep(time.Second)
    45  	}
    46  }