Skip to content

Commit

Permalink
Test setting a lot of items (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Dec 13, 2024
1 parent 7069e9b commit 633eb89
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
3 changes: 1 addition & 2 deletions space/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ func (s *Space[K, V]) set(
}

// Add pointer node.
var err error
volatileAddress, err = s.addPointerNode(v, tx, volatileAllocator, conflict)
volatileAddress, err := s.addPointerNode(v, tx, volatileAllocator, conflict)
if err != nil {
return err
}
Expand Down
71 changes: 71 additions & 0 deletions space/space_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2152,3 +2152,74 @@ func TestSetTheSameSlotTwiceUsingDifferentEntry(t *testing.T) {
requireT.True(exists)
requireT.Equal(txtypes.Amount(10), balance)
}

// TestSetManyItems sets a lot of items to trigger node splits and parent node creations.
func TestSetManyItems(t *testing.T) {
requireT := require.New(t)

const numOfItems = 1000

state := alloc.NewForTest(t, stateSize)

s := NewSpaceTest[uint64, uint64](t, state, nil)

for i := range uint64(numOfItems) {
v := s.NewEntry(TestKey[uint64]{
Key: i,
KeyHash: types.KeyHash(i + 1),
}, StageData)
requireT.NoError(s.SetKey(v, i))
}

for i := range uint64(numOfItems) {
key := TestKey[uint64]{
Key: i,
KeyHash: types.KeyHash(i + 1),
}

v := s.NewEntry(key, StageData)
requireT.True(s.KeyExists(v))
requireT.Equal(i, s.ReadKey(v))

value, exists := s.Query(key)
requireT.True(exists)
requireT.Equal(i, value)
}
}

// TestSetManyItems sets a lot of items with conflicting key hash to trigger node splits and parent node creations.
func TestSetManyItemsWithConflicts(t *testing.T) {
requireT := require.New(t)

const (
keyHash = 1
numOfItems = 1000
)

state := alloc.NewForTest(t, stateSize)

s := NewSpaceTest[uint64, uint64](t, state, nil)

for i := range uint64(numOfItems) {
v := s.NewEntry(TestKey[uint64]{
Key: i,
KeyHash: keyHash,
}, StageData)
requireT.NoError(s.SetKey(v, i))
}

for i := range uint64(numOfItems) {
key := TestKey[uint64]{
Key: i,
KeyHash: keyHash,
}

v := s.NewEntry(key, StageData)
requireT.True(s.KeyExists(v))
requireT.Equal(i, s.ReadKey(v))

value, exists := s.Query(key)
requireT.True(exists)
requireT.Equal(i, value)
}
}

0 comments on commit 633eb89

Please sign in to comment.