From 1af229b8668c73b616d5cc846b6222b15e8bb20d Mon Sep 17 00:00:00 2001 From: redwrasse Date: Mon, 23 Sep 2024 21:55:52 -0700 Subject: [PATCH] tests: finish errors.Is equality cases Signed-off-by: redwrasse --- tests/e2e/ctl_v3_member_test.go | 5 +++-- tests/e2e/ctl_v3_snapshot_test.go | 3 ++- .../clientv3/concurrency/example_mutex_test.go | 3 ++- .../clientv3/connectivity/black_hole_test.go | 11 ++++++----- .../clientv3/connectivity/network_partition_test.go | 4 ++-- .../clientv3/connectivity/server_shutdown_test.go | 3 ++- .../clientv3/experimental/recipes/v3_lock_test.go | 3 ++- tests/integration/v3_grpc_test.go | 2 +- 8 files changed, 20 insertions(+), 14 deletions(-) diff --git a/tests/e2e/ctl_v3_member_test.go b/tests/e2e/ctl_v3_member_test.go index bd045e5f63b..08faf741902 100644 --- a/tests/e2e/ctl_v3_member_test.go +++ b/tests/e2e/ctl_v3_member_test.go @@ -17,6 +17,7 @@ package e2e import ( "context" "encoding/json" + "errors" "fmt" "io" "reflect" @@ -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 @@ -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) diff --git a/tests/e2e/ctl_v3_snapshot_test.go b/tests/e2e/ctl_v3_snapshot_test.go index bb3f1f06fe0..b9ef782ffcf 100644 --- a/tests/e2e/ctl_v3_snapshot_test.go +++ b/tests/e2e/ctl_v3_snapshot_test.go @@ -17,6 +17,7 @@ package e2e import ( "context" "encoding/json" + "errors" "fmt" "io" "os" @@ -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 diff --git a/tests/integration/clientv3/concurrency/example_mutex_test.go b/tests/integration/clientv3/concurrency/example_mutex_test.go index c3b593aed9b..eba8c543f2c 100644 --- a/tests/integration/clientv3/concurrency/example_mutex_test.go +++ b/tests/integration/clientv3/concurrency/example_mutex_test.go @@ -16,6 +16,7 @@ package concurrency_test import ( "context" + "errors" "fmt" "log" @@ -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") } diff --git a/tests/integration/clientv3/connectivity/black_hole_test.go b/tests/integration/clientv3/connectivity/black_hole_test.go index c0e16aefe1d..e1fc1c57b97 100644 --- a/tests/integration/clientv3/connectivity/black_hole_test.go +++ b/tests/integration/clientv3/connectivity/black_hole_test.go @@ -18,6 +18,7 @@ package connectivity_test import ( "context" + "errors" "testing" "time" @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/tests/integration/clientv3/connectivity/network_partition_test.go b/tests/integration/clientv3/connectivity/network_partition_test.go index cb421a84e4b..6c99c32d04c 100644 --- a/tests/integration/clientv3/connectivity/network_partition_test.go +++ b/tests/integration/clientv3/connectivity/network_partition_test.go @@ -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, @@ -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 diff --git a/tests/integration/clientv3/connectivity/server_shutdown_test.go b/tests/integration/clientv3/connectivity/server_shutdown_test.go index a75efa0f8fe..3afc8eb4c01 100644 --- a/tests/integration/clientv3/connectivity/server_shutdown_test.go +++ b/tests/integration/clientv3/connectivity/server_shutdown_test.go @@ -17,6 +17,7 @@ package connectivity_test import ( "bytes" "context" + "errors" "fmt" "testing" "time" @@ -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) diff --git a/tests/integration/clientv3/experimental/recipes/v3_lock_test.go b/tests/integration/clientv3/experimental/recipes/v3_lock_test.go index 79f61662032..1fcbc46e144 100644 --- a/tests/integration/clientv3/experimental/recipes/v3_lock_test.go +++ b/tests/integration/clientv3/experimental/recipes/v3_lock_test.go @@ -16,6 +16,7 @@ package recipes_test import ( "context" + "errors" "fmt" "math/rand" "sync" @@ -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(): diff --git a/tests/integration/v3_grpc_test.go b/tests/integration/v3_grpc_test.go index 60b5eaccb64..8efeb5d522d 100644 --- a/tests/integration/v3_grpc_test.go +++ b/tests/integration/v3_grpc_test.go @@ -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) } }