Skip to content

Commit

Permalink
Avoid creating ArrayDataSlab directly in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Jan 27, 2025
1 parent 5d6d296 commit 1ff0c84
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
40 changes: 9 additions & 31 deletions storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,30 +864,25 @@ func TestPersistentStorage(t *testing.T) {
baseStorage := NewInMemBaseStorage()
storage := NewPersistentSlabStorage(baseStorage, encMode, decMode, nil, nil)

// Encoding slabWithNonStorable returns error.
slabWithNonStorable := &ArrayDataSlab{
header: ArraySlabHeader{size: uint32(1), count: uint32(1)},
elements: []Storable{nonStorable{}},
}
// Encoding slabWithSlowStorable takes some time which delays
// sending encoding result to results channel.
slabWithSlowStorable := &ArrayDataSlab{
header: ArraySlabHeader{size: uint32(3), count: uint32(1)},
elements: []Storable{newSlowStorable(1)},
}

address := Address{1}

id, err := storage.GenerateSlabID(address)
require.NoError(t, err)

// Encoding slabWithNonStorable returns error.
slabWithNonStorable := NewArrayRootDataSlab(id, []Storable{nonStorable{}})

err = storage.Store(id, slabWithNonStorable)
require.NoError(t, err)

for i := 0; i < 500; i++ {
id, err := storage.GenerateSlabID(address)
require.NoError(t, err)

// Encoding slabWithSlowStorable takes some time which delays
// sending encoding result to results channel.
slabWithSlowStorable := NewArrayRootDataSlab(id, []Storable{newSlowStorable(1)})

err = storage.Store(id, slabWithSlowStorable)
require.NoError(t, err)
}
Expand Down Expand Up @@ -1075,37 +1070,20 @@ func TestPersistentStorageGenerateSlabID(t *testing.T) {

func generateRandomSlab(id SlabID, r *rand.Rand) Slab {
storable := Uint64Value(r.Uint64())

return &ArrayDataSlab{
header: ArraySlabHeader{
slabID: id,
size: arrayRootDataSlabPrefixSize + storable.ByteSize(),
count: 1,
},
elements: []Storable{storable},
}
return NewArrayRootDataSlab(id, []Storable{storable})
}

func generateLargeSlab(id SlabID) Slab {

const elementCount = 100

storables := make([]Storable, elementCount)
size := uint32(0)
for i := 0; i < elementCount; i++ {
storable := Uint64Value(uint64(i))
size += storable.ByteSize()
storables[i] = storable
}

return &ArrayDataSlab{
header: ArraySlabHeader{
slabID: id,
size: arrayRootDataSlabPrefixSize + size,
count: elementCount,
},
elements: storables,
}
return NewArrayRootDataSlab(id, storables)
}

func generateRandomAddress(r *rand.Rand) Address {
Expand Down
17 changes: 17 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,20 @@ func IsMapRootDataSlab(m *OrderedMap) bool {
func GetMapRootSlabByteSize(m *OrderedMap) uint32 {
return GetMapRootSlab(m).ByteSize()
}

func NewArrayRootDataSlab(id SlabID, storables []Storable) ArraySlab {
size := uint32(arrayRootDataSlabPrefixSize)

for _, storable := range storables {
size += storable.ByteSize()
}

return &ArrayDataSlab{
header: ArraySlabHeader{
slabID: id,
size: size,
count: uint32(len(storables)),
},
elements: storables,
}
}

0 comments on commit 1ff0c84

Please sign in to comment.