1// Copyright 2015 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
15package rafthttp
16
17import (
18	"testing"
19
20	"go.etcd.io/etcd/raft/raftpb"
21)
22
23func TestPeerPick(t *testing.T) {
24	tests := []struct {
25		msgappWorking  bool
26		messageWorking bool
27		m              raftpb.Message
28		wpicked        string
29	}{
30		{
31			true, true,
32			raftpb.Message{Type: raftpb.MsgSnap},
33			pipelineMsg,
34		},
35		{
36			true, true,
37			raftpb.Message{Type: raftpb.MsgApp, Term: 1, LogTerm: 1},
38			streamAppV2,
39		},
40		{
41			true, true,
42			raftpb.Message{Type: raftpb.MsgProp},
43			streamMsg,
44		},
45		{
46			true, true,
47			raftpb.Message{Type: raftpb.MsgHeartbeat},
48			streamMsg,
49		},
50		{
51			false, true,
52			raftpb.Message{Type: raftpb.MsgApp, Term: 1, LogTerm: 1},
53			streamMsg,
54		},
55		{
56			false, false,
57			raftpb.Message{Type: raftpb.MsgApp, Term: 1, LogTerm: 1},
58			pipelineMsg,
59		},
60		{
61			false, false,
62			raftpb.Message{Type: raftpb.MsgProp},
63			pipelineMsg,
64		},
65		{
66			false, false,
67			raftpb.Message{Type: raftpb.MsgSnap},
68			pipelineMsg,
69		},
70		{
71			false, false,
72			raftpb.Message{Type: raftpb.MsgHeartbeat},
73			pipelineMsg,
74		},
75	}
76	for i, tt := range tests {
77		peer := &peer{
78			msgAppV2Writer: &streamWriter{working: tt.msgappWorking},
79			writer:         &streamWriter{working: tt.messageWorking},
80			pipeline:       &pipeline{},
81		}
82		_, picked := peer.pick(tt.m)
83		if picked != tt.wpicked {
84			t.Errorf("#%d: picked = %v, want %v", i, picked, tt.wpicked)
85		}
86	}
87}
88