lessor_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "reflect"
  17. "testing"
  18. "time"
  19. )
  20. // TestLessorGrant ensures Lessor can grant wanted lease.
  21. // The granted lease should have a unique ID with a term
  22. // that is greater than minLeaseTerm.
  23. func TestLessorGrant(t *testing.T) {
  24. le := NewLessor(1, &fakeDeleteable{})
  25. l := le.Grant(time.Now().Add(time.Second))
  26. gl := le.get(l.id)
  27. if !reflect.DeepEqual(gl, l) {
  28. t.Errorf("lease = %v, want %v", gl, l)
  29. }
  30. if l.expiry.Sub(time.Now()) < minLeaseTerm-time.Second {
  31. t.Errorf("term = %v, want at least %v", l.expiry.Sub(time.Now()), minLeaseTerm-time.Second)
  32. }
  33. nl := le.Grant(time.Now().Add(time.Second))
  34. if nl.id == l.id {
  35. t.Errorf("new lease.id = %x, want != %x", nl.id, l.id)
  36. }
  37. }
  38. // TestLessorRevoke ensures Lessor can revoke a lease.
  39. // The items in the revoked lease should be removed from
  40. // the DeleteableKV.
  41. // The revoked lease cannot be got from Lessor again.
  42. func TestLessorRevoke(t *testing.T) {
  43. fd := &fakeDeleteable{}
  44. le := NewLessor(1, fd)
  45. // grant a lease with long term (100 seconds) to
  46. // avoid early termination during the test.
  47. l := le.Grant(time.Now().Add(100 * time.Second))
  48. items := []leaseItem{
  49. {"foo", ""},
  50. {"bar", "zar"},
  51. }
  52. err := le.Attach(l.id, items)
  53. if err != nil {
  54. t.Fatalf("failed to attach items to the lease: %v", err)
  55. }
  56. err = le.Revoke(l.id)
  57. if err != nil {
  58. t.Fatal("failed to revoke lease:", err)
  59. }
  60. if le.get(l.id) != nil {
  61. t.Errorf("got revoked lease %x", l.id)
  62. }
  63. wdeleted := []string{"foo_", "bar_zar"}
  64. if !reflect.DeepEqual(fd.deleted, wdeleted) {
  65. t.Errorf("deleted= %v, want %v", fd.deleted, wdeleted)
  66. }
  67. }
  68. // TestLessorRenew ensures Lessor can renew an existing lease.
  69. func TestLessorRenew(t *testing.T) {
  70. le := NewLessor(1, &fakeDeleteable{})
  71. l := le.Grant(time.Now().Add(5 * time.Second))
  72. le.Renew(l.id, time.Now().Add(100*time.Second))
  73. l = le.get(l.id)
  74. if l.expiry.Sub(time.Now()) < 95*time.Second {
  75. t.Errorf("failed to renew the lease for 100 seconds")
  76. }
  77. }
  78. type fakeDeleteable struct {
  79. deleted []string
  80. }
  81. func (fd *fakeDeleteable) DeleteRange(key, end []byte) (int64, int64) {
  82. fd.deleted = append(fd.deleted, string(key)+"_"+string(end))
  83. return 0, 0
  84. }