Skip to content

Commit

Permalink
Simplify test
Browse files Browse the repository at this point in the history
Signed-off-by: Bin Shi <binshi.bing@gmail.com>
  • Loading branch information
binshi-bing committed Apr 4, 2023
1 parent 561e3cf commit 5684abd
Showing 1 changed file with 21 additions and 53 deletions.
74 changes: 21 additions & 53 deletions pkg/tso/keyspace_group_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package tso
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"path"
"sort"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -174,13 +176,13 @@ func runTestLoadKeyspaceGroupsAssignment(
loadKeyspaceGroupsBatchSize int64, // set to 0 to use the default value
probabilityAssignToMe int, // percentage of assigning keyspace groups to this host/pod
) {
ids := make(map[uint32]bool, 0)
idsExpected := []int{}
mgr := newUniqueKeyspaceGroupManager(ctx, etcdClient, cfg, 0, loadKeyspaceGroupsBatchSize)
re.NotNil(mgr)
defer mgr.Close()

const step = 30
keyspaceGroupsAdded := sync.Map{}
step := 30
mux := sync.Mutex{}
wg := sync.WaitGroup{}
for i := 0; i < numberOfKeypaceGroupsToAdd; i += step {
wg.Add(1)
Expand All @@ -197,7 +199,9 @@ func runTestLoadKeyspaceGroupsAssignment(
assignToMe := false
if randomGen.Intn(100) < probabilityAssignToMe {
assignToMe = true
keyspaceGroupsAdded.Store(uint32(j), struct{}{})
mux.Lock()
idsExpected = append(idsExpected, j)
mux.Unlock()
}
addKeyspaceGroupAssignment(
ctx, etcdClient, assignToMe,
Expand All @@ -207,21 +211,13 @@ func runTestLoadKeyspaceGroupsAssignment(
}
wg.Wait()

// Set the expected result. The keyspace group manager only loads len(mgr.ams) keyspace
// groups as max, so we need to ignore the extra keyspace groups in the expected result.
keyspaceGroupsAdded.Range(func(key, _ interface{}) bool {
id := key.(uint32)
if id < uint32(numberOfKeypaceGroupsToAdd) {
ids[id] = true
}
return true
})

err := mgr.Initialize(true)
re.NoError(err)

// Verify the keyspace group assignment.
re.True(verifyKeyspaceGroupAssignment(ids, mgr))
sort.Ints(idsExpected)
idsAssigned := collectAssignedKeyspaceGroupIDs(re, mgr)
re.Equal(idsExpected, idsAssigned)
}

func newUniqueKeyspaceGroupManager(
Expand Down Expand Up @@ -277,50 +273,22 @@ func addKeyspaceGroupAssignment(
return nil
}

func verifyKeyspaceGroupAssignment(ids map[uint32]bool, ksgMgr *KeyspaceGroupManager) bool {
for i := 0; i < len(ksgMgr.ams); i++ {
am := ksgMgr.ams[i].Load()
if am == nil {
if _, ok := ids[uint32(i)]; ok {
return false
}
} else {
if i != int(am.ksgID) {
return false
}
if _, ok := ids[uint32(i)]; !ok {
return false
}
}
}

func collectAssignedKeyspaceGroupIDs(re *require.Assertions, ksgMgr *KeyspaceGroupManager) []int {
ids := []int{}
for i := 0; i < len(ksgMgr.ksgs); i++ {
ksg := ksgMgr.ksgs[i].Load()
if ksg == nil {
if _, ok := ids[uint32(i)]; ok {
return false
}
re.Nil(ksgMgr.ams[i].Load(), fmt.Sprintf("ksg is nil but am is not nil for id %d", i))
} else {
if i != int(ksg.ID) {
return false
}
if _, ok := ids[uint32(i)]; !ok {
return false
}
if ksg.Members[0].Address != ksgMgr.tsoServiceID.ServiceAddr {
return false
am := ksgMgr.ams[i].Load()
re.NotNil(am, fmt.Sprintf("ksg is not nil but am is nil for id %d", i))
re.Equal(i, int(am.ksgID))
re.Equal(i, int(ksg.ID))
if ksg.Members[0].Address == ksgMgr.tsoServiceID.ServiceAddr {
ids = append(ids, i)
}
if len(ksg.Keyspaces) != 1 || ksg.Keyspaces[0] != uint32(i) {
return false
}
}
}

for k := range ids {
if k >= uint32(len(ksgMgr.ams)) || ksgMgr.ams[k].Load() == nil || ksgMgr.ksgs[k].Load() == nil {
return false
}
}

return true
return ids
}

0 comments on commit 5684abd

Please sign in to comment.