123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- // Copyright 2015 CoreOS, Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package raft
- import (
- "reflect"
- "testing"
- "time"
- "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
- "github.com/coreos/etcd/pkg/testutil"
- "github.com/coreos/etcd/raft/raftpb"
- )
- // TestNodeStep ensures that node.Step sends msgProp to propc chan
- // and other kinds of messages to recvc chan.
- func TestNodeStep(t *testing.T) {
- for i, msgn := range raftpb.MessageType_name {
- n := &node{
- propc: make(chan raftpb.Message, 1),
- recvc: make(chan raftpb.Message, 1),
- }
- msgt := raftpb.MessageType(i)
- n.Step(context.TODO(), raftpb.Message{Type: msgt})
- // Proposal goes to proc chan. Others go to recvc chan.
- if msgt == raftpb.MsgProp {
- select {
- case <-n.propc:
- default:
- t.Errorf("%d: cannot receive %s on propc chan", msgt, msgn)
- }
- } else {
- if msgt == raftpb.MsgBeat || msgt == raftpb.MsgHup || msgt == raftpb.MsgUnreachable || msgt == raftpb.MsgSnapStatus || msgt == raftpb.MsgCheckQuorum {
- select {
- case <-n.recvc:
- t.Errorf("%d: step should ignore %s", msgt, msgn)
- default:
- }
- } else {
- select {
- case <-n.recvc:
- default:
- t.Errorf("%d: cannot receive %s on recvc chan", msgt, msgn)
- }
- }
- }
- }
- }
- // Cancel and Stop should unblock Step()
- func TestNodeStepUnblock(t *testing.T) {
- // a node without buffer to block step
- n := &node{
- propc: make(chan raftpb.Message),
- done: make(chan struct{}),
- }
- ctx, cancel := context.WithCancel(context.Background())
- stopFunc := func() { close(n.done) }
- tests := []struct {
- unblock func()
- werr error
- }{
- {stopFunc, ErrStopped},
- {cancel, context.Canceled},
- }
- for i, tt := range tests {
- errc := make(chan error, 1)
- go func() {
- err := n.Step(ctx, raftpb.Message{Type: raftpb.MsgProp})
- errc <- err
- }()
- tt.unblock()
- select {
- case err := <-errc:
- if err != tt.werr {
- t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
- }
- //clean up side-effect
- if ctx.Err() != nil {
- ctx = context.TODO()
- }
- select {
- case <-n.done:
- n.done = make(chan struct{})
- default:
- }
- case <-time.After(time.Millisecond * 100):
- t.Errorf("#%d: failed to unblock step", i)
- }
- }
- }
- // TestNodePropose ensures that node.Propose sends the given proposal to the underlying raft.
- func TestNodePropose(t *testing.T) {
- msgs := []raftpb.Message{}
- appendStep := func(r *raft, m raftpb.Message) {
- msgs = append(msgs, m)
- }
- n := newNode()
- s := NewMemoryStorage()
- r := newTestRaft(1, []uint64{1}, 10, 1, s)
- go n.run(r)
- n.Campaign(context.TODO())
- for {
- rd := <-n.Ready()
- s.Append(rd.Entries)
- // change the step function to appendStep until this raft becomes leader
- if rd.SoftState.Lead == r.id {
- r.step = appendStep
- n.Advance()
- break
- }
- n.Advance()
- }
- n.Propose(context.TODO(), []byte("somedata"))
- n.Stop()
- if len(msgs) != 1 {
- t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
- }
- if msgs[0].Type != raftpb.MsgProp {
- t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgProp)
- }
- if !reflect.DeepEqual(msgs[0].Entries[0].Data, []byte("somedata")) {
- t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, []byte("somedata"))
- }
- }
- // TestNodeProposeConfig ensures that node.ProposeConfChange sends the given configuration proposal
- // to the underlying raft.
- func TestNodeProposeConfig(t *testing.T) {
- msgs := []raftpb.Message{}
- appendStep := func(r *raft, m raftpb.Message) {
- msgs = append(msgs, m)
- }
- n := newNode()
- s := NewMemoryStorage()
- r := newTestRaft(1, []uint64{1}, 10, 1, s)
- go n.run(r)
- n.Campaign(context.TODO())
- for {
- rd := <-n.Ready()
- s.Append(rd.Entries)
- // change the step function to appendStep until this raft becomes leader
- if rd.SoftState.Lead == r.id {
- r.step = appendStep
- n.Advance()
- break
- }
- n.Advance()
- }
- cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
- ccdata, err := cc.Marshal()
- if err != nil {
- t.Fatal(err)
- }
- n.ProposeConfChange(context.TODO(), cc)
- n.Stop()
- if len(msgs) != 1 {
- t.Fatalf("len(msgs) = %d, want %d", len(msgs), 1)
- }
- if msgs[0].Type != raftpb.MsgProp {
- t.Errorf("msg type = %d, want %d", msgs[0].Type, raftpb.MsgProp)
- }
- if !reflect.DeepEqual(msgs[0].Entries[0].Data, ccdata) {
- t.Errorf("data = %v, want %v", msgs[0].Entries[0].Data, ccdata)
- }
- }
- // TestBlockProposal ensures that node will block proposal when it does not
- // know who is the current leader; node will accept proposal when it knows
- // who is the current leader.
- func TestBlockProposal(t *testing.T) {
- n := newNode()
- r := newTestRaft(1, []uint64{1}, 10, 1, NewMemoryStorage())
- go n.run(r)
- defer n.Stop()
- errc := make(chan error, 1)
- go func() {
- errc <- n.Propose(context.TODO(), []byte("somedata"))
- }()
- testutil.WaitSchedule()
- select {
- case err := <-errc:
- t.Errorf("err = %v, want blocking", err)
- default:
- }
- n.Campaign(context.TODO())
- select {
- case err := <-errc:
- if err != nil {
- t.Errorf("err = %v, want %v", err, nil)
- }
- case <-time.After(10 * time.Second):
- t.Errorf("blocking proposal, want unblocking")
- }
- }
- // TestNodeTick ensures that node.Tick() will increase the
- // elapsed of the underlying raft state machine.
- func TestNodeTick(t *testing.T) {
- n := newNode()
- s := NewMemoryStorage()
- r := newTestRaft(1, []uint64{1}, 10, 1, s)
- go n.run(r)
- elapsed := r.electionElapsed
- n.Tick()
- n.Stop()
- if r.electionElapsed != elapsed+1 {
- t.Errorf("elapsed = %d, want %d", r.electionElapsed, elapsed+1)
- }
- }
- // TestNodeStop ensures that node.Stop() blocks until the node has stopped
- // processing, and that it is idempotent
- func TestNodeStop(t *testing.T) {
- n := newNode()
- s := NewMemoryStorage()
- r := newTestRaft(1, []uint64{1}, 10, 1, s)
- donec := make(chan struct{})
- go func() {
- n.run(r)
- close(donec)
- }()
- elapsed := r.electionElapsed
- n.Tick()
- n.Stop()
- select {
- case <-donec:
- case <-time.After(time.Second):
- t.Fatalf("timed out waiting for node to stop!")
- }
- if r.electionElapsed != elapsed+1 {
- t.Errorf("elapsed = %d, want %d", r.electionElapsed, elapsed+1)
- }
- // Further ticks should have no effect, the node is stopped.
- n.Tick()
- if r.electionElapsed != elapsed+1 {
- t.Errorf("elapsed = %d, want %d", r.electionElapsed, elapsed+1)
- }
- // Subsequent Stops should have no effect.
- n.Stop()
- }
- func TestReadyContainUpdates(t *testing.T) {
- tests := []struct {
- rd Ready
- wcontain bool
- }{
- {Ready{}, false},
- {Ready{SoftState: &SoftState{Lead: 1}}, true},
- {Ready{HardState: raftpb.HardState{Vote: 1}}, true},
- {Ready{Entries: make([]raftpb.Entry, 1, 1)}, true},
- {Ready{CommittedEntries: make([]raftpb.Entry, 1, 1)}, true},
- {Ready{Messages: make([]raftpb.Message, 1, 1)}, true},
- {Ready{Snapshot: raftpb.Snapshot{Metadata: raftpb.SnapshotMetadata{Index: 1}}}, true},
- }
- for i, tt := range tests {
- if g := tt.rd.containsUpdates(); g != tt.wcontain {
- t.Errorf("#%d: containUpdates = %v, want %v", i, g, tt.wcontain)
- }
- }
- }
- // TestNodeStart ensures that a node can be started correctly. The node should
- // start with correct configuration change entries, and can accept and commit
- // proposals.
- func TestNodeStart(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- cc := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
- ccdata, err := cc.Marshal()
- if err != nil {
- t.Fatalf("unexpected marshal error: %v", err)
- }
- wants := []Ready{
- {
- SoftState: &SoftState{Lead: 1, RaftState: StateLeader},
- HardState: raftpb.HardState{Term: 2, Commit: 2, Vote: 1},
- Entries: []raftpb.Entry{
- {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
- {Term: 2, Index: 2},
- },
- CommittedEntries: []raftpb.Entry{
- {Type: raftpb.EntryConfChange, Term: 1, Index: 1, Data: ccdata},
- {Term: 2, Index: 2},
- },
- },
- {
- HardState: raftpb.HardState{Term: 2, Commit: 3, Vote: 1},
- Entries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
- CommittedEntries: []raftpb.Entry{{Term: 2, Index: 3, Data: []byte("foo")}},
- },
- }
- storage := NewMemoryStorage()
- c := &Config{
- ID: 1,
- ElectionTick: 10,
- HeartbeatTick: 1,
- Storage: storage,
- MaxSizePerMsg: noLimit,
- MaxInflightMsgs: 256,
- }
- n := StartNode(c, []Peer{{ID: 1}})
- defer n.Stop()
- n.Campaign(ctx)
- g := <-n.Ready()
- if !reflect.DeepEqual(g, wants[0]) {
- t.Fatalf("#%d: g = %+v,\n w %+v", 1, g, wants[0])
- } else {
- storage.Append(g.Entries)
- n.Advance()
- }
- n.Propose(ctx, []byte("foo"))
- if g2 := <-n.Ready(); !reflect.DeepEqual(g2, wants[1]) {
- t.Errorf("#%d: g = %+v,\n w %+v", 2, g2, wants[1])
- } else {
- storage.Append(g2.Entries)
- n.Advance()
- }
- select {
- case rd := <-n.Ready():
- t.Errorf("unexpected Ready: %+v", rd)
- case <-time.After(time.Millisecond):
- }
- }
- func TestNodeRestart(t *testing.T) {
- entries := []raftpb.Entry{
- {Term: 1, Index: 1},
- {Term: 1, Index: 2, Data: []byte("foo")},
- }
- st := raftpb.HardState{Term: 1, Commit: 1}
- want := Ready{
- HardState: st,
- // commit up to index commit index in st
- CommittedEntries: entries[:st.Commit],
- }
- storage := NewMemoryStorage()
- storage.SetHardState(st)
- storage.Append(entries)
- c := &Config{
- ID: 1,
- ElectionTick: 10,
- HeartbeatTick: 1,
- Storage: storage,
- MaxSizePerMsg: noLimit,
- MaxInflightMsgs: 256,
- }
- n := RestartNode(c)
- defer n.Stop()
- if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
- t.Errorf("g = %+v,\n w %+v", g, want)
- }
- n.Advance()
- select {
- case rd := <-n.Ready():
- t.Errorf("unexpected Ready: %+v", rd)
- case <-time.After(time.Millisecond):
- }
- }
- func TestNodeRestartFromSnapshot(t *testing.T) {
- snap := raftpb.Snapshot{
- Metadata: raftpb.SnapshotMetadata{
- ConfState: raftpb.ConfState{Nodes: []uint64{1, 2}},
- Index: 2,
- Term: 1,
- },
- }
- entries := []raftpb.Entry{
- {Term: 1, Index: 3, Data: []byte("foo")},
- }
- st := raftpb.HardState{Term: 1, Commit: 3}
- want := Ready{
- HardState: st,
- // commit up to index commit index in st
- CommittedEntries: entries,
- }
- s := NewMemoryStorage()
- s.SetHardState(st)
- s.ApplySnapshot(snap)
- s.Append(entries)
- c := &Config{
- ID: 1,
- ElectionTick: 10,
- HeartbeatTick: 1,
- Storage: s,
- MaxSizePerMsg: noLimit,
- MaxInflightMsgs: 256,
- }
- n := RestartNode(c)
- defer n.Stop()
- if g := <-n.Ready(); !reflect.DeepEqual(g, want) {
- t.Errorf("g = %+v,\n w %+v", g, want)
- } else {
- n.Advance()
- }
- select {
- case rd := <-n.Ready():
- t.Errorf("unexpected Ready: %+v", rd)
- case <-time.After(time.Millisecond):
- }
- }
- func TestNodeAdvance(t *testing.T) {
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- storage := NewMemoryStorage()
- c := &Config{
- ID: 1,
- ElectionTick: 10,
- HeartbeatTick: 1,
- Storage: storage,
- MaxSizePerMsg: noLimit,
- MaxInflightMsgs: 256,
- }
- n := StartNode(c, []Peer{{ID: 1}})
- defer n.Stop()
- n.Campaign(ctx)
- <-n.Ready()
- n.Propose(ctx, []byte("foo"))
- var rd Ready
- select {
- case rd = <-n.Ready():
- t.Fatalf("unexpected Ready before Advance: %+v", rd)
- case <-time.After(time.Millisecond):
- }
- storage.Append(rd.Entries)
- n.Advance()
- select {
- case <-n.Ready():
- case <-time.After(100 * time.Millisecond):
- t.Errorf("expect Ready after Advance, but there is no Ready available")
- }
- }
- func TestSoftStateEqual(t *testing.T) {
- tests := []struct {
- st *SoftState
- we bool
- }{
- {&SoftState{}, true},
- {&SoftState{Lead: 1}, false},
- {&SoftState{RaftState: StateLeader}, false},
- }
- for i, tt := range tests {
- if g := tt.st.equal(&SoftState{}); g != tt.we {
- t.Errorf("#%d, equal = %v, want %v", i, g, tt.we)
- }
- }
- }
- func TestIsHardStateEqual(t *testing.T) {
- tests := []struct {
- st raftpb.HardState
- we bool
- }{
- {emptyState, true},
- {raftpb.HardState{Vote: 1}, false},
- {raftpb.HardState{Commit: 1}, false},
- {raftpb.HardState{Term: 1}, false},
- }
- for i, tt := range tests {
- if isHardStateEqual(tt.st, emptyState) != tt.we {
- t.Errorf("#%d, equal = %v, want %v", i, isHardStateEqual(tt.st, emptyState), tt.we)
- }
- }
- }
|