Skip to content

Commit

Permalink
server: refine TestProcessIgnoreMismatchMessage
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Shen <overvenus@gmail.com>
  • Loading branch information
overvenus committed Dec 7, 2023
1 parent fb769c4 commit 9f82390
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions server/etcdserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func TestApplyConfigChangeUpdatesConsistIndex(t *testing.T) {
lgMu: new(sync.RWMutex),
lg: lg,
memberId: 1,
r: *realisticRaftNode(lg, nil),
r: *realisticRaftNode(lg, 1, nil),
cluster: cl,
w: wait.New(),
consistIndex: ci,
Expand Down Expand Up @@ -514,7 +514,7 @@ func TestApplyConfigChangeUpdatesConsistIndex(t *testing.T) {
assert.Equal(t, consistIndex, rindex)
}

func realisticRaftNode(lg *zap.Logger, snap *raftpb.Snapshot) *raftNode {
func realisticRaftNode(lg *zap.Logger, id uint64, snap *raftpb.Snapshot) *raftNode {
storage := raft.NewMemoryStorage()
storage.SetHardState(raftpb.HardState{Commit: 0, Term: 0})
if snap != nil {
Expand All @@ -524,7 +524,7 @@ func realisticRaftNode(lg *zap.Logger, snap *raftpb.Snapshot) *raftNode {
}
}
c := &raft.Config{
ID: 1,
ID: id,
ElectionTick: 10,
HeartbeatTick: 1,
Storage: storage,
Expand Down Expand Up @@ -906,20 +906,25 @@ func TestProcessIgnoreMismatchMessage(t *testing.T) {
defer betesting.Close(t, be)
cl.SetBackend(schema.NewMembershipBackend(lg, be))

// Bootstrap a 3-node cluster
// Bootstrap a 3-node cluster, member IDs: 1 2 3.
cl.AddMember(&membership.Member{ID: types.ID(1)}, true)
cl.AddMember(&membership.Member{ID: types.ID(2)}, true)
cl.AddMember(&membership.Member{ID: types.ID(3)}, true)
r := realisticRaftNode(lg, &raftpb.Snapshot{
// r is initialized with ID 1.
r := realisticRaftNode(lg, 1, &raftpb.Snapshot{
Metadata: raftpb.SnapshotMetadata{
Index: 11, // magic number
Term: 11, // magic number
ConfState: raftpb.ConfState{Voters: []uint64{1, 2, 3}},
Index: 11, // Magic number.
Term: 11, // Magic number.
ConfState: raftpb.ConfState{
// Member ID list.
Voters: []uint64{1, 2, 3},
},
},
})
s := &EtcdServer{
lgMu: new(sync.RWMutex),
lg: lg,
memberId: 1,
r: *r,
v2store: st,
cluster: cl,
Expand All @@ -931,13 +936,13 @@ func TestProcessIgnoreMismatchMessage(t *testing.T) {
// Mock a mad switch dispatching messages to wrong node.
m := raftpb.Message{
Type: raftpb.MsgHeartbeat,
To: 2, // Wrong ID.
To: 2, // Wrong ID, s.MemberId() is 1.
From: 3,
Term: 11,
Commit: 42, // Commit is larger than the last index.
Commit: 42, // Commit is larger than the last index 11.
}
if types.ID(m.To) == s.MemberId() {
t.Fatalf("To must mismatch")
t.Fatalf("m.To (%d) is expected to mismatch s.MemberId (%d)", m.To, s.MemberId())
}
err := s.Process(context.Background(), m)
if err == nil {
Expand Down

0 comments on commit 9f82390

Please sign in to comment.