github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/migration/refCntSize_test.go (about)

     1  // Copyright 2023 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package migration_test
     6  
     7  import (
     8  	"math/rand"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/sharky"
    12  	"github.com/ethersphere/bee/v2/pkg/storage/inmemstore"
    13  	"github.com/ethersphere/bee/v2/pkg/storer/internal/chunkstore"
    14  	localmigration "github.com/ethersphere/bee/v2/pkg/storer/migration"
    15  	"github.com/ethersphere/bee/v2/pkg/swarm"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func Test_RefCntSize(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	stepFn := localmigration.RefCountSizeInc
    23  	store := inmemstore.New()
    24  
    25  	// simulate old cacheEntryItem with some random bytes.
    26  	var oldItems []*localmigration.OldRetrievalIndexItem
    27  	for i := 0; i < 10; i++ {
    28  		entry := &localmigration.OldRetrievalIndexItem{
    29  			Address:   swarm.RandAddress(t),
    30  			Timestamp: uint64(rand.Int()),
    31  			Location:  sharky.Location{Shard: uint8(rand.Int()), Slot: uint32(rand.Int()), Length: uint16(rand.Int())},
    32  			RefCnt:    uint8(rand.Int()),
    33  		}
    34  		oldItems = append(oldItems, entry)
    35  		err := store.Put(entry)
    36  		assert.NoError(t, err)
    37  	}
    38  
    39  	assert.NoError(t, stepFn(store)())
    40  
    41  	// check if all entries are migrated.
    42  	for _, entry := range oldItems {
    43  		cEntry := &chunkstore.RetrievalIndexItem{Address: entry.Address}
    44  		err := store.Get(cEntry)
    45  		assert.NoError(t, err)
    46  		assert.Equal(t, entry.Address, cEntry.Address)
    47  		assert.Equal(t, entry.Timestamp, cEntry.Timestamp)
    48  		assert.Equal(t, entry.Location, cEntry.Location)
    49  		assert.Equal(t, uint32(entry.RefCnt), cEntry.RefCnt)
    50  	}
    51  }