github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/consensus/hotstuff/pacemaker/timeout/config_test.go (about)

     1  package timeout
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/onflow/flow-go/consensus/hotstuff/model"
    10  )
    11  
    12  // TestConstructor tests that constructor performs needed checks and returns expected values depending on different inputs.
    13  func TestConstructor(t *testing.T) {
    14  	c, err := NewConfig(1200*time.Millisecond, 2000*time.Millisecond, 1.5, 3, 2000*time.Millisecond)
    15  	require.NoError(t, err)
    16  	require.Equal(t, float64(1200), c.MinReplicaTimeout)
    17  	require.Equal(t, float64(2000), c.MaxReplicaTimeout)
    18  	require.Equal(t, float64(1.5), c.TimeoutAdjustmentFactor)
    19  	require.Equal(t, uint64(3), c.HappyPathMaxRoundFailures)
    20  	require.Equal(t, float64(2000), c.MaxTimeoutObjectRebroadcastInterval)
    21  
    22  	// should not allow negative minReplicaTimeout
    23  	c, err = NewConfig(-1200*time.Millisecond, 2000*time.Millisecond, 1.5, 3, 2000*time.Millisecond)
    24  	require.True(t, model.IsConfigurationError(err))
    25  
    26  	// should not allow 0 minReplicaTimeout
    27  	c, err = NewConfig(0, 2000*time.Millisecond, 1.5, 3, 2000*time.Millisecond)
    28  	require.True(t, model.IsConfigurationError(err))
    29  
    30  	// should not allow maxReplicaTimeout < minReplicaTimeout
    31  	c, err = NewConfig(1200*time.Millisecond, 1000*time.Millisecond, 1.5, 3, 2000*time.Millisecond)
    32  	require.True(t, model.IsConfigurationError(err))
    33  
    34  	// should not allow timeoutIncrease to be 1.0 or smaller
    35  	c, err = NewConfig(1200*time.Millisecond, 2000*time.Millisecond, 1.0, 3, 2000*time.Millisecond)
    36  	require.True(t, model.IsConfigurationError(err))
    37  
    38  	// should accept only positive values for maxRebroadcastInterval
    39  	c, err = NewConfig(1200*time.Millisecond, 2000*time.Millisecond, 1.5, 3, 0)
    40  	require.True(t, model.IsConfigurationError(err))
    41  	c, err = NewConfig(1200*time.Millisecond, 2000*time.Millisecond, 1.5, 3, -1000*time.Millisecond)
    42  	require.True(t, model.IsConfigurationError(err))
    43  }
    44  
    45  // TestDefaultConfig tests that default config is filled with correct values.
    46  func TestDefaultConfig(t *testing.T) {
    47  	c := NewDefaultConfig()
    48  
    49  	require.Equal(t, float64(3000), c.MinReplicaTimeout)
    50  	require.Equal(t, 1.2, c.TimeoutAdjustmentFactor)
    51  	require.Equal(t, uint64(6), c.HappyPathMaxRoundFailures)
    52  }