github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/node_liveness_unit_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/kv/kvserver/kvserverpb"
    17  	"github.com/cockroachdb/cockroach/pkg/util/hlc"
    18  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    19  )
    20  
    21  func TestShouldReplaceLiveness(t *testing.T) {
    22  	defer leaktest.AfterTest(t)()
    23  
    24  	l := func(epo int64, expiration hlc.Timestamp, draining, decom bool) kvserverpb.Liveness {
    25  		return kvserverpb.Liveness{
    26  			Epoch:           epo,
    27  			Expiration:      hlc.LegacyTimestamp(expiration),
    28  			Draining:        draining,
    29  			Decommissioning: decom,
    30  		}
    31  	}
    32  	const (
    33  		no  = false
    34  		yes = true
    35  	)
    36  	now := hlc.Timestamp{WallTime: 12345}
    37  
    38  	for _, test := range []struct {
    39  		old, new kvserverpb.Liveness
    40  		exp      bool
    41  	}{
    42  		{
    43  			// Epoch update only.
    44  			kvserverpb.Liveness{},
    45  			l(1, hlc.Timestamp{}, false, false),
    46  			yes,
    47  		},
    48  		{
    49  			// No Epoch update, but Expiration update.
    50  			l(1, now, false, false),
    51  			l(1, now.Add(0, 1), false, false),
    52  			yes,
    53  		},
    54  		{
    55  			// No update.
    56  			l(1, now, false, false),
    57  			l(1, now, false, false),
    58  			no,
    59  		},
    60  		{
    61  			// Only Decommissioning changes.
    62  			l(1, now, false, false),
    63  			l(1, now, false, true),
    64  			yes,
    65  		},
    66  		{
    67  			// Only Draining changes.
    68  			l(1, now, false, false),
    69  			l(1, now, true, false),
    70  			yes,
    71  		},
    72  		{
    73  			// Decommissioning changes, but Epoch moves backwards.
    74  			l(10, now, true, true),
    75  			l(9, now, true, false),
    76  			no,
    77  		},
    78  		{
    79  			// Draining changes, but Expiration moves backwards..
    80  			l(10, now, false, false),
    81  			l(10, now.Add(-1, 0), true, false),
    82  			no,
    83  		},
    84  	} {
    85  		t.Run("", func(t *testing.T) {
    86  			if act := shouldReplaceLiveness(test.old, test.new); act != test.exp {
    87  				t.Errorf("unexpected update: %+v", test)
    88  			}
    89  		})
    90  	}
    91  }