Add simple voting test.

pull/820/head
Ben Johnson 2013-04-28 16:49:52 -06:00
parent f8148f1cc5
commit 4d3d5b8727
2 changed files with 25 additions and 9 deletions

View File

@ -65,6 +65,7 @@ func NewServer(name string, path string) (*Server, error) {
path: path,
state: Stopped,
log: NewLog(),
electionTimer: NewElectionTimer(DefaultElectionTimeout),
}
return s, nil
}
@ -324,16 +325,14 @@ func (s *Server) RequestVote(req *RequestVoteRequest) *RequestVoteResponse {
}
// If the candidate's log is not at least as up-to-date as our committed log then don't vote.
/*
lastCommitIndex, lastCommitTerm := s.log.LastCommitInfo()
if lastCommitIndex > req.LastLogIndex || lastCommitTerm > req.LastLogTerm {
return NewRequestVoteResponse(s.currentTerm, false)
}
lastCommitIndex, lastCommitTerm := s.log.CommitInfo()
if lastCommitIndex > req.LastLogIndex || lastCommitTerm > req.LastLogTerm {
return NewRequestVoteResponse(s.currentTerm, false)
}
// If we made it this far then cast a vote and reset our election time out.
s.votedFor = req.CandidateName
s.electionTimer.Reset()
*/
// If we made it this far then cast a vote and reset our election time out.
s.votedFor = req.CandidateName
s.electionTimer.Reset()
return NewRequestVoteResponse(s.currentTerm, true)
}

View File

@ -10,6 +10,23 @@ import (
//
//------------------------------------------------------------------------------
//--------------------------------------
// Request Vote
//--------------------------------------
// Ensure that we can request a vote from a server that has not voted.
func TestServerRequestVote(t *testing.T) {
server := newTestServer("1")
resp := server.RequestVote(NewRequestVoteRequest(1, "foo", 0, 0))
if !(resp.Term == 1 && resp.VoteGranted) {
t.Fatalf("Invalid request vote response: %v/%v", resp.Term, resp.VoteGranted)
}
}
//--------------------------------------
// Membership
//--------------------------------------
// Ensure that we can start a single server and append to its log.
func TestServerSingleNode(t *testing.T) {
server := newTestServer("1")