raft_test.go 5.0 KB

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