raft: use the channel instead of sleep to make test case reliable

release-3.1
Vincent Lee 2016-11-21 13:30:15 +08:00
parent bc6f5ad53e
commit e6d1ebcc1d
1 changed files with 5 additions and 3 deletions

View File

@ -303,6 +303,7 @@ func TestNodeProposeAddDuplicateNode(t *testing.T) {
ticker := time.NewTicker(time.Millisecond * 100) ticker := time.NewTicker(time.Millisecond * 100)
done := make(chan struct{}) done := make(chan struct{})
stop := make(chan struct{}) stop := make(chan struct{})
applyChan := make(chan struct{})
go func() { go func() {
defer close(done) defer close(done)
for { for {
@ -324,21 +325,22 @@ func TestNodeProposeAddDuplicateNode(t *testing.T) {
} }
} }
n.Advance() n.Advance()
applyChan <- struct{}{}
} }
} }
}() }()
cc1 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1} cc1 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 1}
ccdata1, _ := cc1.Marshal() ccdata1, _ := cc1.Marshal()
n.ProposeConfChange(context.TODO(), cc1) n.ProposeConfChange(context.TODO(), cc1)
time.Sleep(time.Millisecond * 10) <-applyChan
// try add the same node again // try add the same node again
n.ProposeConfChange(context.TODO(), cc1) n.ProposeConfChange(context.TODO(), cc1)
time.Sleep(time.Millisecond * 10) <-applyChan
// the new node join should be ok // the new node join should be ok
cc2 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2} cc2 := raftpb.ConfChange{Type: raftpb.ConfChangeAddNode, NodeID: 2}
ccdata2, _ := cc2.Marshal() ccdata2, _ := cc2.Marshal()
n.ProposeConfChange(context.TODO(), cc2) n.ProposeConfChange(context.TODO(), cc2)
time.Sleep(time.Millisecond * 10) <-applyChan
close(stop) close(stop)
<-done <-done