raft_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2015 CoreOS, Inc.
  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. package etcdserver
  15. import (
  16. "encoding/json"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/pkg/pbutil"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/raft"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. )
  25. func TestGetIDs(t *testing.T) {
  26. addcc := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
  27. addEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc)}
  28. removecc := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  29. removeEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc)}
  30. normalEntry := raftpb.Entry{Type: raftpb.EntryNormal}
  31. updatecc := &raftpb.ConfChange{Type: raftpb.ConfChangeUpdateNode, NodeID: 2}
  32. updateEntry := raftpb.Entry{Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(updatecc)}
  33. tests := []struct {
  34. confState *raftpb.ConfState
  35. ents []raftpb.Entry
  36. widSet []uint64
  37. }{
  38. {nil, []raftpb.Entry{}, []uint64{}},
  39. {&raftpb.ConfState{Nodes: []uint64{1}},
  40. []raftpb.Entry{}, []uint64{1}},
  41. {&raftpb.ConfState{Nodes: []uint64{1}},
  42. []raftpb.Entry{addEntry}, []uint64{1, 2}},
  43. {&raftpb.ConfState{Nodes: []uint64{1}},
  44. []raftpb.Entry{addEntry, removeEntry}, []uint64{1}},
  45. {&raftpb.ConfState{Nodes: []uint64{1}},
  46. []raftpb.Entry{addEntry, normalEntry}, []uint64{1, 2}},
  47. {&raftpb.ConfState{Nodes: []uint64{1}},
  48. []raftpb.Entry{addEntry, normalEntry, updateEntry}, []uint64{1, 2}},
  49. {&raftpb.ConfState{Nodes: []uint64{1}},
  50. []raftpb.Entry{addEntry, removeEntry, normalEntry}, []uint64{1}},
  51. }
  52. for i, tt := range tests {
  53. var snap raftpb.Snapshot
  54. if tt.confState != nil {
  55. snap.Metadata.ConfState = *tt.confState
  56. }
  57. idSet := getIDs(&snap, tt.ents)
  58. if !reflect.DeepEqual(idSet, tt.widSet) {
  59. t.Errorf("#%d: idset = %#v, want %#v", i, idSet, tt.widSet)
  60. }
  61. }
  62. }
  63. func TestCreateConfigChangeEnts(t *testing.T) {
  64. m := Member{
  65. ID: types.ID(1),
  66. RaftAttributes: RaftAttributes{PeerURLs: []string{"http://localhost:7001", "http://localhost:2380"}},
  67. }
  68. ctx, err := json.Marshal(m)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. addcc1 := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1, Context: ctx}
  73. removecc2 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  74. removecc3 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 3}
  75. tests := []struct {
  76. ids []uint64
  77. self uint64
  78. term, index uint64
  79. wents []raftpb.Entry
  80. }{
  81. {
  82. []uint64{1},
  83. 1,
  84. 1, 1,
  85. []raftpb.Entry{},
  86. },
  87. {
  88. []uint64{1, 2},
  89. 1,
  90. 1, 1,
  91. []raftpb.Entry{{Term: 1, Index: 2, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  92. },
  93. {
  94. []uint64{1, 2},
  95. 1,
  96. 2, 2,
  97. []raftpb.Entry{{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  98. },
  99. {
  100. []uint64{1, 2, 3},
  101. 1,
  102. 2, 2,
  103. []raftpb.Entry{
  104. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  105. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  106. },
  107. },
  108. {
  109. []uint64{2, 3},
  110. 2,
  111. 2, 2,
  112. []raftpb.Entry{
  113. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  114. },
  115. },
  116. {
  117. []uint64{2, 3},
  118. 1,
  119. 2, 2,
  120. []raftpb.Entry{
  121. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  122. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  123. {Term: 2, Index: 5, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc1)},
  124. },
  125. },
  126. }
  127. for i, tt := range tests {
  128. gents := createConfigChangeEnts(tt.ids, tt.self, tt.term, tt.index)
  129. if !reflect.DeepEqual(gents, tt.wents) {
  130. t.Errorf("#%d: ents = %v, want %v", i, gents, tt.wents)
  131. }
  132. }
  133. }
  134. func TestStopRaftWhenWaitingForApplyDone(t *testing.T) {
  135. n := newReadyNode()
  136. r := raftNode{
  137. Node: n,
  138. storage: &storageRecorder{},
  139. raftStorage: raft.NewMemoryStorage(),
  140. transport: &nopTransporter{},
  141. }
  142. r.start(&EtcdServer{r: r})
  143. n.readyc <- raft.Ready{}
  144. select {
  145. case <-r.applyc:
  146. case <-time.After(time.Second):
  147. t.Fatalf("failed to receive apply struct")
  148. }
  149. r.stopped <- struct{}{}
  150. select {
  151. case <-r.done:
  152. case <-time.After(time.Second):
  153. t.Fatalf("failed to stop raft loop")
  154. }
  155. }