Files
waf-platform/EdgeNode/internal/utils/sync/values_test.go

59 lines
1.2 KiB
Go

// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package syncutils_test
import (
syncutils "github.com/TeaOSLab/EdgeNode/internal/utils/sync"
"math/rand"
"runtime"
"testing"
)
func TestNewValues_Int(t *testing.T) {
var value = -1
var values = syncutils.NewValues[int](func() int {
value++
return value
})
t.Log(values.Len())
values.DoRead(func(value int) {
t.Log("value at 1:", value)
})
values.DoWrite(func(value int) {
t.Log("value at 2:", value)
})
}
func TestNewValues_Map(t *testing.T) {
var values = syncutils.NewValues[map[int]bool](func() map[int]bool {
return map[int]bool{}
})
t.Log(values.Len())
values.DoWrite(func(value map[int]bool) {
value[123] = true
value[456] = false
})
values.DoRead(func(value map[int]bool) {
t.Log("value at 0:", value)
})
}
func BenchmarkValues_Map(b *testing.B) {
runtime.GOMAXPROCS(32)
var values = syncutils.NewValues[map[int]bool](func() map[int]bool {
return map[int]bool{}
})
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
values.DoWrite(func(value map[int]bool) {
value[rand.Int()%10_000] = true
})
}
})
}