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.
 
 

46 lines
820 B

package group
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
func TestConcurrentMap(t *testing.T) {
cm := NewConcurrentMap[string, string](16, 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()
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()
}