Skip to content

Commit

Permalink
Add more test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Bin Shi <[email protected]>
  • Loading branch information
binshi-bing committed Jun 18, 2023
1 parent 71f31d8 commit ea49027
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions pkg/utils/syncutil/lock_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestLockGroup(t *testing.T) {
for i := 0; i < concurrency; i++ {
go func(spaceID uint32) {
defer wg.Done()
mustSequentialUpdateSingle(re, spaceID, group)
mustSequentialUpdateSingle(re, spaceID, group, concurrency)
}(rand.Uint32())
}
wg.Wait()
Expand All @@ -42,23 +42,36 @@ func TestLockGroup(t *testing.T) {
func TestLockGroupWithRemoveEntryOnUnlock(t *testing.T) {
re := require.New(t)
group := NewLockGroup(WithRemoveEntryOnUnlock(true))
concurrency := 50
maxID := 1024

// Test Concurrent lock/unlock.
var wg sync.WaitGroup
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
wg.Add(maxID)
for i := 0; i < maxID; i++ {
go func(spaceID uint32) {
defer wg.Done()
mustSequentialUpdateSingle(re, spaceID, group)
}(rand.Uint32())
mustSequentialUpdateSingle(re, spaceID, group, 10)
}(uint32(i))
}

// Test range lock in a scenario with non-consecutive large key space. One of example is
// keyspace group split loads non-consecutive keyspace meta in batches and lock all loaded
// keyspace meta within a batch at the same time.
for i := 0; i < maxID; i++ {
group.Lock(uint32(i))
}
re.Equal(len(group.entries), maxID)
for i := 0; i < maxID; i++ {
group.Unlock(uint32(i))
}

wg.Wait()
// Check that size of the lock group is limited.
re.LessOrEqual(len(group.entries), 16)
re.Equal(len(group.entries), 0)
}

// mustSequentialUpdateSingle checks that for any given update, update is sequential.
func mustSequentialUpdateSingle(re *require.Assertions, spaceID uint32, group *LockGroup) {
concurrency := 50
func mustSequentialUpdateSingle(re *require.Assertions, spaceID uint32, group *LockGroup, concurrency int) {
total := 0
var wg sync.WaitGroup
wg.Add(concurrency)
Expand Down

0 comments on commit ea49027

Please sign in to comment.