github.com/lingyao2333/mo-zero@v1.4.1/core/threading/routinegroup_test.go (about)

     1  package threading
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  	"sync"
     7  	"sync/atomic"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRoutineGroupRun(t *testing.T) {
    14  	var count int32
    15  	group := NewRoutineGroup()
    16  	for i := 0; i < 3; i++ {
    17  		group.Run(func() {
    18  			atomic.AddInt32(&count, 1)
    19  		})
    20  	}
    21  
    22  	group.Wait()
    23  
    24  	assert.Equal(t, int32(3), count)
    25  }
    26  
    27  func TestRoutingGroupRunSafe(t *testing.T) {
    28  	log.SetOutput(io.Discard)
    29  
    30  	var count int32
    31  	group := NewRoutineGroup()
    32  	var once sync.Once
    33  	for i := 0; i < 3; i++ {
    34  		group.RunSafe(func() {
    35  			once.Do(func() {
    36  				panic("")
    37  			})
    38  			atomic.AddInt32(&count, 1)
    39  		})
    40  	}
    41  
    42  	group.Wait()
    43  
    44  	assert.Equal(t, int32(2), count)
    45  }