raft_test.go 4.6 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. tests := []struct {
  32. confState *raftpb.ConfState
  33. ents []raftpb.Entry
  34. widSet []uint64
  35. }{
  36. {nil, []raftpb.Entry{}, []uint64{}},
  37. {&raftpb.ConfState{Nodes: []uint64{1}},
  38. []raftpb.Entry{}, []uint64{1}},
  39. {&raftpb.ConfState{Nodes: []uint64{1}},
  40. []raftpb.Entry{addEntry}, []uint64{1, 2}},
  41. {&raftpb.ConfState{Nodes: []uint64{1}},
  42. []raftpb.Entry{addEntry, removeEntry}, []uint64{1}},
  43. {&raftpb.ConfState{Nodes: []uint64{1}},
  44. []raftpb.Entry{addEntry, normalEntry}, []uint64{1, 2}},
  45. {&raftpb.ConfState{Nodes: []uint64{1}},
  46. []raftpb.Entry{addEntry, removeEntry, normalEntry}, []uint64{1}},
  47. }
  48. for i, tt := range tests {
  49. var snap raftpb.Snapshot
  50. if tt.confState != nil {
  51. snap.Metadata.ConfState = *tt.confState
  52. }
  53. idSet := getIDs(&snap, tt.ents)
  54. if !reflect.DeepEqual(idSet, tt.widSet) {
  55. t.Errorf("#%d: idset = %#v, want %#v", i, idSet, tt.widSet)
  56. }
  57. }
  58. }
  59. func TestCreateConfigChangeEnts(t *testing.T) {
  60. m := Member{
  61. ID: types.ID(1),
  62. RaftAttributes: RaftAttributes{PeerURLs: []string{"http://localhost:7001", "http://localhost:2380"}},
  63. }
  64. ctx, err := json.Marshal(m)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. addcc1 := &raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1, Context: ctx}
  69. removecc2 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 2}
  70. removecc3 := &raftpb.ConfChange{Type: raftpb.ConfChangeRemoveNode, NodeID: 3}
  71. tests := []struct {
  72. ids []uint64
  73. self uint64
  74. term, index uint64
  75. wents []raftpb.Entry
  76. }{
  77. {
  78. []uint64{1},
  79. 1,
  80. 1, 1,
  81. []raftpb.Entry{},
  82. },
  83. {
  84. []uint64{1, 2},
  85. 1,
  86. 1, 1,
  87. []raftpb.Entry{{Term: 1, Index: 2, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  88. },
  89. {
  90. []uint64{1, 2},
  91. 1,
  92. 2, 2,
  93. []raftpb.Entry{{Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)}},
  94. },
  95. {
  96. []uint64{1, 2, 3},
  97. 1,
  98. 2, 2,
  99. []raftpb.Entry{
  100. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  101. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  102. },
  103. },
  104. {
  105. []uint64{2, 3},
  106. 2,
  107. 2, 2,
  108. []raftpb.Entry{
  109. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  110. },
  111. },
  112. {
  113. []uint64{2, 3},
  114. 1,
  115. 2, 2,
  116. []raftpb.Entry{
  117. {Term: 2, Index: 3, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc2)},
  118. {Term: 2, Index: 4, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(removecc3)},
  119. {Term: 2, Index: 5, Type: raftpb.EntryConfChange, Data: pbutil.MustMarshal(addcc1)},
  120. },
  121. },
  122. }
  123. for i, tt := range tests {
  124. gents := createConfigChangeEnts(tt.ids, tt.self, tt.term, tt.index)
  125. if !reflect.DeepEqual(gents, tt.wents) {
  126. t.Errorf("#%d: ents = %v, want %v", i, gents, tt.wents)
  127. }
  128. }
  129. }
  130. func TestStopRaftWhenWaitingForApplyDone(t *testing.T) {
  131. n := newReadyNode()
  132. r := raftNode{
  133. Node: n,
  134. applyc: make(chan apply),
  135. storage: &storageRecorder{},
  136. raftStorage: raft.NewMemoryStorage(),
  137. transport: &nopTransporter{},
  138. stopped: make(chan struct{}),
  139. done: make(chan struct{}),
  140. }
  141. r.s = &EtcdServer{r: r}
  142. go r.run()
  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. }