...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package lease
16
17 import (
18 "testing"
19 "time"
20 )
21
22 func TestLeaseQueue(t *testing.T) {
23 expiredRetryInterval := 100 * time.Millisecond
24 le := &lessor{
25 leaseExpiredNotifier: newLeaseExpiredNotifier(),
26 leaseMap: make(map[LeaseID]*Lease),
27 expiredLeaseRetryInterval: expiredRetryInterval,
28 }
29 le.leaseExpiredNotifier.Init()
30
31
32 for i := 50; i >= 1; i-- {
33 now := time.Now()
34 exp := now.Add(time.Hour)
35 if i == 1 {
36 exp = now
37 }
38 le.leaseMap[LeaseID(i)] = &Lease{ID: LeaseID(i)}
39 le.leaseExpiredNotifier.RegisterOrUpdate(&LeaseWithTime{id: LeaseID(i), time: exp})
40 }
41
42
43 if le.leaseExpiredNotifier.Poll().id != LeaseID(1) {
44 t.Fatalf("first item expected lease ID %d, got %d", LeaseID(1), le.leaseExpiredNotifier.Poll().id)
45 }
46
47 existExpiredEvent := func() {
48 l, ok, more := le.expireExists()
49 if l.ID != 1 {
50 t.Fatalf("first item expected lease ID %d, got %d", 1, l.ID)
51 }
52 if !ok {
53 t.Fatal("expect expiry lease exists")
54 }
55 if more {
56 t.Fatal("expect no more expiry lease")
57 }
58
59 if le.leaseExpiredNotifier.Len() != 50 {
60 t.Fatalf("expected the expired lease to be pushed back to the heap, heap size got %d", le.leaseExpiredNotifier.Len())
61 }
62
63 if le.leaseExpiredNotifier.Poll().id != LeaseID(1) {
64 t.Fatalf("first item expected lease ID %d, got %d", LeaseID(1), le.leaseExpiredNotifier.Poll().id)
65 }
66 }
67
68 noExpiredEvent := func() {
69
70 _, ok, more := le.expireExists()
71 if ok {
72 t.Fatal("expect no expiry lease exists")
73 }
74 if more {
75 t.Fatal("expect no more expiry lease")
76 }
77 }
78
79 existExpiredEvent()
80 noExpiredEvent()
81 time.Sleep(expiredRetryInterval)
82 existExpiredEvent()
83 }
84
View as plain text