Merge pull request #15024 from wafuwafu13/client-isunavailableerr-test

test(client): add `TestIsUnavailableErr`
This commit is contained in:
Benjamin Wang 2022-12-21 06:42:44 +08:00 committed by GitHub
commit 4d9b709ce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,12 +17,15 @@ package clientv3
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"net" "net"
"sync" "sync"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes" "go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
"go.etcd.io/etcd/client/pkg/v3/testutil" "go.etcd.io/etcd/client/pkg/v3/testutil"
@ -152,23 +155,63 @@ func TestDialNoTimeout(t *testing.T) {
} }
func TestIsHaltErr(t *testing.T) { func TestIsHaltErr(t *testing.T) {
if !isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")) { assert.Equal(t,
t.Errorf(`error prefixed with "etcdserver: " should be Halted by default`) isHaltErr(context.TODO(), errors.New("etcdserver: some etcdserver error")),
} true,
if isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped) { "error created by errors.New should be unavailable error",
t.Errorf("error %v should not halt", rpctypes.ErrGRPCStopped) )
} assert.Equal(t,
if isHaltErr(context.TODO(), rpctypes.ErrGRPCNoLeader) { isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped),
t.Errorf("error %v should not halt", rpctypes.ErrGRPCNoLeader) false,
} 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 be halt error`, rpctypes.ErrGRPCNoLeader),
)
ctx, cancel := context.WithCancel(context.TODO()) ctx, cancel := context.WithCancel(context.TODO())
if isHaltErr(ctx, nil) { assert.Equal(t,
t.Errorf("no error and active context should not be Halted") isHaltErr(ctx, nil),
} false,
"no error and active context should be halt error",
)
cancel() cancel()
if !isHaltErr(ctx, nil) { assert.Equal(t,
t.Errorf("cancel on context should be Halted") isHaltErr(ctx, nil),
} true,
"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 created by errors.New should not be unavailable error",
)
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",
)
} }
func TestCloseCtxClient(t *testing.T) { func TestCloseCtxClient(t *testing.T) {