lessor_test.go 4.2 KB

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