lessor_test.go 5.7 KB

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