github.com/MetalBlockchain/metalgo@v1.11.9/chains/atomic/memory_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package atomic
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/MetalBlockchain/metalgo/database/memdb"
    12  	"github.com/MetalBlockchain/metalgo/ids"
    13  )
    14  
    15  var (
    16  	blockchainID0 = ids.Empty.Prefix(0)
    17  	blockchainID1 = ids.Empty.Prefix(1)
    18  )
    19  
    20  func TestSharedID(t *testing.T) {
    21  	sharedID0 := sharedID(blockchainID0, blockchainID1)
    22  	sharedID1 := sharedID(blockchainID1, blockchainID0)
    23  
    24  	require.Equal(t, sharedID0, sharedID1)
    25  }
    26  
    27  func TestMemoryMakeReleaseLock(t *testing.T) {
    28  	require := require.New(t)
    29  
    30  	m := NewMemory(memdb.New())
    31  
    32  	sharedID := sharedID(blockchainID0, blockchainID1)
    33  
    34  	lock0 := m.makeLock(sharedID)
    35  
    36  	require.Equal(lock0, m.makeLock(sharedID))
    37  	m.releaseLock(sharedID)
    38  
    39  	require.Equal(lock0, m.makeLock(sharedID))
    40  	m.releaseLock(sharedID)
    41  	m.releaseLock(sharedID)
    42  
    43  	require.Equal(lock0, m.makeLock(sharedID))
    44  	m.releaseLock(sharedID)
    45  }
    46  
    47  func TestMemoryUnknownFree(t *testing.T) {
    48  	m := NewMemory(memdb.New())
    49  
    50  	sharedID := sharedID(blockchainID0, blockchainID1)
    51  
    52  	defer func() {
    53  		require.NotNil(t, recover())
    54  	}()
    55  
    56  	m.releaseLock(sharedID)
    57  }