ethclient: fix error handling for header test (#22514)

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.
This commit is contained in:
meowsbits 2021-03-19 05:14:23 -05:00 committed by GitHub
parent 6a528fce33
commit aa8b2189c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 4 deletions

View File

@ -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 {
@ -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) {