Skip to content

Commit

Permalink
use each retrier once, add dedicated method for restarting postgres D…
Browse files Browse the repository at this point in the history
…B (required by vrf tests), add method to get underlaying eth client to EVMClient
  • Loading branch information
Tofel committed Mar 12, 2024
1 parent ef62a73 commit 70d115f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
3 changes: 3 additions & 0 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions blockchain/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
22 changes: 14 additions & 8 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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()).
Expand All @@ -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") {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
}

Expand Down
10 changes: 9 additions & 1 deletion docker/test_env/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 70d115f

Please sign in to comment.