lessor_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 minLeaseTerm.
  27. func TestLessorGrant(t *testing.T) {
  28. dir, be := NewTestBackend(t)
  29. defer os.RemoveAll(dir)
  30. defer be.Close()
  31. le := newLessor(1, be)
  32. le.Promote()
  33. l := le.Grant(1)
  34. gl := le.get(l.ID)
  35. if !reflect.DeepEqual(gl, l) {
  36. t.Errorf("lease = %v, want %v", gl, l)
  37. }
  38. if l.expiry.Sub(time.Now()) < time.Duration(minLeaseTTL)*time.Second-time.Second {
  39. t.Errorf("term = %v, want at least %v", l.expiry.Sub(time.Now()), time.Duration(minLeaseTTL)*time.Second-time.Second)
  40. }
  41. nl := le.Grant(1)
  42. if nl.ID == l.ID {
  43. t.Errorf("new lease.id = %x, want != %x", nl.ID, l.ID)
  44. }
  45. be.BatchTx().Lock()
  46. _, vs := be.BatchTx().UnsafeRange(leaseBucketName, int64ToBytes(int64(l.ID)), nil, 0)
  47. if len(vs) != 1 {
  48. t.Errorf("len(vs) = %d, want 1", len(vs))
  49. }
  50. be.BatchTx().Unlock()
  51. }
  52. // TestLessorRevoke ensures Lessor can revoke a lease.
  53. // The items in the revoked lease should be removed from
  54. // the DeleteableKV.
  55. // The revoked lease cannot be got from Lessor again.
  56. func TestLessorRevoke(t *testing.T) {
  57. dir, be := NewTestBackend(t)
  58. defer os.RemoveAll(dir)
  59. defer be.Close()
  60. fd := &fakeDeleter{}
  61. le := newLessor(1, be)
  62. le.SetRangeDeleter(fd)
  63. // grant a lease with long term (100 seconds) to
  64. // avoid early termination during the test.
  65. l := le.Grant(100)
  66. items := []LeaseItem{
  67. {"foo"},
  68. {"bar"},
  69. }
  70. err := le.Attach(l.ID, items)
  71. if err != nil {
  72. t.Fatalf("failed to attach items to the lease: %v", err)
  73. }
  74. err = le.Revoke(l.ID)
  75. if err != nil {
  76. t.Fatal("failed to revoke lease:", err)
  77. }
  78. if le.get(l.ID) != nil {
  79. t.Errorf("got revoked lease %x", l.ID)
  80. }
  81. wdeleted := []string{"foo_", "bar_"}
  82. if !reflect.DeepEqual(fd.deleted, wdeleted) {
  83. t.Errorf("deleted= %v, want %v", fd.deleted, wdeleted)
  84. }
  85. be.BatchTx().Lock()
  86. _, vs := be.BatchTx().UnsafeRange(leaseBucketName, int64ToBytes(int64(l.ID)), nil, 0)
  87. if len(vs) != 0 {
  88. t.Errorf("len(vs) = %d, want 0", len(vs))
  89. }
  90. be.BatchTx().Unlock()
  91. }
  92. // TestLessorRenew ensures Lessor can renew an existing lease.
  93. func TestLessorRenew(t *testing.T) {
  94. dir, be := NewTestBackend(t)
  95. defer be.Close()
  96. defer os.RemoveAll(dir)
  97. le := newLessor(1, be)
  98. le.Promote()
  99. l := le.Grant(5)
  100. // manually change the ttl field
  101. l.TTL = 10
  102. err := le.Renew(l.ID)
  103. if err != nil {
  104. t.Fatalf("failed to renew lease (%v)", err)
  105. }
  106. l = le.get(l.ID)
  107. if l.expiry.Sub(time.Now()) < 9*time.Second {
  108. t.Errorf("failed to renew the lease")
  109. }
  110. }
  111. // TestLessorRecover ensures Lessor recovers leases from
  112. // persist backend.
  113. func TestLessorRecover(t *testing.T) {
  114. dir, be := NewTestBackend(t)
  115. defer os.RemoveAll(dir)
  116. defer be.Close()
  117. le := newLessor(1, be)
  118. l1 := le.Grant(10)
  119. l2 := le.Grant(20)
  120. // Create a new lessor with the same backend
  121. nle := newLessor(1, be)
  122. nl1 := nle.get(l1.ID)
  123. if nl1 == nil || nl1.TTL != l1.TTL {
  124. t.Errorf("nl1 = %v, want nl1.TTL= %d", nl1.TTL, l1.TTL)
  125. }
  126. nl2 := nle.get(l2.ID)
  127. if nl2 == nil || nl2.TTL != l2.TTL {
  128. t.Errorf("nl2 = %v, want nl2.TTL= %d", nl2.TTL, l2.TTL)
  129. }
  130. }
  131. type fakeDeleter struct {
  132. deleted []string
  133. }
  134. func (fd *fakeDeleter) DeleteRange(key, end []byte) (int64, int64) {
  135. fd.deleted = append(fd.deleted, string(key)+"_"+string(end))
  136. return 0, 0
  137. }
  138. func NewTestBackend(t *testing.T) (string, backend.Backend) {
  139. tmpPath, err := ioutil.TempDir("", "lease")
  140. if err != nil {
  141. t.Fatalf("failed to create tmpdir (%v)", err)
  142. }
  143. return tmpPath, backend.New(path.Join(tmpPath, "be"), time.Second, 10000)
  144. }