mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
chore: add more tests (#3575)
This commit is contained in:
parent
5e435b6a76
commit
68df0c3620
@ -29,6 +29,8 @@ func NewSafeMap() *SafeMap {
|
|||||||
// Del deletes the value with the given key from m.
|
// Del deletes the value with the given key from m.
|
||||||
func (m *SafeMap) Del(key any) {
|
func (m *SafeMap) Del(key any) {
|
||||||
m.lock.Lock()
|
m.lock.Lock()
|
||||||
|
defer m.lock.Unlock()
|
||||||
|
|
||||||
if _, ok := m.dirtyOld[key]; ok {
|
if _, ok := m.dirtyOld[key]; ok {
|
||||||
delete(m.dirtyOld, key)
|
delete(m.dirtyOld, key)
|
||||||
m.deletionOld++
|
m.deletionOld++
|
||||||
@ -52,7 +54,6 @@ func (m *SafeMap) Del(key any) {
|
|||||||
m.dirtyNew = make(map[any]any)
|
m.dirtyNew = make(map[any]any)
|
||||||
m.deletionNew = 0
|
m.deletionNew = 0
|
||||||
}
|
}
|
||||||
m.lock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get gets the value with the given key from m.
|
// Get gets the value with the given key from m.
|
||||||
@ -89,6 +90,8 @@ func (m *SafeMap) Range(f func(key, val any) bool) {
|
|||||||
// Set sets the value into m with the given key.
|
// Set sets the value into m with the given key.
|
||||||
func (m *SafeMap) Set(key, value any) {
|
func (m *SafeMap) Set(key, value any) {
|
||||||
m.lock.Lock()
|
m.lock.Lock()
|
||||||
|
defer m.lock.Unlock()
|
||||||
|
|
||||||
if m.deletionOld <= maxDeletion {
|
if m.deletionOld <= maxDeletion {
|
||||||
if _, ok := m.dirtyNew[key]; ok {
|
if _, ok := m.dirtyNew[key]; ok {
|
||||||
delete(m.dirtyNew, key)
|
delete(m.dirtyNew, key)
|
||||||
@ -102,7 +105,6 @@ func (m *SafeMap) Set(key, value any) {
|
|||||||
}
|
}
|
||||||
m.dirtyNew[key] = value
|
m.dirtyNew[key] = value
|
||||||
}
|
}
|
||||||
m.lock.Unlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the size of m.
|
// Size returns the size of m.
|
||||||
|
@ -147,3 +147,65 @@ func TestSafeMap_Range(t *testing.T) {
|
|||||||
assert.Equal(t, m.dirtyNew, newMap.dirtyNew)
|
assert.Equal(t, m.dirtyNew, newMap.dirtyNew)
|
||||||
assert.Equal(t, m.dirtyOld, newMap.dirtyOld)
|
assert.Equal(t, m.dirtyOld, newMap.dirtyOld)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetManyTimes(t *testing.T) {
|
||||||
|
const iteration = maxDeletion * 2
|
||||||
|
m := NewSafeMap()
|
||||||
|
for i := 0; i < iteration; i++ {
|
||||||
|
m.Set(i, i)
|
||||||
|
if i%3 == 0 {
|
||||||
|
m.Del(i / 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var count int
|
||||||
|
m.Range(func(k, v any) bool {
|
||||||
|
count++
|
||||||
|
return count < maxDeletion/2
|
||||||
|
})
|
||||||
|
assert.Equal(t, maxDeletion/2, count)
|
||||||
|
for i := 0; i < iteration; i++ {
|
||||||
|
m.Set(i, i)
|
||||||
|
if i%3 == 0 {
|
||||||
|
m.Del(i / 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < iteration; i++ {
|
||||||
|
m.Set(i, i)
|
||||||
|
if i%3 == 0 {
|
||||||
|
m.Del(i / 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < iteration; i++ {
|
||||||
|
m.Set(i, i)
|
||||||
|
if i%3 == 0 {
|
||||||
|
m.Del(i / 2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
count = 0
|
||||||
|
m.Range(func(k, v any) bool {
|
||||||
|
count++
|
||||||
|
return count < maxDeletion
|
||||||
|
})
|
||||||
|
assert.Equal(t, maxDeletion, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetManyTimesNew(t *testing.T) {
|
||||||
|
m := NewSafeMap()
|
||||||
|
for i := 0; i < maxDeletion*3; i++ {
|
||||||
|
m.Set(i, i)
|
||||||
|
}
|
||||||
|
for i := 0; i < maxDeletion*2; i++ {
|
||||||
|
m.Del(i)
|
||||||
|
}
|
||||||
|
for i := 0; i < maxDeletion*3; i++ {
|
||||||
|
m.Set(i+maxDeletion*3, i+maxDeletion*3)
|
||||||
|
}
|
||||||
|
for i := 0; i < maxDeletion*2; i++ {
|
||||||
|
m.Del(i + maxDeletion*2)
|
||||||
|
}
|
||||||
|
for i := 0; i < maxDeletion-copyThreshold+1; i++ {
|
||||||
|
m.Del(i + maxDeletion*2)
|
||||||
|
}
|
||||||
|
assert.Equal(t, 0, len(m.dirtyNew))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user