Skip to content

Commit

Permalink
ethclient: fix error handling for header test (ethereum#22514)
Browse files Browse the repository at this point in the history
The wantErr field was disused, and the error returned by HeaderByNumber
was not properly tested.

This simplifies the error checking using errors.Is and asserts that getting
an expected missing header returns ethereum.NotFound. Also adds a nil
check condition for header.Number before using big.Int's Sign method.
  • Loading branch information
meowsbits committed Mar 19, 2021
1 parent 6a528fc commit aa8b218
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions ethclient/ethclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ func testHeader(t *testing.T, chain []*types.Block, client *rpc.Client) {
want: chain[1].Header(),
},
"future_block": {
block: big.NewInt(1000000000),
want: nil,
block: big.NewInt(1000000000),
want: nil,
wantErr: ethereum.NotFound,
},
}
for name, tt := range tests {
Expand All @@ -299,10 +300,10 @@ func testHeader(t *testing.T, chain []*types.Block, client *rpc.Client) {
defer cancel()

got, err := ec.HeaderByNumber(ctx, tt.block)
if tt.wantErr != nil && (err == nil || err.Error() != tt.wantErr.Error()) {
if !errors.Is(err, tt.wantErr) {
t.Fatalf("HeaderByNumber(%v) error = %q, want %q", tt.block, err, tt.wantErr)
}
if got != nil && got.Number.Sign() == 0 {
if got != nil && got.Number != nil && got.Number.Sign() == 0 {
got.Number = big.NewInt(0) // hack to make DeepEqual work
}
if !reflect.DeepEqual(got, tt.want) {
Expand Down

0 comments on commit aa8b218

Please sign in to comment.