Skip to content

Commit

Permalink
Merge pull request etcd-io#18 from heyitsanthony/leasing-06-12-17.2
Browse files Browse the repository at this point in the history
more integration tests
  • Loading branch information
visheshnp authored Jun 13, 2017
2 parents d7613ed + a21b39e commit a11d3f7
Showing 1 changed file with 119 additions and 5 deletions.
124 changes: 119 additions & 5 deletions clientv3/integration/leasing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ func TestLeasingRevGet(t *testing.T) {
}
}

// TestLeasingGetKeysOnly checks only keys are returnd with keys only
func TestLeasingGetKeysOnly(t *testing.T) {
// TestLeasingGetWithOpts checks options that can be served through the cache do not depend on the server.
func TestLeasingGetWithOpts(t *testing.T) {
defer testutil.AfterTest(t)
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
defer clus.Terminate(t)
Expand All @@ -330,13 +330,33 @@ func TestLeasingGetKeysOnly(t *testing.T) {
if _, err := clus.Client(0).Put(context.TODO(), "k", "abc"); err != nil {
t.Fatal(err)
}
if _, err := lkv.Get(context.TODO(), "k", clientv3.WithKeysOnly(), clientv3.WithRev(1)); err != nil {
// in cache
if _, err := lkv.Get(context.TODO(), "k", clientv3.WithKeysOnly()); err != nil {
t.Fatal(err)
}

clus.Members[0].Stop(t)

if _, err := lkv.Get(context.TODO(), "k", clientv3.WithKeysOnly()); err != nil {
opts := []clientv3.OpOption{
clientv3.WithKeysOnly(),
clientv3.WithLimit(1),
clientv3.WithMinCreateRev(1),
clientv3.WithMinModRev(1),
clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend),
clientv3.WithSerializable(),
}
for _, opt := range opts {
if _, err := lkv.Get(context.TODO(), "k", opt); err != nil {
t.Fatal(err)
}
}

getOpts := []clientv3.OpOption{}
for i := 0; i < len(opts); i++ {
getOpts = append(getOpts, opts[len(opts)%rand.Intn(len(opts))])
}
getOpts = getOpts[:rand.Intn(len(opts))]
if _, err := lkv.Get(context.TODO(), "k", getOpts...); err != nil {
t.Fatal(err)
}
}
Expand Down Expand Up @@ -733,6 +753,41 @@ func TestLeasingTxnOwnerIf(t *testing.T) {
}
}

func TestLeasingTxnCancel(t *testing.T) {
defer testutil.AfterTest(t)
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
defer clus.Terminate(t)

lkv1, err := leasing.NewleasingKV(clus.Client(0), "pfx/")
if err != nil {
t.Fatal(err)
}
lkv2, err := leasing.NewleasingKV(clus.Client(1), "pfx/")
if err != nil {
t.Fatal(err)
}

// acquire lease but disconnect so no revoke in time
if _, err := lkv1.Get(context.TODO(), "k"); err != nil {
t.Fatal(err)
}
clus.Members[0].Stop(t)

// wait for leader election, if any
if _, err := clus.Client(1).Get(context.TODO(), "abc"); err != nil {
t.Fatal(err)
}

ctx, cancel := context.WithCancel(context.TODO())
go func() {
time.Sleep(100 * time.Millisecond)
cancel()
}()
if _, err := lkv2.Txn(ctx).Then(clientv3.OpPut("k", "v")).Commit(); err != context.Canceled {
t.Fatal("expected %v, got %v", context.Canceled, err)
}
}

func TestLeasingTxnNonOwnerPut(t *testing.T) {
defer testutil.AfterTest(t)
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
Expand Down Expand Up @@ -1116,7 +1171,7 @@ func TestLeasingReconnectOwnerRevoke(t *testing.T) {
go func() {
defer close(pdonec)
if _, err := lkv2.Put(cctx, "k", "v"); err != nil {
t.Fatal(err)
t.Log(err)
}
}()
select {
Expand Down Expand Up @@ -1177,6 +1232,43 @@ func TestLeasingReconnectOwnerPut(t *testing.T) {
}
}

// TestLeasingReconnectTxn checks that txns are resilient to disconnects.
func TestLeasingReconnectTxn(t *testing.T) {
defer testutil.AfterTest(t)
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
defer clus.Terminate(t)

lkv, err := leasing.NewleasingKV(clus.Client(0), "foo/")
if err != nil {
t.Fatal(err)
}
if _, err := lkv.Get(context.TODO(), "k"); err != nil {
t.Fatal(err)
}

donec := make(chan struct{})
go func() {
defer close(donec)
clus.Members[0].DropConnections()
go func() {
defer close(donec)
for i := 0; i < 10; i++ {
clus.Members[0].DropConnections()
time.Sleep(time.Millisecond)
}
}()
}()

_, lerr := lkv.Txn(context.TODO()).
If(clientv3.Compare(clientv3.Version("k"), "=", 0)).
Then(clientv3.OpGet("k")).
Commit()
<-donec
if lerr != nil {
t.Fatal(lerr)
}
}

// TestLeasingReconnectNonOwnerGet checks a get error on an owner will
// not cause inconsistency between the server and the client.
func TestLeasingReconnectNonOwnerGet(t *testing.T) {
Expand Down Expand Up @@ -1231,6 +1323,28 @@ func TestLeasingReconnectNonOwnerGet(t *testing.T) {
}
}

func TestLeasingDo(t *testing.T) {
defer testutil.AfterTest(t)
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
defer clus.Terminate(t)

lkv, err := leasing.NewleasingKV(clus.Client(0), "foo/")
if err != nil {
t.Fatal(err)
}

ops := []clientv3.Op{
clientv3.OpGet("a"),
clientv3.OpPut("b", "v"),
clientv3.OpDelete("a"),
}
for i, op := range ops {
if _, err := lkv.Do(context.TODO(), op); err != nil {
t.Errorf("#%d: failed (%v)", i, err)
}
}
}

func randCmps(pfx string, dat []*clientv3.PutResponse) (cmps []clientv3.Cmp, then bool) {
for i := 0; i < len(dat); i++ {
idx := rand.Intn(len(dat))
Expand Down

0 comments on commit a11d3f7

Please sign in to comment.