From 633eb89e7d0a781e8d4e05bfa3fcc95ab71785e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Ma=C5=82ota-W=C3=B3jcik?= <59281144+outofforest@users.noreply.github.com> Date: Fri, 13 Dec 2024 20:03:38 +0100 Subject: [PATCH] Test setting a lot of items (#305) --- space/space.go | 3 +- space/space_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 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..9cec407 100644 --- a/space/space_test.go +++ b/space/space_test.go @@ -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) + } +}