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 (
"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,63 @@ 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 created by errors.New should be unavailable error",
)
assert.Equal(t,
isHaltErr(context.TODO(), rpctypes.ErrGRPCStopped),
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())
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 be halt error",
)
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 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) {