go-zero/core/syncx/once_test.go

34 lines
374 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package syncx
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestOnce(t *testing.T) {
var v int
add := Once(func() {
v++
})
for i := 0; i < 5; i++ {
add()
}
assert.Equal(t, 1, v)
}
func BenchmarkOnce(b *testing.B) {
var v int
add := Once(func() {
v++
})
2020-08-10 14:53:01 +08:00
b.ResetTimer()
2020-08-10 14:53:01 +08:00
for i := 0; i < b.N; i++ {
add()
}
assert.Equal(b, 1, v)
2020-08-10 14:53:01 +08:00
}