From 70d115f53be76f9325aef139340fac6ba353699d Mon Sep 17 00:00:00 2001 From: Bartek Tofel Date: Tue, 12 Mar 2024 11:47:27 +0100 Subject: [PATCH] use each retrier once, add dedicated method for restarting postgres DB (required by vrf tests), add method to get underlaying eth client to EVMClient --- blockchain/blockchain.go | 3 +++ blockchain/ethereum.go | 8 ++++++++ docker/docker.go | 22 ++++++++++++++-------- docker/test_env/postgres.go | 10 +++++++++- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 0b3ac49c1..85957de94 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -13,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethclient" ) // EVMClient is the interface that wraps a given client implementation for a blockchain, to allow for switching @@ -112,6 +113,8 @@ type EVMClient interface { FilterLogs(ctx context.Context, filterQuery ethereum.FilterQuery) ([]types.Log, error) RawJsonRPCCall(ctx context.Context, result interface{}, method string, params ...interface{}) error + + GetEthClient() *ethclient.Client } // NodeHeader header with the ID of the node that received it diff --git a/blockchain/ethereum.go b/blockchain/ethereum.go index 825b375aa..0bc91e047 100644 --- a/blockchain/ethereum.go +++ b/blockchain/ethereum.go @@ -587,6 +587,10 @@ func (e *EthereumClient) ProcessTransaction(tx *types.Transaction) error { return nil } +func (e *EthereumClient) GetEthClient() *ethclient.Client { + return e.Client +} + // ProcessEvent will queue or wait on an event depending on whether parallel transactions are enabled func (e *EthereumClient) ProcessEvent(name string, event *types.Log, confirmedChan chan bool, errorChan chan error) error { var eventConfirmer HeaderEventSubscription @@ -1171,6 +1175,10 @@ type EthereumMultinodeClient struct { Clients []EVMClient } +func (e *EthereumMultinodeClient) GetEthClient() *ethclient.Client { + return e.DefaultClient.GetEthClient() +} + func (e *EthereumMultinodeClient) Backend() bind.ContractBackend { return e.DefaultClient.Backend() } diff --git a/docker/docker.go b/docker/docker.go index 4916c0463..ea30f8bfe 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -55,6 +55,9 @@ var NaiveRetrier = func(l zerolog.Logger, startErr error, req tc.GenericContaine Str("Retrier", "NaiveRetrier"). Msgf("Attempting to start %s container", req.Name) + oldName := req.Name + req.Name = req.Name + "-naive-retry" + ct, err := tc.GenericContainer(testcontext.Get(nil), req) if err == nil { l.Debug(). @@ -72,6 +75,8 @@ var NaiveRetrier = func(l zerolog.Logger, startErr error, req tc.GenericContaine } } + req.Name = oldName + l.Debug(). Str("Original start error", startErr.Error()). Str("Current start error", err.Error()). @@ -87,6 +92,8 @@ var LinuxPlatformImageRetrier = func(l zerolog.Logger, startErr error, req tc.Ge return nil, startErr } req.Reuse = false // We need to force a new container to be created + oldName := req.Name + req.Name = req.Name + "-linux-retry" // a bit lame, but that's the lame error we get in case there's no specific image for our platform :facepalm: if !strings.Contains(startErr.Error(), "No such image") { @@ -114,6 +121,7 @@ var LinuxPlatformImageRetrier = func(l zerolog.Logger, startErr error, req tc.Ge } req.ImagePlatform = originalPlatform + req.Name = oldName if ct != nil { err := ct.Terminate(testcontext.Get(nil)) @@ -148,15 +156,13 @@ func StartContainerWithRetry(l zerolog.Logger, req tc.GenericContainerRequest, r retriers = append(retriers, LinuxPlatformImageRetrier, NaiveRetrier) } - for i := 0; i < RetryAttempts; i++ { - l.Info().Err(err).Msgf("Cannot start %s container, retrying %d/%d", req.Name, i+1, RetryAttempts) + l.Info().Err(err).Msgf("Cannot start %s container, retrying", req.Name) - req.Reuse = true // Try and see if we can reuse the container for a retry - for _, retrier := range retriers { - ct, err = retrier(l, err, req) - if err == nil { - return ct, nil - } + req.Reuse = true // Try and see if we can reuse the container for a retry + for _, retrier := range retriers { + ct, err = retrier(l, err, req) + if err == nil { + return ct, nil } } diff --git a/docker/test_env/postgres.go b/docker/test_env/postgres.go index 385b2fdd0..c87ce59e3 100644 --- a/docker/test_env/postgres.go +++ b/docker/test_env/postgres.go @@ -118,12 +118,20 @@ func (pg *PostgresDb) WithTestInstance(t *testing.T) *PostgresDb { } func (pg *PostgresDb) StartContainer() error { + return pg.startContainer(false) +} + +func (pg *PostgresDb) RestartContainer() error { + return pg.startContainer(true) +} + +func (pg *PostgresDb) startContainer(withReuse bool) error { req := pg.getContainerRequest() l := logging.GetTestContainersGoTestLogger(pg.t) c, err := docker.StartContainerWithRetry(pg.l, tc.GenericContainerRequest{ ContainerRequest: *req, Started: true, - Reuse: false, + Reuse: withReuse, Logger: l, }) if err != nil {