add test case

Signed-off-by: Jay Lee <BusyJayLee@gmail.com>
This commit is contained in:
Jay Lee 2021-01-21 16:02:46 +08:00
parent 5dcd459ae9
commit f85b08c735
No known key found for this signature in database
GPG Key ID: CF3437B6A06C22F9

View File

@ -2806,6 +2806,46 @@ func TestRestoreWithLearner(t *testing.T) {
}
}
/// Tests if incoming voter and outgoing voter can receive and apply snapshot
/// correctly.
func TestRestoreWithVotersOutgoing(t *testing.T) {
s := pb.Snapshot{
Metadata: pb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: pb.ConfState{Voters: []uint64{2, 3, 4}, VotersOutgoing: []uint64{1, 2, 3}},
},
}
storage := newTestMemoryStorage(withPeers(1, 2))
sm := newTestRaft(1, 10, 1, storage)
if ok := sm.restore(s); !ok {
t.Fatal("restore fail, want succeed")
}
if sm.raftLog.lastIndex() != s.Metadata.Index {
t.Errorf("log.lastIndex = %d, want %d", sm.raftLog.lastIndex(), s.Metadata.Index)
}
if mustTerm(sm.raftLog.term(s.Metadata.Index)) != s.Metadata.Term {
t.Errorf("log.lastTerm = %d, want %d", mustTerm(sm.raftLog.term(s.Metadata.Index)), s.Metadata.Term)
}
sg := sm.prs.VoterNodes()
if !reflect.DeepEqual(sg, []uint64{1, 2, 3, 4}) {
t.Errorf("sm.Voters = %+v, want %+v", sg, s.Metadata.ConfState.Voters)
}
if ok := sm.restore(s); ok {
t.Fatal("restore succeed, want fail")
}
// It should not campaign before actually applying data.
for i := 0; i < sm.randomizedElectionTimeout; i++ {
sm.tick()
}
if sm.state != StateFollower {
t.Errorf("state = %d, want %d", sm.state, StateFollower)
}
}
// TestRestoreVoterToLearner verifies that a normal peer can be downgraded to a
// learner through a snapshot. At the time of writing, we don't allow
// configuration changes to do this directly, but note that the snapshot may