2016-05-13 06:51:48 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2014-09-21 22:26:59 +04:00
|
|
|
package discovery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-12-11 07:02:45 +03:00
|
|
|
"math"
|
2014-09-22 22:34:15 +04:00
|
|
|
"math/rand"
|
2014-10-13 22:24:23 +04:00
|
|
|
"net/http"
|
2014-11-01 00:12:40 +03:00
|
|
|
"reflect"
|
2014-09-22 22:34:15 +04:00
|
|
|
"sort"
|
2014-10-09 03:41:59 +04:00
|
|
|
"strconv"
|
2014-09-21 22:26:59 +04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-03-23 03:10:28 +03:00
|
|
|
"github.com/jonboulle/clockwork"
|
|
|
|
"golang.org/x/net/context"
|
2015-01-22 02:10:26 +03:00
|
|
|
|
2014-10-17 07:23:07 +04:00
|
|
|
"github.com/coreos/etcd/client"
|
2014-09-21 22:26:59 +04:00
|
|
|
)
|
|
|
|
|
2014-12-11 07:02:45 +03:00
|
|
|
const (
|
|
|
|
maxRetryInTest = 3
|
|
|
|
)
|
|
|
|
|
2014-11-07 03:35:24 +03:00
|
|
|
func TestNewProxyFuncUnset(t *testing.T) {
|
|
|
|
pf, err := newProxyFunc("")
|
2014-10-13 22:24:23 +04:00
|
|
|
if pf != nil {
|
|
|
|
t.Fatal("unexpected non-nil proxyFunc")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected non-nil err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 03:35:24 +03:00
|
|
|
func TestNewProxyFuncBad(t *testing.T) {
|
2014-10-13 22:24:23 +04:00
|
|
|
tests := []string{
|
|
|
|
"%%",
|
|
|
|
"http://foo.com/%1",
|
|
|
|
}
|
|
|
|
for i, in := range tests {
|
2014-11-07 03:35:24 +03:00
|
|
|
pf, err := newProxyFunc(in)
|
2014-10-13 22:24:23 +04:00
|
|
|
if pf != nil {
|
|
|
|
t.Errorf("#%d: unexpected non-nil proxyFunc", i)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("#%d: unexpected nil err", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 03:35:24 +03:00
|
|
|
func TestNewProxyFunc(t *testing.T) {
|
2014-10-13 22:24:23 +04:00
|
|
|
tests := map[string]string{
|
|
|
|
"bar.com": "http://bar.com",
|
|
|
|
"http://disco.foo.bar": "http://disco.foo.bar",
|
|
|
|
}
|
|
|
|
for in, w := range tests {
|
2014-11-07 03:35:24 +03:00
|
|
|
pf, err := newProxyFunc(in)
|
2014-10-13 22:24:23 +04:00
|
|
|
if pf == nil {
|
|
|
|
t.Errorf("%s: unexpected nil proxyFunc", in)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: unexpected non-nil err: %v", in, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
g, err := pf(&http.Request{})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%s: unexpected non-nil err: %v", in, err)
|
|
|
|
}
|
|
|
|
if g.String() != w {
|
2014-10-21 04:55:18 +04:00
|
|
|
t.Errorf("%s: proxyURL=%q, want %q", in, g, w)
|
2014-10-13 22:24:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 22:26:59 +04:00
|
|
|
func TestCheckCluster(t *testing.T) {
|
2014-10-30 04:30:59 +03:00
|
|
|
cluster := "/prefix/1000"
|
2014-09-21 22:26:59 +04:00
|
|
|
self := "/1000/1"
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
nodes []*client.Node
|
2014-12-11 07:02:45 +03:00
|
|
|
index uint64
|
2014-09-21 22:26:59 +04:00
|
|
|
werr error
|
|
|
|
wsize int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// self is in the size range
|
2014-10-21 11:17:36 +04:00
|
|
|
[]*client.Node{
|
2014-09-23 01:25:35 +04:00
|
|
|
{Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
|
2014-10-30 04:30:59 +03:00
|
|
|
{Key: "/1000/_config/"},
|
2014-09-21 22:26:59 +04:00
|
|
|
{Key: self, CreatedIndex: 2},
|
|
|
|
{Key: "/1000/2", CreatedIndex: 3},
|
|
|
|
{Key: "/1000/3", CreatedIndex: 4},
|
|
|
|
{Key: "/1000/4", CreatedIndex: 5},
|
|
|
|
},
|
2014-12-11 07:02:45 +03:00
|
|
|
5,
|
2014-09-21 22:26:59 +04:00
|
|
|
nil,
|
|
|
|
3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// self is in the size range
|
2014-10-21 11:17:36 +04:00
|
|
|
[]*client.Node{
|
2014-09-23 01:25:35 +04:00
|
|
|
{Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
|
2014-10-30 04:30:59 +03:00
|
|
|
{Key: "/1000/_config/"},
|
2014-09-21 22:26:59 +04:00
|
|
|
{Key: "/1000/2", CreatedIndex: 2},
|
|
|
|
{Key: "/1000/3", CreatedIndex: 3},
|
|
|
|
{Key: self, CreatedIndex: 4},
|
|
|
|
{Key: "/1000/4", CreatedIndex: 5},
|
|
|
|
},
|
2014-12-11 07:02:45 +03:00
|
|
|
5,
|
2014-09-21 22:26:59 +04:00
|
|
|
nil,
|
|
|
|
3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// self is out of the size range
|
2014-10-21 11:17:36 +04:00
|
|
|
[]*client.Node{
|
2014-09-23 01:25:35 +04:00
|
|
|
{Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
|
2014-10-30 04:30:59 +03:00
|
|
|
{Key: "/1000/_config/"},
|
2014-09-21 22:26:59 +04:00
|
|
|
{Key: "/1000/2", CreatedIndex: 2},
|
|
|
|
{Key: "/1000/3", CreatedIndex: 3},
|
|
|
|
{Key: "/1000/4", CreatedIndex: 4},
|
|
|
|
{Key: self, CreatedIndex: 5},
|
|
|
|
},
|
2014-12-11 07:02:45 +03:00
|
|
|
5,
|
2014-09-21 22:26:59 +04:00
|
|
|
ErrFullCluster,
|
|
|
|
3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// self is not in the cluster
|
2014-10-21 11:17:36 +04:00
|
|
|
[]*client.Node{
|
2014-09-23 01:25:35 +04:00
|
|
|
{Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
|
2014-10-30 04:30:59 +03:00
|
|
|
{Key: "/1000/_config/"},
|
2014-09-21 22:26:59 +04:00
|
|
|
{Key: "/1000/2", CreatedIndex: 2},
|
|
|
|
{Key: "/1000/3", CreatedIndex: 3},
|
|
|
|
},
|
2014-12-11 07:02:45 +03:00
|
|
|
3,
|
2014-09-21 22:26:59 +04:00
|
|
|
nil,
|
|
|
|
3,
|
|
|
|
},
|
|
|
|
{
|
2014-10-21 11:17:36 +04:00
|
|
|
[]*client.Node{
|
2014-09-23 01:25:35 +04:00
|
|
|
{Key: "/1000/_config/size", Value: "3", CreatedIndex: 1},
|
2014-10-30 04:30:59 +03:00
|
|
|
{Key: "/1000/_config/"},
|
2014-09-21 22:26:59 +04:00
|
|
|
{Key: "/1000/2", CreatedIndex: 2},
|
|
|
|
{Key: "/1000/3", CreatedIndex: 3},
|
|
|
|
{Key: "/1000/4", CreatedIndex: 4},
|
|
|
|
},
|
2014-12-11 07:02:45 +03:00
|
|
|
3,
|
2014-09-21 22:26:59 +04:00
|
|
|
ErrFullCluster,
|
|
|
|
3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// bad size key
|
2014-10-21 11:17:36 +04:00
|
|
|
[]*client.Node{
|
2014-09-23 01:25:35 +04:00
|
|
|
{Key: "/1000/_config/size", Value: "bad", CreatedIndex: 1},
|
2014-09-21 22:26:59 +04:00
|
|
|
},
|
2014-12-11 07:02:45 +03:00
|
|
|
0,
|
2014-09-23 00:54:21 +04:00
|
|
|
ErrBadSizeKey,
|
2014-09-21 22:26:59 +04:00
|
|
|
0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// no size key
|
2014-10-21 11:17:36 +04:00
|
|
|
[]*client.Node{},
|
2014-12-11 07:02:45 +03:00
|
|
|
0,
|
2014-09-21 22:26:59 +04:00
|
|
|
ErrSizeNotFound,
|
|
|
|
0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-09-23 00:54:21 +04:00
|
|
|
rs := make([]*client.Response, 0)
|
|
|
|
if len(tt.nodes) > 0 {
|
2014-12-11 07:02:45 +03:00
|
|
|
rs = append(rs, &client.Response{Node: tt.nodes[0], Index: tt.index})
|
2014-09-23 00:54:21 +04:00
|
|
|
rs = append(rs, &client.Response{
|
|
|
|
Node: &client.Node{
|
|
|
|
Key: cluster,
|
2014-10-30 04:30:59 +03:00
|
|
|
Nodes: tt.nodes[1:],
|
2014-09-23 00:54:21 +04:00
|
|
|
},
|
2014-12-11 07:02:45 +03:00
|
|
|
Index: tt.index,
|
2014-09-23 00:54:21 +04:00
|
|
|
})
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
2014-09-23 00:54:21 +04:00
|
|
|
c := &clientWithResp{rs: rs}
|
2015-02-18 00:15:48 +03:00
|
|
|
dBase := discovery{cluster: cluster, id: 1, c: c}
|
2014-09-23 00:54:21 +04:00
|
|
|
|
2014-10-09 05:58:19 +04:00
|
|
|
cRetry := &clientWithRetry{failTimes: 3}
|
2014-10-09 03:41:59 +04:00
|
|
|
cRetry.rs = rs
|
2014-10-15 22:53:05 +04:00
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
|
dRetry := discovery{cluster: cluster, id: 1, c: cRetry, clock: fc}
|
2014-10-09 03:41:59 +04:00
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
for _, d := range []discovery{dBase, dRetry} {
|
2014-10-15 22:53:05 +04:00
|
|
|
go func() {
|
2014-12-11 07:02:45 +03:00
|
|
|
for i := uint(1); i <= maxRetryInTest; i++ {
|
2014-10-15 22:53:05 +04:00
|
|
|
fc.BlockUntil(1)
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(time.Second * (0x1 << i))
|
2014-10-15 22:53:05 +04:00
|
|
|
}
|
|
|
|
}()
|
2014-12-11 07:02:45 +03:00
|
|
|
ns, size, index, err := d.checkCluster()
|
2014-10-09 03:41:59 +04:00
|
|
|
if err != tt.werr {
|
|
|
|
t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(ns, tt.nodes) {
|
|
|
|
t.Errorf("#%d: nodes = %v, want %v", i, ns, tt.nodes)
|
|
|
|
}
|
|
|
|
if size != tt.wsize {
|
|
|
|
t.Errorf("#%d: size = %v, want %d", i, size, tt.wsize)
|
|
|
|
}
|
2014-12-11 07:02:45 +03:00
|
|
|
if index != tt.index {
|
|
|
|
t.Errorf("#%d: index = %v, want %d", i, index, tt.index)
|
|
|
|
}
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitNodes(t *testing.T) {
|
2015-01-27 22:47:08 +03:00
|
|
|
all := []*client.Node{
|
2014-10-21 11:17:36 +04:00
|
|
|
0: {Key: "/1000/1", CreatedIndex: 2},
|
|
|
|
1: {Key: "/1000/2", CreatedIndex: 3},
|
|
|
|
2: {Key: "/1000/3", CreatedIndex: 4},
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
2015-01-27 22:47:08 +03:00
|
|
|
nodes []*client.Node
|
2014-09-21 22:26:59 +04:00
|
|
|
rs []*client.Response
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
all,
|
|
|
|
[]*client.Response{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
all[:1],
|
|
|
|
[]*client.Response{
|
|
|
|
{Node: &client.Node{Key: "/1000/2", CreatedIndex: 3}},
|
|
|
|
{Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
all[:2],
|
|
|
|
[]*client.Response{
|
|
|
|
{Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
|
|
|
|
},
|
|
|
|
},
|
2014-09-23 03:49:01 +04:00
|
|
|
{
|
|
|
|
append(all, &client.Node{Key: "/1000/4", CreatedIndex: 5}),
|
|
|
|
[]*client.Response{
|
|
|
|
{Node: &client.Node{Key: "/1000/3", CreatedIndex: 4}},
|
|
|
|
},
|
|
|
|
},
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
2014-10-09 03:41:59 +04:00
|
|
|
// Basic case
|
2015-01-22 02:10:26 +03:00
|
|
|
c := &clientWithResp{rs: nil, w: &watcherWithResp{rs: tt.rs}}
|
2015-02-18 00:15:48 +03:00
|
|
|
dBase := &discovery{cluster: "1000", c: c}
|
2014-10-09 03:41:59 +04:00
|
|
|
|
|
|
|
// Retry case
|
|
|
|
retryScanResp := make([]*client.Response, 0)
|
|
|
|
if len(tt.nodes) > 0 {
|
|
|
|
retryScanResp = append(retryScanResp, &client.Response{
|
|
|
|
Node: &client.Node{
|
|
|
|
Key: "1000",
|
2014-10-15 22:53:05 +04:00
|
|
|
Value: strconv.Itoa(3),
|
2014-10-09 03:41:59 +04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
retryScanResp = append(retryScanResp, &client.Response{
|
|
|
|
Node: &client.Node{
|
|
|
|
Nodes: tt.nodes,
|
|
|
|
},
|
|
|
|
})
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
2014-10-09 05:53:15 +04:00
|
|
|
cRetry := &clientWithResp{
|
|
|
|
rs: retryScanResp,
|
|
|
|
w: &watcherWithRetry{rs: tt.rs, failTimes: 2},
|
|
|
|
}
|
2014-10-15 22:53:05 +04:00
|
|
|
fc := clockwork.NewFakeClock()
|
2014-10-09 03:41:59 +04:00
|
|
|
dRetry := &discovery{
|
2014-10-15 22:53:05 +04:00
|
|
|
cluster: "1000",
|
|
|
|
c: cRetry,
|
|
|
|
clock: fc,
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-18 00:15:48 +03:00
|
|
|
for _, d := range []*discovery{dBase, dRetry} {
|
2014-10-15 22:53:05 +04:00
|
|
|
go func() {
|
2014-12-11 07:02:45 +03:00
|
|
|
for i := uint(1); i <= maxRetryInTest; i++ {
|
2014-10-15 22:53:05 +04:00
|
|
|
fc.BlockUntil(1)
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(time.Second * (0x1 << i))
|
2014-10-15 22:53:05 +04:00
|
|
|
}
|
|
|
|
}()
|
2014-12-11 07:02:45 +03:00
|
|
|
g, err := d.waitNodes(tt.nodes, 3, 0) // we do not care about index in this test
|
2014-10-15 22:53:05 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("#%d: err = %v, want %v", i, err, nil)
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
2014-10-15 22:53:05 +04:00
|
|
|
if !reflect.DeepEqual(g, all) {
|
|
|
|
t.Errorf("#%d: all = %v, want %v", i, g, all)
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateSelf(t *testing.T) {
|
|
|
|
rs := []*client.Response{{Node: &client.Node{Key: "1000/1", CreatedIndex: 2}}}
|
|
|
|
|
2015-01-22 02:10:26 +03:00
|
|
|
w := &watcherWithResp{rs: rs}
|
|
|
|
errw := &watcherWithErr{err: errors.New("watch err")}
|
2014-09-21 22:26:59 +04:00
|
|
|
|
2015-01-22 02:10:26 +03:00
|
|
|
c := &clientWithResp{rs: rs, w: w}
|
|
|
|
errc := &clientWithErr{err: errors.New("create err"), w: w}
|
2015-05-11 20:49:49 +03:00
|
|
|
errdupc := &clientWithErr{err: client.Error{Code: client.ErrorCodeNodeExist}}
|
2015-01-22 02:10:26 +03:00
|
|
|
errwc := &clientWithResp{rs: rs, w: errw}
|
2014-09-21 22:26:59 +04:00
|
|
|
|
|
|
|
tests := []struct {
|
2014-10-24 04:11:04 +04:00
|
|
|
c client.KeysAPI
|
2014-09-21 22:26:59 +04:00
|
|
|
werr error
|
|
|
|
}{
|
|
|
|
// no error
|
|
|
|
{c, nil},
|
|
|
|
// client.create returns an error
|
|
|
|
{errc, errc.err},
|
2016-02-01 08:42:39 +03:00
|
|
|
// watcher.next returns an error
|
2014-09-21 22:26:59 +04:00
|
|
|
{errwc, errw.err},
|
2016-02-01 08:42:39 +03:00
|
|
|
// parse key exist error to duplicate ID error
|
2015-05-11 20:49:49 +03:00
|
|
|
{errdupc, ErrDuplicateID},
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, tt := range tests {
|
|
|
|
d := discovery{cluster: "1000", c: tt.c}
|
2014-11-06 09:45:01 +03:00
|
|
|
if err := d.createSelf(""); err != tt.werr {
|
2014-09-21 22:26:59 +04:00
|
|
|
t.Errorf("#%d: err = %v, want %v", i, err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-04 16:20:45 +04:00
|
|
|
func TestNodesToCluster(t *testing.T) {
|
2015-07-03 04:16:49 +03:00
|
|
|
tests := []struct {
|
|
|
|
nodes []*client.Node
|
|
|
|
size int
|
|
|
|
wcluster string
|
|
|
|
werr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
[]*client.Node{
|
|
|
|
0: {Key: "/1000/1", Value: "1=http://1.1.1.1:2380", CreatedIndex: 1},
|
|
|
|
1: {Key: "/1000/2", Value: "2=http://2.2.2.2:2380", CreatedIndex: 2},
|
|
|
|
2: {Key: "/1000/3", Value: "3=http://3.3.3.3:2380", CreatedIndex: 3},
|
|
|
|
},
|
|
|
|
3,
|
|
|
|
"1=http://1.1.1.1:2380,2=http://2.2.2.2:2380,3=http://3.3.3.3:2380",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*client.Node{
|
|
|
|
0: {Key: "/1000/1", Value: "1=http://1.1.1.1:2380", CreatedIndex: 1},
|
|
|
|
1: {Key: "/1000/2", Value: "2=http://2.2.2.2:2380", CreatedIndex: 2},
|
|
|
|
2: {Key: "/1000/3", Value: "2=http://3.3.3.3:2380", CreatedIndex: 3},
|
|
|
|
},
|
|
|
|
3,
|
|
|
|
"1=http://1.1.1.1:2380,2=http://2.2.2.2:2380,2=http://3.3.3.3:2380",
|
|
|
|
ErrDuplicateName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]*client.Node{
|
|
|
|
0: {Key: "/1000/1", Value: "1=1.1.1.1:2380", CreatedIndex: 1},
|
|
|
|
1: {Key: "/1000/2", Value: "2=http://2.2.2.2:2380", CreatedIndex: 2},
|
|
|
|
2: {Key: "/1000/3", Value: "2=http://3.3.3.3:2380", CreatedIndex: 3},
|
|
|
|
},
|
|
|
|
3,
|
|
|
|
"1=1.1.1.1:2380,2=http://2.2.2.2:2380,2=http://3.3.3.3:2380",
|
|
|
|
ErrInvalidURL,
|
|
|
|
},
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
|
2015-07-03 04:16:49 +03:00
|
|
|
for i, tt := range tests {
|
|
|
|
cluster, err := nodesToCluster(tt.nodes, tt.size)
|
|
|
|
if err != tt.werr {
|
|
|
|
t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(cluster, tt.wcluster) {
|
|
|
|
t.Errorf("#%d: cluster = %v, want %v", i, cluster, tt.wcluster)
|
|
|
|
}
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 22:34:15 +04:00
|
|
|
func TestSortableNodes(t *testing.T) {
|
2015-01-27 22:47:08 +03:00
|
|
|
ns := []*client.Node{
|
2014-10-21 11:17:36 +04:00
|
|
|
0: {CreatedIndex: 5},
|
|
|
|
1: {CreatedIndex: 1},
|
|
|
|
2: {CreatedIndex: 3},
|
|
|
|
3: {CreatedIndex: 4},
|
2014-09-23 02:38:19 +04:00
|
|
|
}
|
|
|
|
// add some randomness
|
2014-09-22 22:34:15 +04:00
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
ns = append(ns, &client.Node{CreatedIndex: uint64(rand.Int31())})
|
|
|
|
}
|
2014-09-23 02:38:19 +04:00
|
|
|
sns := sortableNodes{ns}
|
2014-09-22 22:34:15 +04:00
|
|
|
sort.Sort(sns)
|
|
|
|
cis := make([]int, 0)
|
|
|
|
for _, n := range sns.Nodes {
|
|
|
|
cis = append(cis, int(n.CreatedIndex))
|
|
|
|
}
|
2016-04-03 04:27:54 +03:00
|
|
|
if !sort.IntsAreSorted(cis) {
|
2014-09-22 22:34:15 +04:00
|
|
|
t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
|
|
|
|
}
|
2014-09-23 02:38:19 +04:00
|
|
|
cis = make([]int, 0)
|
|
|
|
for _, n := range ns {
|
|
|
|
cis = append(cis, int(n.CreatedIndex))
|
|
|
|
}
|
2016-04-03 04:27:54 +03:00
|
|
|
if !sort.IntsAreSorted(cis) {
|
2014-09-23 02:38:19 +04:00
|
|
|
t.Errorf("isSorted = %v, want %v", sort.IntsAreSorted(cis), true)
|
|
|
|
}
|
2014-09-22 22:34:15 +04:00
|
|
|
}
|
|
|
|
|
2014-10-09 05:58:19 +04:00
|
|
|
func TestRetryFailure(t *testing.T) {
|
2014-12-11 07:02:45 +03:00
|
|
|
nRetries = maxRetryInTest
|
|
|
|
defer func() { nRetries = math.MaxUint32 }()
|
|
|
|
|
2014-10-09 05:58:19 +04:00
|
|
|
cluster := "1000"
|
|
|
|
c := &clientWithRetry{failTimes: 4}
|
2014-10-15 22:53:05 +04:00
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
|
d := discovery{
|
|
|
|
cluster: cluster,
|
|
|
|
id: 1,
|
|
|
|
c: c,
|
|
|
|
clock: fc,
|
|
|
|
}
|
|
|
|
go func() {
|
2014-12-11 07:02:45 +03:00
|
|
|
for i := uint(1); i <= maxRetryInTest; i++ {
|
2014-10-15 22:53:05 +04:00
|
|
|
fc.BlockUntil(1)
|
2014-10-17 07:23:07 +04:00
|
|
|
fc.Advance(time.Second * (0x1 << i))
|
2014-10-15 22:53:05 +04:00
|
|
|
}
|
|
|
|
}()
|
2014-12-11 07:02:45 +03:00
|
|
|
if _, _, _, err := d.checkCluster(); err != ErrTooManyRetries {
|
2014-10-09 05:58:19 +04:00
|
|
|
t.Errorf("err = %v, want %v", err, ErrTooManyRetries)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-21 22:26:59 +04:00
|
|
|
type clientWithResp struct {
|
|
|
|
rs []*client.Response
|
|
|
|
w client.Watcher
|
2015-01-22 02:10:26 +03:00
|
|
|
client.KeysAPI
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:22:16 +03:00
|
|
|
func (c *clientWithResp) Create(ctx context.Context, key string, value string) (*client.Response, error) {
|
2014-09-21 22:26:59 +04:00
|
|
|
if len(c.rs) == 0 {
|
|
|
|
return &client.Response{}, nil
|
|
|
|
}
|
|
|
|
r := c.rs[0]
|
|
|
|
c.rs = c.rs[1:]
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-01-28 23:34:06 +03:00
|
|
|
func (c *clientWithResp) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
|
2014-09-21 22:26:59 +04:00
|
|
|
if len(c.rs) == 0 {
|
2015-01-29 03:46:58 +03:00
|
|
|
return &client.Response{}, &client.Error{Code: client.ErrorCodeKeyNotFound}
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
r := c.rs[0]
|
2014-10-09 05:53:15 +04:00
|
|
|
c.rs = append(c.rs[1:], r)
|
2014-09-21 22:26:59 +04:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:54:52 +03:00
|
|
|
func (c *clientWithResp) Watcher(key string, opts *client.WatcherOptions) client.Watcher {
|
2014-09-21 22:26:59 +04:00
|
|
|
return c.w
|
|
|
|
}
|
|
|
|
|
|
|
|
type clientWithErr struct {
|
|
|
|
err error
|
|
|
|
w client.Watcher
|
2015-01-22 02:10:26 +03:00
|
|
|
client.KeysAPI
|
2014-09-21 22:26:59 +04:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:22:16 +03:00
|
|
|
func (c *clientWithErr) Create(ctx context.Context, key string, value string) (*client.Response, error) {
|
2014-09-21 22:26:59 +04:00
|
|
|
return &client.Response{}, c.err
|
|
|
|
}
|
|
|
|
|
2015-01-28 23:34:06 +03:00
|
|
|
func (c *clientWithErr) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
|
2014-09-21 22:26:59 +04:00
|
|
|
return &client.Response{}, c.err
|
|
|
|
}
|
|
|
|
|
2015-01-23 21:54:52 +03:00
|
|
|
func (c *clientWithErr) Watcher(key string, opts *client.WatcherOptions) client.Watcher {
|
2014-09-21 22:26:59 +04:00
|
|
|
return c.w
|
|
|
|
}
|
|
|
|
|
|
|
|
type watcherWithResp struct {
|
2015-01-22 02:10:26 +03:00
|
|
|
client.KeysAPI
|
2014-09-21 22:26:59 +04:00
|
|
|
rs []*client.Response
|
|
|
|
}
|
|
|
|
|
2014-11-01 00:12:40 +03:00
|
|
|
func (w *watcherWithResp) Next(context.Context) (*client.Response, error) {
|
2014-09-21 22:26:59 +04:00
|
|
|
if len(w.rs) == 0 {
|
|
|
|
return &client.Response{}, nil
|
|
|
|
}
|
|
|
|
r := w.rs[0]
|
|
|
|
w.rs = w.rs[1:]
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type watcherWithErr struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2014-11-01 00:12:40 +03:00
|
|
|
func (w *watcherWithErr) Next(context.Context) (*client.Response, error) {
|
2014-09-21 22:26:59 +04:00
|
|
|
return &client.Response{}, w.err
|
|
|
|
}
|
2014-10-09 03:41:59 +04:00
|
|
|
|
2014-10-15 22:53:05 +04:00
|
|
|
// clientWithRetry will timeout all requests up to failTimes
|
2014-10-09 03:41:59 +04:00
|
|
|
type clientWithRetry struct {
|
|
|
|
clientWithResp
|
2014-10-09 05:53:15 +04:00
|
|
|
failCount int
|
|
|
|
failTimes int
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
|
|
|
|
2015-01-21 23:22:16 +03:00
|
|
|
func (c *clientWithRetry) Create(ctx context.Context, key string, value string) (*client.Response, error) {
|
2014-10-09 05:53:15 +04:00
|
|
|
if c.failCount < c.failTimes {
|
|
|
|
c.failCount++
|
2015-07-20 03:24:42 +03:00
|
|
|
return nil, &client.ClusterError{Errors: []error{context.DeadlineExceeded}}
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
2015-01-21 23:22:16 +03:00
|
|
|
return c.clientWithResp.Create(ctx, key, value)
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
|
|
|
|
2015-01-28 23:34:06 +03:00
|
|
|
func (c *clientWithRetry) Get(ctx context.Context, key string, opts *client.GetOptions) (*client.Response, error) {
|
2014-10-09 05:53:15 +04:00
|
|
|
if c.failCount < c.failTimes {
|
|
|
|
c.failCount++
|
2015-07-20 03:24:42 +03:00
|
|
|
return nil, &client.ClusterError{Errors: []error{context.DeadlineExceeded}}
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
2015-01-28 23:34:06 +03:00
|
|
|
return c.clientWithResp.Get(ctx, key, opts)
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
|
|
|
|
2014-10-15 22:53:05 +04:00
|
|
|
// watcherWithRetry will timeout all requests up to failTimes
|
2014-10-09 03:41:59 +04:00
|
|
|
type watcherWithRetry struct {
|
2014-10-09 05:53:15 +04:00
|
|
|
rs []*client.Response
|
|
|
|
failCount int
|
|
|
|
failTimes int
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
|
|
|
|
2014-11-01 00:12:40 +03:00
|
|
|
func (w *watcherWithRetry) Next(context.Context) (*client.Response, error) {
|
2014-10-09 05:53:15 +04:00
|
|
|
if w.failCount < w.failTimes {
|
|
|
|
w.failCount++
|
2015-07-20 03:24:42 +03:00
|
|
|
return nil, &client.ClusterError{Errors: []error{context.DeadlineExceeded}}
|
2014-10-09 03:41:59 +04:00
|
|
|
}
|
|
|
|
if len(w.rs) == 0 {
|
|
|
|
return &client.Response{}, nil
|
|
|
|
}
|
|
|
|
r := w.rs[0]
|
|
|
|
w.rs = w.rs[1:]
|
|
|
|
return r, nil
|
|
|
|
}
|