github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/lease_history_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package kvserver
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/roachpb"
    17  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    18  )
    19  
    20  func TestLeaseHistory(t *testing.T) {
    21  	defer leaktest.AfterTest(t)()
    22  	history := newLeaseHistory()
    23  
    24  	for i := 0; i < leaseHistoryMaxEntries; i++ {
    25  		leases := history.get()
    26  		if e, a := i, len(leases); e != a {
    27  			t.Errorf("%d: expected history len to be %d , actual %d:\n%+v", i, e, a, leases)
    28  		}
    29  		if i > 0 {
    30  			if e, a := int64(0), leases[0].Epoch; e != a {
    31  				t.Errorf("%d: expected oldest lease to have epoch of %d , actual %d:\n%+v", i, e, a, leases)
    32  			}
    33  			if e, a := int64(i-1), leases[len(leases)-1].Epoch; e != a {
    34  				t.Errorf("%d: expected newest lease to have epoch of %d , actual %d:\n%+v", i, e, a, leases)
    35  			}
    36  		}
    37  
    38  		history.add(roachpb.Lease{
    39  			Epoch: int64(i),
    40  		})
    41  	}
    42  
    43  	// Now overflow the circular buffer.
    44  	for i := 0; i < leaseHistoryMaxEntries; i++ {
    45  		leases := history.get()
    46  		if e, a := leaseHistoryMaxEntries, len(leases); e != a {
    47  			t.Errorf("%d: expected history len to be %d , actual %d:\n%+v", i, e, a, leases)
    48  		}
    49  		if e, a := int64(i), leases[0].Epoch; e != a {
    50  			t.Errorf("%d: expected oldest lease to have epoch of %d , actual %d:\n%+v", i, e, a, leases)
    51  		}
    52  		if e, a := int64(i+leaseHistoryMaxEntries-1), leases[leaseHistoryMaxEntries-1].Epoch; e != a {
    53  			t.Errorf("%d: expected newest lease to have epoch of %d , actual %d:\n%+v", i, e, a, leases)
    54  		}
    55  
    56  		history.add(roachpb.Lease{
    57  			Epoch: int64(i + leaseHistoryMaxEntries),
    58  		})
    59  	}
    60  }