From b261a5edc1c87964bf99cda8f3e911bef4b6ae68 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 11 Sep 2014 20:32:55 -0700 Subject: [PATCH] raft: test node block proposal --- raft/node_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/raft/node_test.go b/raft/node_test.go index 8527214bd..5e11668da 100644 --- a/raft/node_test.go +++ b/raft/node_test.go @@ -81,6 +81,40 @@ func TestNodeStepUnblock(t *testing.T) { } } +// TestBlockProposal ensures that node will block proposal when it does not +// know who is the current leader; node will direct proposal when it knows +// who is the current leader. +func TestBlockProposal(t *testing.T) { + propsal := false + + n := newNode() + defer n.Stop() + r := newRaft(1, []int64{1}, 10, 1) + r.step = func(r *raft, m raftpb.Message) { + if m.Type == msgProp { + propsal = true + } + } + + go n.run(r) + go n.Propose(context.TODO(), []byte("somedata")) + // give some time for go routines sechduling ... + time.Sleep(time.Millisecond * 2) + if propsal { + t.Fatalf("proposal = %v, want %v", propsal, false) + } + + // assign a lead to raft. + // tick to update the node. + r.lead = 1 + n.Tick() + // give some time for go routines sechduling ... + time.Sleep(time.Millisecond * 2) + if !propsal { + t.Fatalf("proposal = %v, want %v", propsal, true) + } +} + func TestNode(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel()