github.com/polarismesh/polaris@v1.17.8/common/utils/collection_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package utils 19 20 import ( 21 "fmt" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 26 commonhash "github.com/polarismesh/polaris/common/hash" 27 ) 28 29 func Test_SegmentMap(t *testing.T) { 30 segmentMap := NewSegmentMap[string, string](32, func(k string) int { 31 return commonhash.Fnv32(k) 32 }) 33 34 total := 100 35 for i := 0; i < total; i++ { 36 segmentMap.Put(fmt.Sprintf("key-%d", i), fmt.Sprintf("value-%d", i)) 37 } 38 39 assert.Equal(t, uint64(total), segmentMap.Count()) 40 soltEntries := map[int]int{} 41 for i := range segmentMap.solts { 42 soltEntries[i] = len(segmentMap.solts[i]) 43 } 44 45 t.Logf("%#v", soltEntries) 46 47 key := fmt.Sprintf("key-%d", 0) 48 val, exist := segmentMap.Get(key) 49 assert.True(t, exist) 50 assert.Equal(t, val, fmt.Sprintf("value-%d", 0)) 51 52 delRet := segmentMap.Del(key) 53 assert.True(t, delRet) 54 val, exist = segmentMap.Get(key) 55 assert.False(t, exist) 56 57 oldVal, ok := segmentMap.PutIfAbsent(key, key) 58 assert.True(t, ok) 59 assert.Equal(t, "", oldVal) 60 61 oldVal, ok = segmentMap.PutIfAbsent(key, key) 62 assert.False(t, ok) 63 assert.Equal(t, key, oldVal) 64 } 65 66 func Test_SyncSegmentMap(t *testing.T) { 67 segmentMap := NewSegmentMap[string, *SegmentMap[string, string]](32, func(k string) int { 68 return commonhash.Fnv32(k) 69 }) 70 71 total := 100 72 for i := 0; i < total; i++ { 73 go func(i int) { 74 subMap, _ := segmentMap.ComputeIfAbsent(fmt.Sprintf("key-%d", i), func(k string) *SegmentMap[string, string] { 75 return NewSegmentMap[string, string](128, func(k string) int { 76 return commonhash.Fnv32(k) 77 }) 78 }) 79 subMap.Put(fmt.Sprintf("key-%d", i), fmt.Sprintf("key-%d", i)) 80 }(i) 81 } 82 83 for { 84 count := 0 85 segmentMap.Range(func(k string, v *SegmentMap[string, string]) { 86 v.Range(func(k string, v string) { 87 t.Logf("%s %s", k, v) 88 }) 89 count++ 90 }) 91 if count == total { 92 break 93 } 94 } 95 96 for { 97 count := 0 98 segmentMap.Range(func(k string, v *SegmentMap[string, string]) { 99 v.Range(func(k string, _ string) { 100 v.Del(k) 101 }) 102 count++ 103 }) 104 if count == total { 105 break 106 } 107 } 108 } 109 110 func Test_SyncMap(t *testing.T) { 111 syncMap := NewSyncMap[int, int]() 112 113 for i := 0; i < 10; i++ { 114 syncMap.Store(i, i) 115 } 116 117 assert.Equal(t, 10, syncMap.Len()) 118 119 syncMap.Range(func(key, val int) bool { 120 syncMap.Delete(key) 121 syncMap.Store(key+100, key+100) 122 return true 123 }) 124 125 assert.Equal(t, 10, syncMap.Len()) 126 127 for i := 0; i < 10; i++ { 128 _, ok := syncMap.Load(i) 129 assert.False(t, ok) 130 } 131 }