From 77e4e87ee7c194530f0318ae8a8bf45eb350d42c Mon Sep 17 00:00:00 2001 From: wafuwafu13 Date: Mon, 19 Dec 2022 22:02:46 +0900 Subject: [PATCH 1/4] refactor: use assert Signed-off-by: wafuwafu13 --- client/v3/client_test.go | 43 ++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/client/v3/client_test.go b/client/v3/client_test.go index 8534536e3..3f367bdd6 100644 --- a/client/v3/client_test.go +++ b/client/v3/client_test.go @@ -17,12 +17,15 @@ package clientv3 import ( "context" "errors" + "fmt" "io" "net" "sync" "testing" "time" + "github.com/stretchr/testify/assert" + "go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/api/v3/v3rpc/rpctypes" "go.etcd.io/etcd/client/pkg/v3/testutil" @@ -152,23 +155,33 @@ func TestDialNoTimeout(t *testing.T) { } func TestIsHaltErr(t *testing.T) { - if !isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")) { - t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`) - } - if isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped) { - t.Errorf("error %v should not halt", rpctypes.ErrGRPCStopped) - } - if isHaltErr(context.TODO(), rpctypes.ErrGRPCNoLeader) { - t.Errorf("error %v should not halt", rpctypes.ErrGRPCNoLeader) - } + assert.Equal(t, + isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")), + true, + `error prefixed with "etcdserver: " should be Halted by default`, + ) + assert.Equal(t, + isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped), + false, + fmt.Sprintf("error %v should not halt", rpctypes.ErrGRPCStopped), + ) + assert.Equal(t, + isHaltErr(context.TODO(), rpctypes.ErrGRPCNoLeader), + false, + fmt.Sprintf("error %v should not halt", rpctypes.ErrGRPCNoLeader), + ) ctx, cancel := context.WithCancel(context.TODO()) - if isHaltErr(ctx, nil) { - t.Errorf("no error and active context should not be Halted") - } + assert.Equal(t, + isHaltErr(ctx, nil), + false, + "no error and active context should not be Halted", + ) cancel() - if !isHaltErr(ctx, nil) { - t.Errorf("cancel on context should be Halted") - } + assert.Equal(t, + isHaltErr(ctx, nil), + true, + "cancel on context should be Halted", + ) } func TestCloseCtxClient(t *testing.T) { From dc88d90763b98a229e3cdfd21617cd3137c785eb Mon Sep 17 00:00:00 2001 From: wafuwafu13 Date: Mon, 19 Dec 2022 22:18:03 +0900 Subject: [PATCH 2/4] test: add TestIsUnavailableErr Signed-off-by: wafuwafu13 --- client/v3/client_test.go | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/client/v3/client_test.go b/client/v3/client_test.go index 3f367bdd6..bdbb6e2e8 100644 --- a/client/v3/client_test.go +++ b/client/v3/client_test.go @@ -158,29 +158,59 @@ func TestIsHaltErr(t *testing.T) { assert.Equal(t, isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")), true, - `error prefixed with "etcdserver: " should be Halted by default`, + `error prefixed with "etcdserver: " should be halt error by default`, ) assert.Equal(t, isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped), false, - fmt.Sprintf("error %v should not halt", rpctypes.ErrGRPCStopped), + fmt.Sprintf(`error "%v" should not be halt error`, rpctypes.ErrGRPCStopped), ) assert.Equal(t, isHaltErr(context.TODO(), rpctypes.ErrGRPCNoLeader), false, - fmt.Sprintf("error %v should not halt", rpctypes.ErrGRPCNoLeader), + fmt.Sprintf(`error "%v" should not be halt error`, rpctypes.ErrGRPCNoLeader), ) ctx, cancel := context.WithCancel(context.TODO()) assert.Equal(t, isHaltErr(ctx, nil), false, - "no error and active context should not be Halted", + "no error and active context should be halt error", ) cancel() assert.Equal(t, isHaltErr(ctx, nil), true, - "cancel on context should be Halted", + "cancel on context should be halte error", + ) +} + +func TestIsUnavailableErr(t *testing.T) { + assert.Equal(t, + isUnavailableErr(context.TODO(), errors.New("etcdserver: some etcdserver error")), + false, + `error prefixed with "etcdserver: " should not be unavailable error by default`, + ) + assert.Equal(t, + isUnavailableErr(context.TODO(), rpctypes.ErrGRPCStopped), + true, + fmt.Sprintf(`error "%v" should be unavailable error`, rpctypes.ErrGRPCStopped), + ) + assert.Equal(t, + isUnavailableErr(context.TODO(), rpctypes.ErrGRPCNotCapable), + false, + fmt.Sprintf("error %v should not be unavailable error", rpctypes.ErrGRPCNotCapable), + ) + ctx, cancel := context.WithCancel(context.TODO()) + assert.Equal(t, + isUnavailableErr(ctx, nil), + false, + "no error and active context should not be unavailable error", + ) + cancel() + assert.Equal(t, + isUnavailableErr(ctx, nil), + false, + "cancel on context should not be unavailable error", ) } From 57413851b61fb3d9bcba834848f78e9a42cc2e3a Mon Sep 17 00:00:00 2001 From: wafuwafu13 Date: Tue, 20 Dec 2022 21:51:55 +0900 Subject: [PATCH 3/4] fix(client): change error message Signed-off-by: wafuwafu13 --- client/v3/client_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/v3/client_test.go b/client/v3/client_test.go index bdbb6e2e8..08bd0f455 100644 --- a/client/v3/client_test.go +++ b/client/v3/client_test.go @@ -158,7 +158,7 @@ func TestIsHaltErr(t *testing.T) { assert.Equal(t, isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")), true, - `error prefixed with "etcdserver: " should be halt error by default`, + "error created by errors.New should not be unavailable error", ) assert.Equal(t, isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped), @@ -188,7 +188,7 @@ func TestIsUnavailableErr(t *testing.T) { assert.Equal(t, isUnavailableErr(context.TODO(), errors.New("etcdserver: some etcdserver error")), false, - `error prefixed with "etcdserver: " should not be unavailable error by default`, + "error created by errors.New should not be unavailable error", ) assert.Equal(t, isUnavailableErr(context.TODO(), rpctypes.ErrGRPCStopped), From 2041d5f2457d45dcbb003d84c27901aa80db0818 Mon Sep 17 00:00:00 2001 From: wafuwafu13 Date: Tue, 20 Dec 2022 21:54:52 +0900 Subject: [PATCH 4/4] fix: change error message Signed-off-by: wafuwafu13 --- client/v3/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/v3/client_test.go b/client/v3/client_test.go index 08bd0f455..d8a4b07fe 100644 --- a/client/v3/client_test.go +++ b/client/v3/client_test.go @@ -158,7 +158,7 @@ func TestIsHaltErr(t *testing.T) { assert.Equal(t, isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")), true, - "error created by errors.New should not be unavailable error", + "error created by errors.New should be unavailable error", ) assert.Equal(t, isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped),