Browse Source

Merge pull request #1696 from xiang90/testnodetick

raft: add a test for node.Tick
Xiang Li 11 years ago
parent
commit
b271e88c20
2 changed files with 21 additions and 2 deletions
  1. 7 2
      raft/node.go
  2. 14 0
      raft/node_test.go

+ 7 - 2
raft/node.go

@@ -192,6 +192,7 @@ type node struct {
 	advancec chan struct{}
 	advancec chan struct{}
 	tickc    chan struct{}
 	tickc    chan struct{}
 	done     chan struct{}
 	done     chan struct{}
+	stop     chan struct{}
 }
 }
 
 
 func newNode() node {
 func newNode() node {
@@ -204,11 +205,13 @@ func newNode() node {
 		advancec: make(chan struct{}),
 		advancec: make(chan struct{}),
 		tickc:    make(chan struct{}),
 		tickc:    make(chan struct{}),
 		done:     make(chan struct{}),
 		done:     make(chan struct{}),
+		stop:     make(chan struct{}),
 	}
 	}
 }
 }
 
 
 func (n *node) Stop() {
 func (n *node) Stop() {
-	close(n.done)
+	n.stop <- struct{}{}
+	<-n.stop
 }
 }
 
 
 func (n *node) run(r *raft) {
 func (n *node) run(r *raft) {
@@ -302,7 +305,9 @@ func (n *node) run(r *raft) {
 			}
 			}
 			r.raftLog.stableTo(prevLastUnstablei)
 			r.raftLog.stableTo(prevLastUnstablei)
 			advancec = nil
 			advancec = nil
-		case <-n.done:
+		case <-n.stop:
+			n.stop <- struct{}{}
+			close(n.done)
 			return
 			return
 		}
 		}
 	}
 	}

+ 14 - 0
raft/node_test.go

@@ -140,6 +140,20 @@ func TestBlockProposal(t *testing.T) {
 	}
 	}
 }
 }
 
 
+// TestNodeTick ensures that node.Tick() will increase the
+// elapsed of the underly raft state machine.
+func TestNodeTick(t *testing.T) {
+	n := newNode()
+	r := newRaft(1, []uint64{1}, 10, 1)
+	go n.run(r)
+	elapsed := r.elapsed
+	n.Tick()
+	n.Stop()
+	if r.elapsed != elapsed+1 {
+		t.Errorf("elapsed = %d, want %d", r.elapsed, elapsed+1)
+	}
+}
+
 func TestReadyContainUpdates(t *testing.T) {
 func TestReadyContainUpdates(t *testing.T) {
 	tests := []struct {
 	tests := []struct {
 		rd       Ready
 		rd       Ready