github.com/koko1123/flow-go-1@v0.29.6/fvm/environment/uuids_test.go (about)

     1  package environment
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/koko1123/flow-go-1/fvm/state"
     9  	"github.com/koko1123/flow-go-1/fvm/tracing"
    10  	"github.com/koko1123/flow-go-1/fvm/utils"
    11  )
    12  
    13  func TestUUIDs_GetAndSetUUID(t *testing.T) {
    14  	view := utils.NewSimpleView()
    15  	txnState := state.NewTransactionState(view, state.DefaultParameters())
    16  	uuidsA := NewUUIDGenerator(
    17  		tracing.NewTracerSpan(),
    18  		NewMeter(txnState),
    19  		txnState)
    20  
    21  	uuid, err := uuidsA.getUUID() // start from zero
    22  	require.NoError(t, err)
    23  	require.Equal(t, uint64(0), uuid)
    24  
    25  	err = uuidsA.setUUID(5)
    26  	require.NoError(t, err)
    27  
    28  	// create new UUIDs instance
    29  	uuidsB := NewUUIDGenerator(
    30  		tracing.NewTracerSpan(),
    31  		NewMeter(txnState),
    32  		txnState)
    33  	uuid, err = uuidsB.getUUID() // should read saved value
    34  	require.NoError(t, err)
    35  
    36  	require.Equal(t, uint64(5), uuid)
    37  }
    38  
    39  func Test_GenerateUUID(t *testing.T) {
    40  	view := utils.NewSimpleView()
    41  	txnState := state.NewTransactionState(view, state.DefaultParameters())
    42  	genA := NewUUIDGenerator(
    43  		tracing.NewTracerSpan(),
    44  		NewMeter(txnState),
    45  		txnState)
    46  
    47  	uuidA, err := genA.GenerateUUID()
    48  	require.NoError(t, err)
    49  	uuidB, err := genA.GenerateUUID()
    50  	require.NoError(t, err)
    51  	uuidC, err := genA.GenerateUUID()
    52  	require.NoError(t, err)
    53  
    54  	require.Equal(t, uint64(0), uuidA)
    55  	require.Equal(t, uint64(1), uuidB)
    56  	require.Equal(t, uint64(2), uuidC)
    57  
    58  	// Create new generator instance from same ledger
    59  	genB := NewUUIDGenerator(
    60  		tracing.NewTracerSpan(),
    61  		NewMeter(txnState),
    62  		txnState)
    63  
    64  	uuidD, err := genB.GenerateUUID()
    65  	require.NoError(t, err)
    66  	uuidE, err := genB.GenerateUUID()
    67  	require.NoError(t, err)
    68  	uuidF, err := genB.GenerateUUID()
    69  	require.NoError(t, err)
    70  
    71  	require.Equal(t, uint64(3), uuidD)
    72  	require.Equal(t, uint64(4), uuidE)
    73  	require.Equal(t, uint64(5), uuidF)
    74  }