lease_queue_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2018 The etcd Authors
  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. "container/heap"
  17. "testing"
  18. "time"
  19. )
  20. func TestLeaseQueue(t *testing.T) {
  21. le := &lessor{
  22. leaseHeap: make(LeaseQueue, 0),
  23. leaseMap: make(map[LeaseID]*Lease),
  24. }
  25. heap.Init(&le.leaseHeap)
  26. // insert in reverse order of expiration time
  27. for i := 50; i >= 1; i-- {
  28. exp := time.Now().Add(time.Hour).UnixNano()
  29. if i == 1 {
  30. exp = time.Now().UnixNano()
  31. }
  32. le.leaseMap[LeaseID(i)] = &Lease{ID: LeaseID(i)}
  33. heap.Push(&le.leaseHeap, &LeaseWithTime{id: LeaseID(i), expiration: exp})
  34. }
  35. // first element must be front
  36. if le.leaseHeap[0].id != LeaseID(1) {
  37. t.Fatalf("first item expected lease ID %d, got %d", LeaseID(1), le.leaseHeap[0].id)
  38. }
  39. l, ok, more := le.expireExists()
  40. if l.ID != 1 {
  41. t.Fatalf("first item expected lease ID %d, got %d", 1, l.ID)
  42. }
  43. if !ok {
  44. t.Fatal("expect expiry lease exists")
  45. }
  46. if more {
  47. t.Fatal("expect no more expiry lease")
  48. }
  49. if le.leaseHeap.Len() != 49 {
  50. t.Fatalf("expected lease heap pop, got %d", le.leaseHeap.Len())
  51. }
  52. }