lessor_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright 2015 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 lease
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path"
  19. "reflect"
  20. "sort"
  21. "testing"
  22. "time"
  23. "github.com/coreos/etcd/mvcc/backend"
  24. )
  25. const minLeaseTTL = int64(5)
  26. // TestLessorGrant ensures Lessor can grant wanted lease.
  27. // The granted lease should have a unique ID with a term
  28. // that is greater than minLeaseTTL.
  29. func TestLessorGrant(t *testing.T) {
  30. dir, be := NewTestBackend(t)
  31. defer os.RemoveAll(dir)
  32. defer be.Close()
  33. le := newLessor(be, minLeaseTTL)
  34. le.Promote(0)
  35. l, err := le.Grant(1, 1)
  36. if err != nil {
  37. t.Fatalf("could not grant lease 1 (%v)", err)
  38. }
  39. gl := le.get(l.ID)
  40. if !reflect.DeepEqual(gl, l) {
  41. t.Errorf("lease = %v, want %v", gl, l)
  42. }
  43. if l.expiry.Sub(time.Now()) < time.Duration(minLeaseTTL)*time.Second-time.Second {
  44. t.Errorf("term = %v, want at least %v", l.expiry.Sub(time.Now()), time.Duration(minLeaseTTL)*time.Second-time.Second)
  45. }
  46. nl, err := le.Grant(1, 1)
  47. if err == nil {
  48. t.Errorf("allocated the same lease")
  49. }
  50. nl, err = le.Grant(2, 1)
  51. if err != nil {
  52. t.Errorf("could not grant lease 2 (%v)", err)
  53. }
  54. if nl.ID == l.ID {
  55. t.Errorf("new lease.id = %x, want != %x", nl.ID, l.ID)
  56. }
  57. be.BatchTx().Lock()
  58. _, vs := be.BatchTx().UnsafeRange(leaseBucketName, int64ToBytes(int64(l.ID)), nil, 0)
  59. if len(vs) != 1 {
  60. t.Errorf("len(vs) = %d, want 1", len(vs))
  61. }
  62. be.BatchTx().Unlock()
  63. }
  64. // TestLessorRevoke ensures Lessor can revoke a lease.
  65. // The items in the revoked lease should be removed from
  66. // the backend.
  67. // The revoked lease cannot be got from Lessor again.
  68. func TestLessorRevoke(t *testing.T) {
  69. dir, be := NewTestBackend(t)
  70. defer os.RemoveAll(dir)
  71. defer be.Close()
  72. fd := &fakeDeleter{}
  73. le := newLessor(be, minLeaseTTL)
  74. le.SetRangeDeleter(fd)
  75. // grant a lease with long term (100 seconds) to
  76. // avoid early termination during the test.
  77. l, err := le.Grant(1, 100)
  78. if err != nil {
  79. t.Fatalf("could not grant lease for 100s ttl (%v)", err)
  80. }
  81. items := []LeaseItem{
  82. {"foo"},
  83. {"bar"},
  84. }
  85. if err = le.Attach(l.ID, items); err != nil {
  86. t.Fatalf("failed to attach items to the lease: %v", err)
  87. }
  88. if err = le.Revoke(l.ID); err != nil {
  89. t.Fatal("failed to revoke lease:", err)
  90. }
  91. if le.get(l.ID) != nil {
  92. t.Errorf("got revoked lease %x", l.ID)
  93. }
  94. wdeleted := []string{"bar_", "foo_"}
  95. sort.Sort(sort.StringSlice(fd.deleted))
  96. if !reflect.DeepEqual(fd.deleted, wdeleted) {
  97. t.Errorf("deleted= %v, want %v", fd.deleted, wdeleted)
  98. }
  99. be.BatchTx().Lock()
  100. _, vs := be.BatchTx().UnsafeRange(leaseBucketName, int64ToBytes(int64(l.ID)), nil, 0)
  101. if len(vs) != 0 {
  102. t.Errorf("len(vs) = %d, want 0", len(vs))
  103. }
  104. be.BatchTx().Unlock()
  105. }
  106. // TestLessorRenew ensures Lessor can renew an existing lease.
  107. func TestLessorRenew(t *testing.T) {
  108. dir, be := NewTestBackend(t)
  109. defer be.Close()
  110. defer os.RemoveAll(dir)
  111. le := newLessor(be, minLeaseTTL)
  112. le.Promote(0)
  113. l, err := le.Grant(1, minLeaseTTL)
  114. if err != nil {
  115. t.Fatalf("failed to grant lease (%v)", err)
  116. }
  117. // manually change the ttl field
  118. l.TTL = 10
  119. ttl, err := le.Renew(l.ID)
  120. if err != nil {
  121. t.Fatalf("failed to renew lease (%v)", err)
  122. }
  123. if ttl != l.TTL {
  124. t.Errorf("ttl = %d, want %d", ttl, l.TTL)
  125. }
  126. l = le.get(l.ID)
  127. if l.expiry.Sub(time.Now()) < 9*time.Second {
  128. t.Errorf("failed to renew the lease")
  129. }
  130. }
  131. func TestLessorDetach(t *testing.T) {
  132. dir, be := NewTestBackend(t)
  133. defer os.RemoveAll(dir)
  134. defer be.Close()
  135. fd := &fakeDeleter{}
  136. le := newLessor(be, minLeaseTTL)
  137. le.SetRangeDeleter(fd)
  138. // grant a lease with long term (100 seconds) to
  139. // avoid early termination during the test.
  140. l, err := le.Grant(1, 100)
  141. if err != nil {
  142. t.Fatalf("could not grant lease for 100s ttl (%v)", err)
  143. }
  144. items := []LeaseItem{
  145. {"foo"},
  146. {"bar"},
  147. }
  148. if err := le.Attach(l.ID, items); err != nil {
  149. t.Fatalf("failed to attach items to the lease: %v", err)
  150. }
  151. if err := le.Detach(l.ID, items[0:1]); err != nil {
  152. t.Fatalf("failed to de-attach items to the lease: %v", err)
  153. }
  154. l = le.Lookup(l.ID)
  155. if len(l.itemSet) != 1 {
  156. t.Fatalf("len(l.itemSet) = %d, failed to de-attach items", len(l.itemSet))
  157. }
  158. if _, ok := l.itemSet[LeaseItem{"bar"}]; !ok {
  159. t.Fatalf("de-attached wrong item, want %q exists", "bar")
  160. }
  161. }
  162. // TestLessorRecover ensures Lessor recovers leases from
  163. // persist backend.
  164. func TestLessorRecover(t *testing.T) {
  165. dir, be := NewTestBackend(t)
  166. defer os.RemoveAll(dir)
  167. defer be.Close()
  168. le := newLessor(be, minLeaseTTL)
  169. l1, err1 := le.Grant(1, 10)
  170. l2, err2 := le.Grant(2, 20)
  171. if err1 != nil || err2 != nil {
  172. t.Fatalf("could not grant initial leases (%v, %v)", err1, err2)
  173. }
  174. // Create a new lessor with the same backend
  175. nle := newLessor(be, minLeaseTTL)
  176. nl1 := nle.get(l1.ID)
  177. if nl1 == nil || nl1.TTL != l1.TTL {
  178. t.Errorf("nl1 = %v, want nl1.TTL= %d", nl1.TTL, l1.TTL)
  179. }
  180. nl2 := nle.get(l2.ID)
  181. if nl2 == nil || nl2.TTL != l2.TTL {
  182. t.Errorf("nl2 = %v, want nl2.TTL= %d", nl2.TTL, l2.TTL)
  183. }
  184. }
  185. type fakeDeleter struct {
  186. deleted []string
  187. }
  188. func (fd *fakeDeleter) TxnBegin() int64 {
  189. return 0
  190. }
  191. func (fd *fakeDeleter) TxnEnd(txnID int64) error {
  192. return nil
  193. }
  194. func (fd *fakeDeleter) TxnDeleteRange(tid int64, key, end []byte) (int64, int64, error) {
  195. fd.deleted = append(fd.deleted, string(key)+"_"+string(end))
  196. return 0, 0, nil
  197. }
  198. func NewTestBackend(t *testing.T) (string, backend.Backend) {
  199. tmpPath, err := ioutil.TempDir("", "lease")
  200. if err != nil {
  201. t.Fatalf("failed to create tmpdir (%v)", err)
  202. }
  203. return tmpPath, backend.New(path.Join(tmpPath, "be"), time.Second, 10000)
  204. }