You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 lines
1.0 KiB

package collect
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
func TestConcurrentMap(t *testing.T) {
cm := NewConcurrentMap[string, string](19, func(k string) string { return k })
concurrent := 1000
wg := sync.WaitGroup{}
wg.Add(concurrent)
for i := 0; i < concurrent; i++ {
go func(loop int) {
for j := 0; j < concurrent; j++ {
k := fmt.Sprintf("%d-%d", loop, j)
cm.Store(k, k)
}
wg.Done()
}(i)
}
wg.Wait()
var sum int
cm.Range(func(key, value string) bool {
sum++
if key != value {
t.Errorf("value load error: %s:%s", key, value)
}
return true
})
if sum != (concurrent * concurrent) {
t.Errorf("count error: %d", sum)
}
wg.Add(concurrent)
for i := 0; i < concurrent; i++ {
go func() {
r := rand.New(rand.NewSource(time.Now().UnixMilli()))
for i := 0; i < concurrent; i++ {
k := fmt.Sprintf("%d-%d", r.Intn(concurrent), r.Intn(concurrent))
v, ok := cm.Load(k)
if !ok || v != k {
t.Errorf("value load error: %s", k)
}
}
wg.Done()
}()
}
wg.Wait()
}