mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: test contain updates
This commit is contained in:
parent
b261a5edc1
commit
f9ef453894
@ -2,8 +2,8 @@ package raft
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/coreos/etcd/raft/raftpb"
|
"github.com/coreos/etcd/raft/raftpb"
|
||||||
"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
|
"github.com/coreos/etcd/third_party/code.google.com/p/go.net/context"
|
||||||
@ -85,33 +85,51 @@ func TestNodeStepUnblock(t *testing.T) {
|
|||||||
// know who is the current leader; node will direct proposal when it knows
|
// know who is the current leader; node will direct proposal when it knows
|
||||||
// who is the current leader.
|
// who is the current leader.
|
||||||
func TestBlockProposal(t *testing.T) {
|
func TestBlockProposal(t *testing.T) {
|
||||||
propsal := false
|
|
||||||
|
|
||||||
n := newNode()
|
n := newNode()
|
||||||
defer n.Stop()
|
defer n.Stop()
|
||||||
r := newRaft(1, []int64{1}, 10, 1)
|
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.run(r)
|
||||||
go n.Propose(context.TODO(), []byte("somedata"))
|
|
||||||
// give some time for go routines sechduling ...
|
errc := make(chan error, 1)
|
||||||
time.Sleep(time.Millisecond * 2)
|
go func() {
|
||||||
if propsal {
|
errc <- n.Propose(context.TODO(), []byte("somedata"))
|
||||||
t.Fatalf("proposal = %v, want %v", propsal, false)
|
}()
|
||||||
|
|
||||||
|
mustEnoughSched()
|
||||||
|
select {
|
||||||
|
case err := <-errc:
|
||||||
|
t.Errorf("err = %v, want blocking", err)
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign a lead to raft.
|
n.Campaign(context.TODO())
|
||||||
// tick to update the node.
|
mustEnoughSched()
|
||||||
r.lead = 1
|
select {
|
||||||
n.Tick()
|
case err := <-errc:
|
||||||
// give some time for go routines sechduling ...
|
if err != nil {
|
||||||
time.Sleep(time.Millisecond * 2)
|
t.Errorf("err = %v, want %v", err, nil)
|
||||||
if !propsal {
|
}
|
||||||
t.Fatalf("proposal = %v, want %v", propsal, true)
|
default:
|
||||||
|
t.Errorf("blocking proposal, want unblocking")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReadyContainUpdates(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
rd Ready
|
||||||
|
wcontain bool
|
||||||
|
}{
|
||||||
|
{Ready{}, false},
|
||||||
|
{Ready{State: raftpb.State{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},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tt := range tests {
|
||||||
|
if tt.rd.containsUpdates() != tt.wcontain {
|
||||||
|
t.Errorf("#%d: containUpdates = %v, want %v", i, tt.rd.containsUpdates(), tt.wcontain)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,3 +192,10 @@ func TestNodeRestart(t *testing.T) {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mustEnoughSched() {
|
||||||
|
// possibility enough to sched upto 10 go routines.
|
||||||
|
for i := 0; i < 10000; i++ {
|
||||||
|
runtime.Gosched()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user