Skip to content

Commit

Permalink
use servicetest.Run & RunHealthy (#11354)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Dec 8, 2023
1 parent 8b13213 commit d3f99e4
Show file tree
Hide file tree
Showing 50 changed files with 214 additions and 317 deletions.
19 changes: 7 additions & 12 deletions core/chains/evm/gas/arbitrum_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/mocks"
Expand Down Expand Up @@ -66,8 +67,7 @@ func TestArbitrumEstimator(t *testing.T) {
}).Return(zeros.Bytes(), nil)

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{v: maxGasLimit}, rpcClient, ethClient)
require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
require.NoError(t, err)
// Expected price for a standard l2_suggested_estimator would be 42, but we add a fixed gasPriceBufferPercentage.
Expand All @@ -92,8 +92,7 @@ func TestArbitrumEstimator(t *testing.T) {
assert.Equal(t, big.NewInt(-1), blockNumber)
}).Return(zeros.Bytes(), nil)

require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, assets.NewWeiI(40))
require.Error(t, err)
assert.EqualError(t, err, "estimated gas price: 42 wei is greater than the maximum gas price configured: 40 wei")
Expand All @@ -118,8 +117,7 @@ func TestArbitrumEstimator(t *testing.T) {
assert.Equal(t, big.NewInt(-1), blockNumber)
}).Return(zeros.Bytes(), nil)

require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, assets.NewWeiI(110))
assert.EqualError(t, err, "estimated gas price: 120 wei is greater than the maximum gas price configured: 110 wei")
assert.Nil(t, gasPrice)
Expand Down Expand Up @@ -148,8 +146,7 @@ func TestArbitrumEstimator(t *testing.T) {
assert.Equal(t, big.NewInt(-1), blockNumber)
}).Return(zeros.Bytes(), nil)

require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)

_, _, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
assert.EqualError(t, err, "failed to estimate gas; gas price not set")
Expand Down Expand Up @@ -181,8 +178,7 @@ func TestArbitrumEstimator(t *testing.T) {
}).Return(b.Bytes(), nil)

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{v: maxGasLimit}, rpcClient, ethClient)
require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
require.NoError(t, err)
require.NotNil(t, gasPrice)
Expand Down Expand Up @@ -216,8 +212,7 @@ func TestArbitrumEstimator(t *testing.T) {
}).Return(b.Bytes(), nil)

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{v: maxGasLimit}, rpcClient, ethClient)
require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
require.Error(t, err, "expected error but got (%s, %d)", gasPrice, chainSpecificGasLimit)
})
Expand Down
10 changes: 4 additions & 6 deletions core/chains/evm/gas/rollups/l1_gas_price_oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"

"github.com/smartcontractkit/chainlink/v2/common/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
Expand Down Expand Up @@ -50,8 +51,7 @@ func TestL1GasPriceOracle(t *testing.T) {
}).Return(common.BigToHash(l1BaseFee).Bytes(), nil)

oracle := NewL1GasPriceOracle(logger.Test(t), ethClient, config.ChainArbitrum)
require.NoError(t, oracle.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, oracle.Close()) })
servicetest.RunHealthy(t, oracle)

gasPrice, err := oracle.GasPrice(testutils.Context(t))
require.NoError(t, err)
Expand All @@ -72,8 +72,7 @@ func TestL1GasPriceOracle(t *testing.T) {
}).Return(common.BigToHash(l1BaseFee).Bytes(), nil)

oracle := NewL1GasPriceOracle(logger.Test(t), ethClient, config.ChainKroma)
require.NoError(t, oracle.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, oracle.Close()) })
servicetest.RunHealthy(t, oracle)

gasPrice, err := oracle.GasPrice(testutils.Context(t))
require.NoError(t, err)
Expand All @@ -94,8 +93,7 @@ func TestL1GasPriceOracle(t *testing.T) {
}).Return(common.BigToHash(l1BaseFee).Bytes(), nil)

oracle := NewL1GasPriceOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock)
require.NoError(t, oracle.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, oracle.Close()) })
servicetest.RunHealthy(t, oracle)

gasPrice, err := oracle.GasPrice(testutils.Context(t))
require.NoError(t, err)
Expand Down
13 changes: 5 additions & 8 deletions core/chains/evm/gas/suggested_price_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/mocks"
Expand Down Expand Up @@ -40,8 +41,7 @@ func TestSuggestedPriceEstimator(t *testing.T) {
})

o := gas.NewSuggestedPriceEstimator(logger.Test(t), client)
require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
require.NoError(t, err)
assert.Equal(t, assets.NewWeiI(42), gasPrice)
Expand All @@ -57,8 +57,7 @@ func TestSuggestedPriceEstimator(t *testing.T) {
(*big.Int)(res).SetInt64(42)
})

require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, assets.NewWeiI(40))
require.Error(t, err)
assert.EqualError(t, err, "estimated gas price: 42 wei is greater than the maximum gas price configured: 40 wei")
Expand All @@ -75,8 +74,7 @@ func TestSuggestedPriceEstimator(t *testing.T) {
(*big.Int)(res).SetInt64(120)
})

require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, assets.NewWeiI(110))
assert.EqualError(t, err, "estimated gas price: 120 wei is greater than the maximum gas price configured: 110 wei")
assert.Nil(t, gasPrice)
Expand All @@ -96,8 +94,7 @@ func TestSuggestedPriceEstimator(t *testing.T) {

client.On("CallContext", mock.Anything, mock.Anything, "eth_gasPrice").Return(errors.New("kaboom"))

require.NoError(t, o.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, o.Close()) })
servicetest.RunHealthy(t, o)

_, _, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
assert.EqualError(t, err, "failed to estimate gas; gas price not set")
Expand Down
11 changes: 6 additions & 5 deletions core/chains/evm/headtracker/head_broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"

commonhtrk "github.com/smartcontractkit/chainlink/v2/common/headtracker"
commonmocks "github.com/smartcontractkit/chainlink/v2/common/types/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker"
Expand All @@ -21,7 +23,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
"github.com/smartcontractkit/chainlink/v2/core/services"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/store/models"
"github.com/smartcontractkit/chainlink/v2/core/utils"
Expand Down Expand Up @@ -68,14 +69,14 @@ func TestHeadBroadcaster_Subscribe(t *testing.T) {
checker1 := &cltest.MockHeadTrackable{}
checker2 := &cltest.MockHeadTrackable{}

hb := headtracker.NewHeadBroadcaster(logger)
orm := headtracker.NewORM(db, logger, cfg.Database(), *ethClient.ConfiguredChainID())
hs := headtracker.NewHeadSaver(logger, orm, evmCfg.EVM(), evmCfg.EVM().HeadTracker())
mailMon := utils.NewMailboxMonitor(t.Name())
servicetest.Run(t, mailMon)
hb := headtracker.NewHeadBroadcaster(logger)
servicetest.Run(t, hb)
ht := headtracker.NewHeadTracker(logger, ethClient, evmCfg.EVM(), evmCfg.EVM().HeadTracker(), hb, hs, mailMon)
var ms services.MultiStart
require.NoError(t, ms.Start(testutils.Context(t), mailMon, hb, ht))
t.Cleanup(func() { require.NoError(t, services.CloseAll(mailMon, hb, ht)) })
servicetest.Run(t, ht)

latest1, unsubscribe1 := hb.Subscribe(checker1)
// "latest head" is nil here because we didn't receive any yet
Expand Down
1 change: 1 addition & 0 deletions core/chains/evm/log/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"

evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks"
evmconfig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/config"
Expand Down
3 changes: 2 additions & 1 deletion core/chains/evm/log/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"

evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/log"
logmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/log/mocks"
Expand Down Expand Up @@ -1324,7 +1325,7 @@ func TestBroadcaster_AppendLogChannel(t *testing.T) {
ch3 := make(chan types.Log)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)
mailMon := servicetest.Run(t, utils.NewMailboxMonitor(t.Name()))
mailMon := servicetest.RunHealthy(t, utils.NewMailboxMonitor(t.Name()))
lb := log.NewBroadcaster(nil, ethClient, nil, logger.Test(t), nil, mailMon)
chCombined := lb.ExportedAppendLogChannel(ch1, ch2)
chCombined = lb.ExportedAppendLogChannel(chCombined, ch3)
Expand Down
13 changes: 5 additions & 8 deletions core/chains/evm/logpoller/log_poller_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"

evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
Expand Down Expand Up @@ -351,8 +352,7 @@ func TestLogPoller_Replay(t *testing.T) {
ec.On("FilterLogs", mock.Anything, mock.Anything).Return([]types.Log{log1}, nil).Maybe() // in case task gets delayed by >= 100ms

t.Cleanup(lp.reset)
require.NoError(t, lp.Start(ctx))
t.Cleanup(func() { assert.NoError(t, lp.Close()) })
servicetest.Run(t, lp)

select {
case <-ctx.Done():
Expand Down Expand Up @@ -389,8 +389,7 @@ func TestLogPoller_Replay(t *testing.T) {
ec.On("FilterLogs", mock.Anything, mock.Anything).Return([]types.Log{log1}, nil).Maybe() // in case task gets delayed by >= 100ms

t.Cleanup(lp.reset)
require.NoError(t, lp.Start(ctx))
t.Cleanup(func() { assert.NoError(t, lp.Close()) })
servicetest.Run(t, lp)

select {
case <-ctx.Done():
Expand All @@ -402,8 +401,7 @@ func TestLogPoller_Replay(t *testing.T) {
// ReplayAsync should return as soon as replayStart is received
t.Run("ReplayAsync success", func(t *testing.T) {
t.Cleanup(lp.reset)
require.NoError(t, lp.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, lp.Close()) })
servicetest.Run(t, lp)

lp.ReplayAsync(1)

Expand All @@ -412,8 +410,7 @@ func TestLogPoller_Replay(t *testing.T) {

t.Run("ReplayAsync error", func(t *testing.T) {
t.Cleanup(lp.reset)
require.NoError(t, lp.Start(testutils.Context(t)))
t.Cleanup(func() { assert.NoError(t, lp.Close()) })
servicetest.Run(t, lp)

anyErr := errors.New("async error")
observedLogs.TakeAll()
Expand Down
18 changes: 6 additions & 12 deletions core/chains/evm/monitor/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclimocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/monitor"
Expand Down Expand Up @@ -44,7 +45,6 @@ func TestBalanceMonitor_Start(t *testing.T) {
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.Test(t))
defer func() { assert.NoError(t, bm.Close()) }()

k0bal := big.NewInt(42)
k1bal := big.NewInt(43)
Expand All @@ -54,7 +54,7 @@ func TestBalanceMonitor_Start(t *testing.T) {
ethClient.On("BalanceAt", mock.Anything, k0Addr, nilBigInt).Once().Return(k0bal, nil)
ethClient.On("BalanceAt", mock.Anything, k1Addr, nilBigInt).Once().Return(k1bal, nil)

assert.NoError(t, bm.Start(testutils.Context(t)))
servicetest.RunHealthy(t, bm)

gomega.NewWithT(t).Eventually(func() *big.Int {
return bm.GetEthBalance(k0Addr).ToInt()
Expand All @@ -72,12 +72,11 @@ func TestBalanceMonitor_Start(t *testing.T) {
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.Test(t))
defer func() { assert.NoError(t, bm.Close()) }()
k0bal := big.NewInt(42)

ethClient.On("BalanceAt", mock.Anything, k0Addr, nilBigInt).Once().Return(k0bal, nil)

assert.NoError(t, bm.Start(testutils.Context(t)))
servicetest.RunHealthy(t, bm)

gomega.NewWithT(t).Eventually(func() *big.Int {
return bm.GetEthBalance(k0Addr).ToInt()
Expand All @@ -92,7 +91,6 @@ func TestBalanceMonitor_Start(t *testing.T) {
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.Test(t))
defer func() { assert.NoError(t, bm.Close()) }()
ctxCancelledAwaiter := cltest.NewAwaiter()

ethClient.On("BalanceAt", mock.Anything, k0Addr, nilBigInt).Once().Run(func(args mock.Arguments) {
Expand Down Expand Up @@ -122,13 +120,12 @@ func TestBalanceMonitor_Start(t *testing.T) {
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.Test(t))
defer func() { assert.NoError(t, bm.Close()) }()

ethClient.On("BalanceAt", mock.Anything, k0Addr, nilBigInt).
Once().
Return(nil, errors.New("a little easter egg for the 4chan link marines error"))

assert.NoError(t, bm.Start(testutils.Context(t)))
servicetest.RunHealthy(t, bm)

gomega.NewWithT(t).Consistently(func() *big.Int {
return bm.GetEthBalance(k0Addr).ToInt()
Expand Down Expand Up @@ -160,8 +157,7 @@ func TestBalanceMonitor_OnNewLongestChain_UpdatesBalance(t *testing.T) {
ethClient.On("BalanceAt", mock.Anything, k0Addr, nilBigInt).Once().Return(k0bal, nil)
ethClient.On("BalanceAt", mock.Anything, k1Addr, nilBigInt).Once().Return(k1bal, nil)

require.NoError(t, bm.Start(testutils.Context(t)))
defer func() { assert.NoError(t, bm.Close()) }()
servicetest.RunHealthy(t, bm)

ethClient.On("BalanceAt", mock.Anything, k0Addr, nilBigInt).Once().Return(k0bal, nil)
ethClient.On("BalanceAt", mock.Anything, k1Addr, nilBigInt).Once().Return(k1bal, nil)
Expand Down Expand Up @@ -205,7 +201,7 @@ func TestBalanceMonitor_FewerRPCCallsWhenBehind(t *testing.T) {
ethClient.On("BalanceAt", mock.Anything, mock.Anything, mock.Anything).
Once().
Return(big.NewInt(1), nil)
require.NoError(t, bm.Start(testutils.Context(t)))
servicetest.RunHealthy(t, bm)

head := cltest.Head(0)

Expand Down Expand Up @@ -234,8 +230,6 @@ func TestBalanceMonitor_FewerRPCCallsWhenBehind(t *testing.T) {
mockUnblocker <- time.Time{}
})

bm.Close()

// Make sure the BalanceAt mock wasn't called more than once
assert.LessOrEqual(t, callCount.Load(), int32(1))
}
Expand Down
Loading

0 comments on commit d3f99e4

Please sign in to comment.