From e20b047769aef8e8788c8693e8cc159c3577173f Mon Sep 17 00:00:00 2001 From: Wojciech Malota-Wojcik Date: Fri, 13 Dec 2024 19:57:52 +0100 Subject: [PATCH] Test setting a lot of items --- space/space.go | 3 +- space/space_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/space/space.go b/space/space.go index 80b07c8..36b4689 100644 --- a/space/space.go +++ b/space/space.go @@ -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 } diff --git a/space/space_test.go b/space/space_test.go index 29791c2..188261f 100644 --- a/space/space_test.go +++ b/space/space_test.go @@ -1,5 +1,5 @@ // Github actions run on machines not supporting AVX-512 instructions. -//go:build nogithub +////go:build nogithub package space @@ -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) + } +}