lessor_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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(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(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. l := le.Grant(5)
  97. // manually change the ttl field
  98. l.ttl = 10
  99. le.Renew(l.id)
  100. l = le.get(l.id)
  101. if l.expiry.Sub(time.Now()) < 9*time.Second {
  102. t.Errorf("failed to renew the lease")
  103. }
  104. }
  105. type fakeDeleteable struct {
  106. deleted []string
  107. }
  108. func (fd *fakeDeleteable) DeleteRange(key, end []byte) (int64, int64) {
  109. fd.deleted = append(fd.deleted, string(key)+"_"+string(end))
  110. return 0, 0
  111. }
  112. func NewTestBackend(t *testing.T) (string, backend.Backend) {
  113. tmpPath, err := ioutil.TempDir("", "lease")
  114. if err != nil {
  115. t.Fatalf("failed to create tmpdir (%v)", err)
  116. }
  117. return tmpPath, backend.New(path.Join(tmpPath, "be"), time.Second, 10000)
  118. }