mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: add a test for node proposal
This commit is contained in:
parent
ec7793557a
commit
68ab7e69e1
@ -107,6 +107,41 @@ func TestNodeStepUnblock(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
r := newRaft(1, []uint64{1}, 10, 1)
|
||||
go n.run(r)
|
||||
n.Campaign(context.TODO())
|
||||
for {
|
||||
rd := <-n.Ready()
|
||||
// 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"))
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user