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() }