1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package raft
16
17 import (
18 "math"
19 "reflect"
20 "strings"
21 "testing"
22
23 pb "go.etcd.io/etcd/raft/v3/raftpb"
24 )
25
26 var testFormatter EntryFormatter = func(data []byte) string {
27 return strings.ToUpper(string(data))
28 }
29
30 func TestDescribeEntry(t *testing.T) {
31 entry := pb.Entry{
32 Term: 1,
33 Index: 2,
34 Type: pb.EntryNormal,
35 Data: []byte("hello\x00world"),
36 }
37
38 defaultFormatted := DescribeEntry(entry, nil)
39 if defaultFormatted != "1/2 EntryNormal \"hello\\x00world\"" {
40 t.Errorf("unexpected default output: %s", defaultFormatted)
41 }
42
43 customFormatted := DescribeEntry(entry, testFormatter)
44 if customFormatted != "1/2 EntryNormal HELLO\x00WORLD" {
45 t.Errorf("unexpected custom output: %s", customFormatted)
46 }
47 }
48
49 func TestLimitSize(t *testing.T) {
50 ents := []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 6}}
51 tests := []struct {
52 maxsize uint64
53 wentries []pb.Entry
54 }{
55 {math.MaxUint64, []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 6}}},
56
57 {0, []pb.Entry{{Index: 4, Term: 4}}},
58
59 {uint64(ents[0].Size() + ents[1].Size()), []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
60
61 {uint64(ents[0].Size() + ents[1].Size() + ents[2].Size()/2), []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
62 {uint64(ents[0].Size() + ents[1].Size() + ents[2].Size() - 1), []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}}},
63
64 {uint64(ents[0].Size() + ents[1].Size() + ents[2].Size()), []pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 5}, {Index: 6, Term: 6}}},
65 }
66
67 for i, tt := range tests {
68 if !reflect.DeepEqual(limitSize(ents, tt.maxsize), tt.wentries) {
69 t.Errorf("#%d: entries = %v, want %v", i, limitSize(ents, tt.maxsize), tt.wentries)
70 }
71 }
72 }
73
74 func TestIsLocalMsg(t *testing.T) {
75 tests := []struct {
76 msgt pb.MessageType
77 isLocal bool
78 }{
79 {pb.MsgHup, true},
80 {pb.MsgBeat, true},
81 {pb.MsgUnreachable, true},
82 {pb.MsgSnapStatus, true},
83 {pb.MsgCheckQuorum, true},
84 {pb.MsgTransferLeader, false},
85 {pb.MsgProp, false},
86 {pb.MsgApp, false},
87 {pb.MsgAppResp, false},
88 {pb.MsgVote, false},
89 {pb.MsgVoteResp, false},
90 {pb.MsgSnap, false},
91 {pb.MsgHeartbeat, false},
92 {pb.MsgHeartbeatResp, false},
93 {pb.MsgTimeoutNow, false},
94 {pb.MsgReadIndex, false},
95 {pb.MsgReadIndexResp, false},
96 {pb.MsgPreVote, false},
97 {pb.MsgPreVoteResp, false},
98 }
99
100 for i, tt := range tests {
101 got := IsLocalMsg(tt.msgt)
102 if got != tt.isLocal {
103 t.Errorf("#%d: got %v, want %v", i, got, tt.isLocal)
104 }
105 }
106 }
107
View as plain text