lease_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "testing"
  17. "time"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  21. "github.com/coreos/etcd/integration"
  22. "github.com/coreos/etcd/pkg/testutil"
  23. )
  24. func TestLeaseCreate(t *testing.T) {
  25. defer testutil.AfterTest(t)
  26. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  27. defer clus.Terminate(t)
  28. lapi := clientv3.NewLease(clus.RandClient())
  29. defer lapi.Close()
  30. kv := clientv3.NewKV(clus.RandClient())
  31. resp, err := lapi.Create(context.Background(), 10)
  32. if err != nil {
  33. t.Errorf("failed to create lease %v", err)
  34. }
  35. _, err = kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(resp.ID)))
  36. if err != nil {
  37. t.Fatalf("failed to create key with lease %v", err)
  38. }
  39. }
  40. func TestLeaseRevoke(t *testing.T) {
  41. defer testutil.AfterTest(t)
  42. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  43. defer clus.Terminate(t)
  44. lapi := clientv3.NewLease(clus.RandClient())
  45. defer lapi.Close()
  46. kv := clientv3.NewKV(clus.RandClient())
  47. resp, err := lapi.Create(context.Background(), 10)
  48. if err != nil {
  49. t.Errorf("failed to create lease %v", err)
  50. }
  51. _, err = lapi.Revoke(context.Background(), clientv3.LeaseID(resp.ID))
  52. if err != nil {
  53. t.Errorf("failed to revoke lease %v", err)
  54. }
  55. _, err = kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(resp.ID)))
  56. if err != rpctypes.ErrLeaseNotFound {
  57. t.Fatalf("err = %v, want %v", err, rpctypes.ErrLeaseNotFound)
  58. }
  59. }
  60. func TestLeaseKeepAliveOnce(t *testing.T) {
  61. defer testutil.AfterTest(t)
  62. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  63. defer clus.Terminate(t)
  64. lapi := clientv3.NewLease(clus.RandClient())
  65. defer lapi.Close()
  66. resp, err := lapi.Create(context.Background(), 10)
  67. if err != nil {
  68. t.Errorf("failed to create lease %v", err)
  69. }
  70. _, err = lapi.KeepAliveOnce(context.Background(), clientv3.LeaseID(resp.ID))
  71. if err != nil {
  72. t.Errorf("failed to keepalive lease %v", err)
  73. }
  74. }
  75. func TestLeaseKeepAlive(t *testing.T) {
  76. defer testutil.AfterTest(t)
  77. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  78. defer clus.Terminate(t)
  79. lapi := clientv3.NewLease(clus.RandClient())
  80. resp, err := lapi.Create(context.Background(), 10)
  81. if err != nil {
  82. t.Errorf("failed to create lease %v", err)
  83. }
  84. rc, kerr := lapi.KeepAlive(context.Background(), clientv3.LeaseID(resp.ID))
  85. if kerr != nil {
  86. t.Errorf("failed to keepalive lease %v", kerr)
  87. }
  88. kresp, ok := <-rc
  89. if !ok {
  90. t.Errorf("chan is closed, want not closed")
  91. }
  92. if kresp.ID != resp.ID {
  93. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  94. }
  95. lapi.Close()
  96. _, ok = <-rc
  97. if ok {
  98. t.Errorf("chan is not closed, want lease Close() closes chan")
  99. }
  100. }
  101. // TODO: add a client that can connect to all the members of cluster via unix sock.
  102. // TODO: test handle more complicated failures.
  103. func TestLeaseKeepAliveHandleFailure(t *testing.T) {
  104. t.Skip("test it when we have a cluster client")
  105. defer testutil.AfterTest(t)
  106. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  107. defer clus.Terminate(t)
  108. // TODO: change this line to get a cluster client
  109. lapi := clientv3.NewLease(clus.RandClient())
  110. resp, err := lapi.Create(context.Background(), 10)
  111. if err != nil {
  112. t.Errorf("failed to create lease %v", err)
  113. }
  114. rc, kerr := lapi.KeepAlive(context.Background(), clientv3.LeaseID(resp.ID))
  115. if kerr != nil {
  116. t.Errorf("failed to keepalive lease %v", kerr)
  117. }
  118. kresp := <-rc
  119. if kresp.ID != resp.ID {
  120. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  121. }
  122. // restart the connected member.
  123. clus.Members[0].Stop(t)
  124. select {
  125. case <-rc:
  126. t.Fatalf("unexpected keepalive")
  127. case <-time.After(10*time.Second/3 + 1):
  128. }
  129. // recover the member.
  130. clus.Members[0].Restart(t)
  131. kresp = <-rc
  132. if kresp.ID != resp.ID {
  133. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  134. }
  135. lapi.Close()
  136. _, ok := <-rc
  137. if ok {
  138. t.Errorf("chan is not closed, want lease Close() closes chan")
  139. }
  140. }