Unverified Commit 78d3ccf2 authored by randy's avatar randy Committed by GitHub

Merge pull request #2 from ztelur/refactor

1 solve test may fail problem
parents 9031b2c6 2f58aadc
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
package etcdv3 package gxetcd
import ( import (
"context" "context"
...@@ -31,17 +31,6 @@ import ( ...@@ -31,17 +31,6 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
) )
const (
// ConnDelay connection delay
ConnDelay = 3
// MaxFailTimes max failure times
MaxFailTimes = 15
// RegistryETCDV3Client client name
RegistryETCDV3Client = "etcd registry"
// MetadataETCDV3Client client name
MetadataETCDV3Client = "etcd metadata"
)
var ( var (
// ErrNilETCDV3Client raw client nil // ErrNilETCDV3Client raw client nil
ErrNilETCDV3Client = perrors.New("etcd raw client is nil") // full describe the ERR ErrNilETCDV3Client = perrors.New("etcd raw client is nil") // full describe the ERR
...@@ -49,46 +38,6 @@ var ( ...@@ -49,46 +38,6 @@ var (
ErrKVPairNotFound = perrors.New("k/v pair not found") ErrKVPairNotFound = perrors.New("k/v pair not found")
) )
// Options client configuration
type Options struct {
name string
endpoints []string
client *Client
timeout time.Duration
heartbeat int // heartbeat second
}
// Option will define a function of handling Options
type Option func(*Options)
// WithEndpoints sets etcd client endpoints
func WithEndpoints(endpoints ...string) Option {
return func(opt *Options) {
opt.endpoints = endpoints
}
}
// WithName sets etcd client name
func WithName(name string) Option {
return func(opt *Options) {
opt.name = name
}
}
// WithTimeout sets etcd client timeout
func WithTimeout(timeout time.Duration) Option {
return func(opt *Options) {
opt.timeout = timeout
}
}
// WithHeartbeat sets etcd client heartbeat
func WithHeartbeat(heartbeat int) Option {
return func(opt *Options) {
opt.heartbeat = heartbeat
}
}
// NewConfigClient create new Client // NewConfigClient create new Client
func NewConfigClient(opts ...Option) *Client { func NewConfigClient(opts ...Option) *Client {
options := &Options{ options := &Options{
...@@ -134,6 +83,7 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat ...@@ -134,6 +83,7 @@ func NewClient(name string, endpoints []string, timeout time.Duration, heartbeat
DialTimeout: timeout, DialTimeout: timeout,
DialOptions: []grpc.DialOption{grpc.WithBlock()}, DialOptions: []grpc.DialOption{grpc.WithBlock()},
}) })
if err != nil { if err != nil {
cancel() cancel()
return nil, perrors.WithMessage(err, "new raw client block connect to server") return nil, perrors.WithMessage(err, "new raw client block connect to server")
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
package etcdv3 package gxetcd
import ( import (
"net/url" "net/url"
...@@ -23,7 +23,6 @@ import ( ...@@ -23,7 +23,6 @@ import (
"path" "path"
"reflect" "reflect"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
) )
...@@ -289,12 +288,24 @@ func (suite *ClientTestSuite) TestClientWatch() { ...@@ -289,12 +288,24 @@ func (suite *ClientTestSuite) TestClientWatch() {
c := suite.client c := suite.client
t := suite.T() t := suite.T()
wg := sync.WaitGroup{}
wg.Add(1)
go func() { go func() {
time.Sleep(time.Second)
for _, tc := range tests {
k := tc.input.k
v := tc.input.v
if err := c.Create(k, v); err != nil {
t.Fatal(err)
}
if err := c.delete(k); err != nil {
t.Fatal(err)
}
}
defer wg.Done() c.Close()
}()
wc, err := c.watch(prefix) wc, err := c.watch(prefix)
if err != nil { if err != nil {
...@@ -321,25 +332,6 @@ func (suite *ClientTestSuite) TestClientWatch() { ...@@ -321,25 +332,6 @@ func (suite *ClientTestSuite) TestClientWatch() {
assert.Equal(t, 2, len(events)) assert.Equal(t, 2, len(events))
assert.Contains(t, events, eCreate) assert.Contains(t, events, eCreate)
assert.Contains(t, events, eDelete) assert.Contains(t, events, eDelete)
}()
for _, tc := range tests {
k := tc.input.k
v := tc.input.v
if err := c.Create(k, v); err != nil {
t.Fatal(err)
}
if err := c.delete(k); err != nil {
t.Fatal(err)
}
}
c.Close()
wg.Wait()
} }
...@@ -349,11 +341,14 @@ func (suite *ClientTestSuite) TestClientRegisterTemp() { ...@@ -349,11 +341,14 @@ func (suite *ClientTestSuite) TestClientRegisterTemp() {
observeC := suite.setUpClient() observeC := suite.setUpClient()
t := suite.T() t := suite.T()
wg := sync.WaitGroup{}
wg.Add(1)
go func() { go func() {
defer wg.Done() time.Sleep(2 * time.Second)
err := c.RegisterTemp("scott/wang", "test")
if err != nil {
t.Fatal(err)
}
c.Close()
}()
completePath := path.Join("scott", "wang") completePath := path.Join("scott", "wang")
wc, err := observeC.watch(completePath) wc, err := observeC.watch(completePath)
...@@ -382,17 +377,6 @@ func (suite *ClientTestSuite) TestClientRegisterTemp() { ...@@ -382,17 +377,6 @@ func (suite *ClientTestSuite) TestClientRegisterTemp() {
assert.Equal(t, 2, len(events)) assert.Equal(t, 2, len(events))
assert.Contains(t, events, eCreate) assert.Contains(t, events, eCreate)
assert.Contains(t, events, eDelete) assert.Contains(t, events, eDelete)
}()
err := c.RegisterTemp("scott/wang", "test")
if err != nil {
t.Fatal(err)
}
time.Sleep(2 * time.Second)
c.Close()
wg.Wait()
} }
func TestClientSuite(t *testing.T) { func TestClientSuite(t *testing.T) {
......
package gxetcd
import (
"time"
)
const (
// ConnDelay connection delay
ConnDelay = 3
// MaxFailTimes max failure times
MaxFailTimes = 15
// RegistryETCDV3Client client name
RegistryETCDV3Client = "etcd registry"
// MetadataETCDV3Client client name
MetadataETCDV3Client = "etcd metadata"
)
// Options client configuration
type Options struct {
name string
endpoints []string
client *Client
timeout time.Duration
heartbeat int // heartbeat second
}
// Option will define a function of handling Options
type Option func(*Options)
// WithEndpoints sets etcd client endpoints
func WithEndpoints(endpoints ...string) Option {
return func(opt *Options) {
opt.endpoints = endpoints
}
}
// WithName sets etcd client name
func WithName(name string) Option {
return func(opt *Options) {
opt.name = name
}
}
// WithTimeout sets etcd client timeout
func WithTimeout(timeout time.Duration) Option {
return func(opt *Options) {
opt.timeout = timeout
}
}
// WithHeartbeat sets etcd client heartbeat
func WithHeartbeat(heartbeat int) Option {
return func(opt *Options) {
opt.heartbeat = heartbeat
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment