Add simple voting test.
parent
f8148f1cc5
commit
4d3d5b8727
17
server.go
17
server.go
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue