lessor_test.go 5.6 KB

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