Skip to content

Commit

Permalink
tests: finish errors.Is equality cases
Browse files Browse the repository at this point in the history
Signed-off-by: redwrasse <[email protected]>
  • Loading branch information
redwrasse committed Sep 24, 2024
1 parent e3319c5 commit 1af229b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions tests/e2e/ctl_v3_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -193,7 +194,7 @@ func getMemberList(cx ctlCtx, serializable bool) (etcdserverpb.MemberListRespons

resp := etcdserverpb.MemberListResponse{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&resp); err == io.EOF {
if err := dec.Decode(&resp); errors.Is(err, io.EOF) {
return etcdserverpb.MemberListResponse{}, err
}
return resp, nil
Expand Down Expand Up @@ -221,7 +222,7 @@ func memberListWithHexTest(cx ctlCtx) {
}
hexResp := etcdserverpb.MemberListResponse{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&hexResp); err == io.EOF {
if err := dec.Decode(&hexResp); errors.Is(err, io.EOF) {
cx.t.Fatalf("memberListWithHexTest error (%v)", err)
}
num := len(resp.Members)
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/ctl_v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package e2e
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -156,7 +157,7 @@ func getSnapshotStatus(cx ctlCtx, fpath string) (snapshot.Status, error) {

resp := snapshot.Status{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&resp); err == io.EOF {
if err := dec.Decode(&resp); errors.Is(err, io.EOF) {
return snapshot.Status{}, err
}
return resp, nil
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/clientv3/concurrency/example_mutex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package concurrency_test

import (
"context"
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -64,7 +65,7 @@ func ExampleMutex_TryLock() {
if err = m2.TryLock(context.TODO()); err == nil {
log.Fatal("should not acquire lock")
}
if err == concurrency.ErrLocked {
if errors.Is(err, concurrency.ErrLocked) {
fmt.Println("cannot acquire lock for s2, as already locked in another session")
}

Expand Down
11 changes: 6 additions & 5 deletions tests/integration/clientv3/connectivity/black_hole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package connectivity_test

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -113,7 +114,7 @@ func TestBalancerUnderBlackholeKeepAliveWatch(t *testing.T) {
func TestBalancerUnderBlackholeNoKeepAlivePut(t *testing.T) {
testBalancerUnderBlackholeNoKeepAlive(t, func(cli *clientv3.Client, ctx context.Context) error {
_, err := cli.Put(ctx, "foo", "bar")
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand All @@ -123,7 +124,7 @@ func TestBalancerUnderBlackholeNoKeepAlivePut(t *testing.T) {
func TestBalancerUnderBlackholeNoKeepAliveDelete(t *testing.T) {
testBalancerUnderBlackholeNoKeepAlive(t, func(cli *clientv3.Client, ctx context.Context) error {
_, err := cli.Delete(ctx, "foo")
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand All @@ -136,7 +137,7 @@ func TestBalancerUnderBlackholeNoKeepAliveTxn(t *testing.T) {
If(clientv3.Compare(clientv3.Version("foo"), "=", 0)).
Then(clientv3.OpPut("foo", "bar")).
Else(clientv3.OpPut("foo", "baz")).Commit()
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand All @@ -146,7 +147,7 @@ func TestBalancerUnderBlackholeNoKeepAliveTxn(t *testing.T) {
func TestBalancerUnderBlackholeNoKeepAliveLinearizableGet(t *testing.T) {
testBalancerUnderBlackholeNoKeepAlive(t, func(cli *clientv3.Client, ctx context.Context) error {
_, err := cli.Get(ctx, "a")
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) {
return errExpected
}
return err
Expand Down Expand Up @@ -207,7 +208,7 @@ func testBalancerUnderBlackholeNoKeepAlive(t *testing.T, op func(*clientv3.Clien
cancel()
if err == nil {
break
} else if err == errExpected {
} else if errors.Is(err, errExpected) {
t.Logf("#%d: current error %v", i, err)
} else {
t.Errorf("#%d: failed with error %v", i, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var errExpected = errors.New("expected error")

func isErrorExpected(err error) bool {
return clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) ||
err == rpctypes.ErrTimeout || err == rpctypes.ErrTimeoutDueToLeaderFail
errors.Is(err, rpctypes.ErrTimeout) || errors.Is(err, rpctypes.ErrTimeoutDueToLeaderFail)
}

// TestBalancerUnderNetworkPartitionPut tests when one member becomes isolated,
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestDropReadUnderNetworkPartition(t *testing.T) {
_, err = kvc.Get(ctx, "a")
cancel()
if err != nil {
if err == rpctypes.ErrTimeout {
if errors.Is(err, rpctypes.ErrTimeout) {
<-time.After(time.Second)
i++
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package connectivity_test
import (
"bytes"
"context"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestBalancerUnderServerShutdownWatch(t *testing.T) {
if err == nil {
break
}
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || err == rpctypes.ErrTimeout || err == rpctypes.ErrTimeoutDueToLeaderFail {
if clientv3test.IsClientTimeout(err) || clientv3test.IsServerCtxTimeout(err) || errors.Is(err, rpctypes.ErrTimeout) || errors.Is(err, rpctypes.ErrTimeoutDueToLeaderFail) {
continue
}
t.Fatal(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package recipes_test

import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
Expand Down Expand Up @@ -139,7 +140,7 @@ func testMutexTryLock(t *testing.T, lockers int, chooseClient func() *clientv3.C
case <-ctx.Done():
t.Errorf("Thread: %v, Context failed: %v", i, err)
}
} else if err == concurrency.ErrLocked {
} else if errors.Is(err, concurrency.ErrLocked) {
select {
case notlockedC <- m:
case <-ctx.Done():
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/v3_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestV3PutRestart(t *testing.T) {
defer cancel()
reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
_, err := kvc.Put(ctx, reqput)
if err != nil && err == ctx.Err() {
if err != nil && errors.Is(err, ctx.Err()) {
t.Fatalf("expected grpc error, got local ctx error (%v)", err)
}
}
Expand Down

0 comments on commit 1af229b

Please sign in to comment.