lease_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2016 The etcd Authors
  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/clientv3"
  19. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  20. "github.com/coreos/etcd/integration"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. "golang.org/x/net/context"
  23. )
  24. func TestLeaseNotFoundError(t *testing.T) {
  25. defer testutil.AfterTest(t)
  26. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  27. defer clus.Terminate(t)
  28. lapi := clientv3.NewLease(clus.RandClient())
  29. defer lapi.Close()
  30. kv := clientv3.NewKV(clus.RandClient())
  31. _, err := kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(500)))
  32. if err != rpctypes.ErrLeaseNotFound {
  33. t.Fatalf("expected %v, got %v", rpctypes.ErrLeaseNotFound, err)
  34. }
  35. }
  36. func TestLeaseGrant(t *testing.T) {
  37. defer testutil.AfterTest(t)
  38. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  39. defer clus.Terminate(t)
  40. lapi := clientv3.NewLease(clus.RandClient())
  41. defer lapi.Close()
  42. kv := clientv3.NewKV(clus.RandClient())
  43. resp, err := lapi.Grant(context.Background(), 10)
  44. if err != nil {
  45. t.Errorf("failed to create lease %v", err)
  46. }
  47. _, err = kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
  48. if err != nil {
  49. t.Fatalf("failed to create key with lease %v", err)
  50. }
  51. }
  52. func TestLeaseRevoke(t *testing.T) {
  53. defer testutil.AfterTest(t)
  54. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  55. defer clus.Terminate(t)
  56. lapi := clientv3.NewLease(clus.RandClient())
  57. defer lapi.Close()
  58. kv := clientv3.NewKV(clus.RandClient())
  59. resp, err := lapi.Grant(context.Background(), 10)
  60. if err != nil {
  61. t.Errorf("failed to create lease %v", err)
  62. }
  63. _, err = lapi.Revoke(context.Background(), clientv3.LeaseID(resp.ID))
  64. if err != nil {
  65. t.Errorf("failed to revoke lease %v", err)
  66. }
  67. _, err = kv.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
  68. if err != rpctypes.ErrLeaseNotFound {
  69. t.Fatalf("err = %v, want %v", err, rpctypes.ErrLeaseNotFound)
  70. }
  71. }
  72. func TestLeaseKeepAliveOnce(t *testing.T) {
  73. defer testutil.AfterTest(t)
  74. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  75. defer clus.Terminate(t)
  76. lapi := clientv3.NewLease(clus.RandClient())
  77. defer lapi.Close()
  78. resp, err := lapi.Grant(context.Background(), 10)
  79. if err != nil {
  80. t.Errorf("failed to create lease %v", err)
  81. }
  82. _, err = lapi.KeepAliveOnce(context.Background(), resp.ID)
  83. if err != nil {
  84. t.Errorf("failed to keepalive lease %v", err)
  85. }
  86. _, err = lapi.KeepAliveOnce(context.Background(), clientv3.LeaseID(0))
  87. if err != rpctypes.ErrLeaseNotFound {
  88. t.Errorf("expected %v, got %v", rpctypes.ErrLeaseNotFound, err)
  89. }
  90. }
  91. func TestLeaseKeepAlive(t *testing.T) {
  92. defer testutil.AfterTest(t)
  93. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  94. defer clus.Terminate(t)
  95. lapi := clientv3.NewLease(clus.RandClient())
  96. resp, err := lapi.Grant(context.Background(), 10)
  97. if err != nil {
  98. t.Errorf("failed to create lease %v", err)
  99. }
  100. rc, kerr := lapi.KeepAlive(context.Background(), resp.ID)
  101. if kerr != nil {
  102. t.Errorf("failed to keepalive lease %v", kerr)
  103. }
  104. kresp, ok := <-rc
  105. if !ok {
  106. t.Errorf("chan is closed, want not closed")
  107. }
  108. if kresp.ID != resp.ID {
  109. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  110. }
  111. lapi.Close()
  112. _, ok = <-rc
  113. if ok {
  114. t.Errorf("chan is not closed, want lease Close() closes chan")
  115. }
  116. }
  117. // TODO: add a client that can connect to all the members of cluster via unix sock.
  118. // TODO: test handle more complicated failures.
  119. func TestLeaseKeepAliveHandleFailure(t *testing.T) {
  120. t.Skip("test it when we have a cluster client")
  121. defer testutil.AfterTest(t)
  122. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3})
  123. defer clus.Terminate(t)
  124. // TODO: change this line to get a cluster client
  125. lapi := clientv3.NewLease(clus.RandClient())
  126. resp, err := lapi.Grant(context.Background(), 10)
  127. if err != nil {
  128. t.Errorf("failed to create lease %v", err)
  129. }
  130. rc, kerr := lapi.KeepAlive(context.Background(), resp.ID)
  131. if kerr != nil {
  132. t.Errorf("failed to keepalive lease %v", kerr)
  133. }
  134. kresp := <-rc
  135. if kresp.ID != resp.ID {
  136. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  137. }
  138. // restart the connected member.
  139. clus.Members[0].Stop(t)
  140. select {
  141. case <-rc:
  142. t.Fatalf("unexpected keepalive")
  143. case <-time.After(10*time.Second/3 + 1):
  144. }
  145. // recover the member.
  146. clus.Members[0].Restart(t)
  147. kresp = <-rc
  148. if kresp.ID != resp.ID {
  149. t.Errorf("ID = %x, want %x", kresp.ID, resp.ID)
  150. }
  151. lapi.Close()
  152. _, ok := <-rc
  153. if ok {
  154. t.Errorf("chan is not closed, want lease Close() closes chan")
  155. }
  156. }
  157. type leaseCh struct {
  158. lid clientv3.LeaseID
  159. ch <-chan *clientv3.LeaseKeepAliveResponse
  160. }
  161. // TestLeaseKeepAliveNotFound ensures a revoked lease won't stop other keep alives
  162. func TestLeaseKeepAliveNotFound(t *testing.T) {
  163. defer testutil.AfterTest(t)
  164. clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
  165. defer clus.Terminate(t)
  166. cli := clus.RandClient()
  167. lchs := []leaseCh{}
  168. for i := 0; i < 3; i++ {
  169. resp, rerr := cli.Grant(context.TODO(), 5)
  170. if rerr != nil {
  171. t.Fatal(rerr)
  172. }
  173. kach, kaerr := cli.KeepAlive(context.Background(), resp.ID)
  174. if kaerr != nil {
  175. t.Fatal(kaerr)
  176. }
  177. lchs = append(lchs, leaseCh{resp.ID, kach})
  178. }
  179. if _, err := cli.Revoke(context.TODO(), lchs[1].lid); err != nil {
  180. t.Fatal(err)
  181. }
  182. <-lchs[0].ch
  183. if _, ok := <-lchs[0].ch; !ok {
  184. t.Fatalf("closed keepalive on wrong lease")
  185. }
  186. timec := time.After(5 * time.Second)
  187. for range lchs[1].ch {
  188. select {
  189. case <-timec:
  190. t.Fatalf("revoke did not close keep alive")
  191. default:
  192. }
  193. }
  194. }