Skip to content

Commit

Permalink
beef up models tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matYang committed Sep 13, 2023
1 parent 055aeb1 commit 20272a4
Showing 1 changed file with 84 additions and 3 deletions.
87 changes: 84 additions & 3 deletions core/chains/evm/gas/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -37,17 +38,21 @@ func TestWrappedEvmEstimator(t *testing.T) {
e.On("BumpLegacyGas", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(legacyFee, gasLimit, nil).Once()

mockEvmEstimatorName := "MockEstimator"
mockEstimatorName := "WrappedEvmEstimator(MockEstimator)"

// L1Oracle returns the correct L1Oracle interface
t.Run("L1Oracle", func(t *testing.T) {
// expect nil
estimator := gas.NewWrappedEvmEstimator(e, false)
l1Oracle := estimator.L1Oracle()
assert.Nil(t, l1Oracle)

o := oraclesMocks.NewL1Oracle(t)
estimator = gas.NewWrappedEvmEstimatorWithL1Oracle(e, false, o)
// expect l1Oracle
oracle := oraclesMocks.NewL1Oracle(t)
estimator = gas.NewWrappedEvmEstimatorWithL1Oracle(e, false, oracle)
l1Oracle = estimator.L1Oracle()
assert.Equal(t, o, l1Oracle)
assert.Equal(t, oracle, l1Oracle)
})

// GetFee returns gas estimation based on configuration value
Expand Down Expand Up @@ -127,4 +132,80 @@ func TestWrappedEvmEstimator(t *testing.T) {
fee = new(big.Int).Mul(dynamicFee.FeeCap.ToInt(), big.NewInt(int64(gasLimit)))
assert.Equal(t, new(big.Int).Add(val.ToInt(), fee), total)
})

t.Run("Name", func(t *testing.T) {
evmEstimator := mocks.NewEvmEstimator(t)
oracle := oraclesMocks.NewL1Oracle(t)

evmEstimator.On("Name").Return(mockEvmEstimatorName, nil).Once()

estimator := gas.NewWrappedEvmEstimatorWithL1Oracle(evmEstimator, false, oracle)
name := estimator.Name()
require.Equal(t, mockEstimatorName, name)
})

t.Run("Start and stop calls both EVM estimator and L1Oracle", func(t *testing.T) {
evmEstimator := mocks.NewEvmEstimator(t)
oracle := oraclesMocks.NewL1Oracle(t)

evmEstimator.On("Name").Return(mockEvmEstimatorName, nil).Times(4)
evmEstimator.On("Start", mock.Anything).Return(nil).Twice()
evmEstimator.On("Close").Return(nil).Twice()
oracle.On("Start", mock.Anything).Return(nil).Once()
oracle.On("Close").Return(nil).Once()

estimator := gas.NewWrappedEvmEstimator(evmEstimator, false)
err := estimator.Start(ctx)
require.NoError(t, err)
err = estimator.Close()
require.NoError(t, err)

estimator = gas.NewWrappedEvmEstimatorWithL1Oracle(evmEstimator, false, oracle)
err = estimator.Start(ctx)
require.NoError(t, err)
err = estimator.Close()
require.NoError(t, err)
})

t.Run("Read calls both EVM estimator and L1Oracle", func(t *testing.T) {
evmEstimator := mocks.NewEvmEstimator(t)
oracle := oraclesMocks.NewL1Oracle(t)

evmEstimator.On("Ready").Return(nil).Twice()
oracle.On("Ready").Return(nil).Once()

estimator := gas.NewWrappedEvmEstimator(evmEstimator, false)
err := estimator.Ready()
require.NoError(t, err)

estimator = gas.NewWrappedEvmEstimatorWithL1Oracle(evmEstimator, false, oracle)
err = estimator.Ready()
require.NoError(t, err)
})

t.Run("HealthReport merges report from EVM estimator and L1Oracle", func(t *testing.T) {
evmEstimator := mocks.NewEvmEstimator(t)
oracle := oraclesMocks.NewL1Oracle(t)

evmEstimatorKey := "evm"
evmEstimatorError := errors.New("evm error")
oracleKey := "oracle"
oracleError := errors.New("oracle error")

evmEstimator.On("Name").Return(mockEvmEstimatorName, nil).Twice()
evmEstimator.On("HealthReport").Return(map[string]error{evmEstimatorKey: evmEstimatorError}).Twice()
oracle.On("HealthReport").Return(map[string]error{oracleKey: oracleError}).Once()

estimator := gas.NewWrappedEvmEstimator(evmEstimator, false)
report := estimator.HealthReport()
require.True(t, errors.Is(report[evmEstimatorKey], evmEstimatorError))
require.Nil(t, report[oracleKey])
require.NotNil(t, report[mockEstimatorName])

estimator = gas.NewWrappedEvmEstimatorWithL1Oracle(evmEstimator, false, oracle)
report = estimator.HealthReport()
require.True(t, errors.Is(report[evmEstimatorKey], evmEstimatorError))
require.True(t, errors.Is(report[oracleKey], oracleError))
require.NotNil(t, report[mockEstimatorName])
})
}

0 comments on commit 20272a4

Please sign in to comment.