...

Source file src/go.etcd.io/etcd/server/v3/lease/lease_queue_test.go

Documentation: go.etcd.io/etcd/server/v3/lease

     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  
    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  	// insert in reverse order of expiration time
    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  	// first element is expired.
    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  		// re-acquire the expired item, nothing exists
    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() // first acquire
    80  	noExpiredEvent()    // second acquire
    81  	time.Sleep(expiredRetryInterval)
    82  	existExpiredEvent() // acquire after retry interval
    83  }
    84  

View as plain text