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

     1  package syncx
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestOnce(t *testing.T) {
    10  	var v int
    11  	add := Once(func() {
    12  		v++
    13  	})
    14  
    15  	for i := 0; i < 5; i++ {
    16  		add()
    17  	}
    18  
    19  	assert.Equal(t, 1, v)
    20  }
    21  
    22  func BenchmarkOnce(b *testing.B) {
    23  	var v int
    24  	add := Once(func() {
    25  		v++
    26  	})
    27  
    28  	b.ResetTimer()
    29  	for i := 0; i < b.N; i++ {
    30  		add()
    31  	}
    32  	assert.Equal(b, 1, v)
    33  }