github.com/decred/dcrlnd@v0.7.6/lnwallet/chainfee/minfeemanager_test.go (about)

     1  package chainfee
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  type mockChainBackend struct {
    11  	minFee    AtomPerKByte
    12  	callCount int
    13  }
    14  
    15  func (m *mockChainBackend) fetchFee() (AtomPerKByte, error) {
    16  	m.callCount++
    17  	return m.minFee, nil
    18  }
    19  
    20  // TestMinFeeManager tests that the minFeeManager returns an up to date min fee
    21  // by querying the chain backend and that it returns a cached fee if the chain
    22  // backend was recently queried.
    23  func TestMinFeeManager(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	// Initialize the mock backend and let it have a minimum fee rate
    27  	// below our fee floor.
    28  	chainBackend := &mockChainBackend{
    29  		minFee: FeePerKBFloor - 1,
    30  	}
    31  
    32  	// Initialise the min fee manager. This should call the chain backend
    33  	// once.
    34  	feeManager, err := newMinFeeManager(
    35  		100*time.Millisecond,
    36  		chainBackend.fetchFee,
    37  	)
    38  	require.NoError(t, err)
    39  	require.Equal(t, 1, chainBackend.callCount)
    40  
    41  	// Check that the minimum fee rate is clamped by our fee floor.
    42  	require.Equal(t, feeManager.minFeePerKW, FeePerKBFloor)
    43  
    44  	// If the fee is requested again, the stored fee should be returned
    45  	// and the chain backend should not be queried.
    46  	chainBackend.minFee = AtomPerKByte(20000)
    47  	minFee := feeManager.fetchMinFee()
    48  	require.Equal(t, minFee, FeePerKBFloor)
    49  	require.Equal(t, 1, chainBackend.callCount)
    50  
    51  	// Fake the passing of time.
    52  	feeManager.lastUpdatedTime = time.Now().Add(-200 * time.Millisecond)
    53  
    54  	// If the fee is queried again after the backoff period has passed
    55  	// then the chain backend should be queried again for the min fee.
    56  	minFee = feeManager.fetchMinFee()
    57  	require.Equal(t, AtomPerKByte(20000), minFee)
    58  	require.Equal(t, 2, chainBackend.callCount)
    59  }