Browse Source

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

Vincent Lee 9 years ago
parent
commit
e6d1ebcc1d
1 changed files with 5 additions and 3 deletions
  1. 5 3
      raft/node_test.go

+ 5 - 3
raft/node_test.go

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