...

Source file src/go.etcd.io/etcd/raft/v3/raftpb/confstate_test.go

Documentation: go.etcd.io/etcd/raft/v3/raftpb

     1  // Copyright 2019 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 raftpb
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestConfState_Equivalent(t *testing.T) {
    22  	type testCase struct {
    23  		cs, cs2 ConfState
    24  		ok      bool
    25  	}
    26  
    27  	testCases := []testCase{
    28  		// Reordered voters and learners.
    29  		{ConfState{
    30  			Voters:         []uint64{1, 2, 3},
    31  			Learners:       []uint64{5, 4, 6},
    32  			VotersOutgoing: []uint64{9, 8, 7},
    33  			LearnersNext:   []uint64{10, 20, 15},
    34  		}, ConfState{
    35  			Voters:         []uint64{1, 2, 3},
    36  			Learners:       []uint64{4, 5, 6},
    37  			VotersOutgoing: []uint64{7, 9, 8},
    38  			LearnersNext:   []uint64{20, 10, 15},
    39  		}, true},
    40  		// Not sensitive to nil vs empty slice.
    41  		{ConfState{Voters: []uint64{}}, ConfState{Voters: []uint64(nil)}, true},
    42  		// Non-equivalent voters.
    43  		{ConfState{Voters: []uint64{1, 2, 3, 4}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
    44  		{ConfState{Voters: []uint64{1, 4, 3}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
    45  		// Non-equivalent learners.
    46  		{ConfState{Voters: []uint64{1, 2, 3, 4}}, ConfState{Voters: []uint64{2, 1, 3}}, false},
    47  		// Sensitive to AutoLeave flag.
    48  		{ConfState{AutoLeave: true}, ConfState{}, false},
    49  	}
    50  
    51  	for _, tc := range testCases {
    52  		t.Run("", func(t *testing.T) {
    53  			if err := tc.cs.Equivalent(tc.cs2); (err == nil) != tc.ok {
    54  				t.Fatalf("wanted error: %t, got:\n%s", tc.ok, err)
    55  			}
    56  		})
    57  	}
    58  }
    59  

View as plain text