raft_test.go 4.8 KB

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