Skip to content

Commit

Permalink
core/chains/evm/client: fix test race
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 committed Dec 16, 2024
1 parent 0fb9d70 commit 7c807d6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 21 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ modgraph:

.PHONY: test-short
test-short: ## Run 'go test -short' and suppress uninteresting output
go test -short ./... | grep -v "no test files" | grep -v "\(cached\)"
go test -short ./... | grep -v "\[no test files\]" | grep -v "\(cached\)"

help:
@echo ""
Expand Down
15 changes: 8 additions & 7 deletions core/chains/evm/client/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"net/url"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -400,15 +401,15 @@ func TestRPCClient_LatestFinalizedBlock(t *testing.T) {
lggr := logger.Test(t)

type rpcServer struct {
Head *evmtypes.Head
Head atomic.Pointer[evmtypes.Head]
URL *url.URL
}
createRPCServer := func() *rpcServer {
server := &rpcServer{}
server.URL = testutils.NewWSServer(t, chainId, func(method string, params gjson.Result) (resp testutils.JSONRPCResponse) {
assert.Equal(t, "eth_getBlockByNumber", method)
if assert.True(t, params.IsArray()) && assert.Equal(t, "finalized", params.Array()[0].String()) {
head := server.Head
head := server.Head.Load()
jsonHead, err := json.Marshal(head)
if err != nil {
panic(fmt.Errorf("failed to marshal head: %w", err))
Expand All @@ -426,7 +427,7 @@ func TestRPCClient_LatestFinalizedBlock(t *testing.T) {
rpc := client.NewRPCClient(nodePoolCfg, lggr, server.URL, nil, "rpc", 1, chainId, commonclient.Primary, commonclient.QueryTimeout, commonclient.QueryTimeout, "")
require.NoError(t, rpc.Dial(ctx))
defer rpc.Close()
server.Head = &evmtypes.Head{Number: 128}
server.Head.Store(&evmtypes.Head{Number: 128})
// updates chain info
_, err := rpc.LatestFinalizedBlock(ctx)
require.NoError(t, err)
Expand All @@ -439,7 +440,7 @@ func TestRPCClient_LatestFinalizedBlock(t *testing.T) {
assert.Equal(t, int64(128), latest.FinalizedBlockNumber)

// lower block number does not update highestUserObservations
server.Head = &evmtypes.Head{Number: 127}
server.Head.Store(&evmtypes.Head{Number: 127})
_, err = rpc.LatestFinalizedBlock(ctx)
require.NoError(t, err)
latest, highestUserObservations = rpc.GetInterceptedChainInfo()
Expand All @@ -451,7 +452,7 @@ func TestRPCClient_LatestFinalizedBlock(t *testing.T) {
assert.Equal(t, int64(127), latest.FinalizedBlockNumber)

// health check flg prevents change in highestUserObservations
server.Head = &evmtypes.Head{Number: 256}
server.Head.Store(&evmtypes.Head{Number: 256})
_, err = rpc.LatestFinalizedBlock(commonclient.CtxAddHealthCheckFlag(ctx))
require.NoError(t, err)
latest, highestUserObservations = rpc.GetInterceptedChainInfo()
Expand All @@ -463,7 +464,7 @@ func TestRPCClient_LatestFinalizedBlock(t *testing.T) {
assert.Equal(t, int64(256), latest.FinalizedBlockNumber)

// subscription updates chain info
server.Head = &evmtypes.Head{Number: 512}
server.Head.Store(&evmtypes.Head{Number: 512})
ch, sub, err := rpc.SubscribeToFinalizedHeads(ctx)
require.NoError(t, err)
defer sub.Unsubscribe()
Expand All @@ -479,7 +480,7 @@ func TestRPCClient_LatestFinalizedBlock(t *testing.T) {

// health check subscription only updates latest
sub.Unsubscribe() // close previous one
server.Head = &evmtypes.Head{Number: 1024}
server.Head.Store(&evmtypes.Head{Number: 1024})
ch, sub, err = rpc.SubscribeToFinalizedHeads(commonclient.CtxAddHealthCheckFlag(ctx))
require.NoError(t, err)
defer sub.Unsubscribe()
Expand Down
14 changes: 1 addition & 13 deletions core/internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,7 @@ func WaitTimeout(t *testing.T) time.Duration {

// Context returns a context with the test's deadline, if available.
func Context(tb testing.TB) context.Context {
ctx := context.Background()
var cancel func()
switch t := tb.(type) {
case *testing.T:
if d, ok := t.Deadline(); ok {
ctx, cancel = context.WithDeadline(ctx, d)
}
}
if cancel == nil {
ctx, cancel = context.WithCancel(ctx)
}
tb.Cleanup(cancel)
return ctx
return tests.Context(tb)
}

// MustParseURL parses the URL or fails the test
Expand Down

0 comments on commit 7c807d6

Please sign in to comment.