refactor use separate lock instead of having setTerm function

pull/820/head
Xiang Li 2013-11-05 19:12:26 -08:00
parent d4748404ab
commit 358e8f148d
2 changed files with 13 additions and 12 deletions

View File

@ -67,7 +67,6 @@ type Server interface {
LogPath() string LogPath() string
SnapshotPath(lastIndex uint64, lastTerm uint64) string SnapshotPath(lastIndex uint64, lastTerm uint64) string
Term() uint64 Term() uint64
setTerm(term uint64)
CommitIndex() uint64 CommitIndex() uint64
VotedFor() string VotedFor() string
MemberCount() int MemberCount() int
@ -260,13 +259,6 @@ func (s *server) Term() uint64 {
return s.currentTerm return s.currentTerm
} }
// Set the current term of the server, for testing purpose.
func (s *server) setTerm(term uint64) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.currentTerm = term
}
// Retrieves the current commit index of the server. // Retrieves the current commit index of the server.
func (s *server) CommitIndex() uint64 { func (s *server) CommitIndex() uint64 {
s.log.mutex.RLock() s.log.mutex.RLock()

View File

@ -45,7 +45,10 @@ func TestServerRequestVoteDeniedForStaleTerm(t *testing.T) {
t.Fatalf("Server %s unable to join: %v", s.Name(), err) t.Fatalf("Server %s unable to join: %v", s.Name(), err)
} }
s.(*server).setTerm(2) s.(*server).mutex.Lock()
s.(*server).currentTerm = 2
s.(*server).mutex.Unlock()
defer s.Stop() defer s.Stop()
resp := s.RequestVote(newRequestVoteRequest(1, "foo", 1, 0)) resp := s.RequestVote(newRequestVoteRequest(1, "foo", 1, 0))
if resp.Term != 2 || resp.VoteGranted { if resp.Term != 2 || resp.VoteGranted {
@ -65,7 +68,9 @@ func TestServerRequestVoteDeniedIfAlreadyVoted(t *testing.T) {
t.Fatalf("Server %s unable to join: %v", s.Name(), err) t.Fatalf("Server %s unable to join: %v", s.Name(), err)
} }
s.(*server).setTerm(2) s.(*server).mutex.Lock()
s.(*server).currentTerm = 2
s.(*server).mutex.Unlock()
defer s.Stop() defer s.Stop()
resp := s.RequestVote(newRequestVoteRequest(2, "foo", 1, 0)) resp := s.RequestVote(newRequestVoteRequest(2, "foo", 1, 0))
if resp.Term != 2 || !resp.VoteGranted { if resp.Term != 2 || !resp.VoteGranted {
@ -88,7 +93,9 @@ func TestServerRequestVoteApprovedIfAlreadyVotedInOlderTerm(t *testing.T) {
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
s.(*server).setTerm(2) s.(*server).mutex.Lock()
s.(*server).currentTerm = 2
s.(*server).mutex.Unlock()
defer s.Stop() defer s.Stop()
resp := s.RequestVote(newRequestVoteRequest(2, "foo", 2, 1)) resp := s.RequestVote(newRequestVoteRequest(2, "foo", 2, 1))
if resp.Term != 2 || !resp.VoteGranted || s.VotedFor() != "foo" { if resp.Term != 2 || !resp.VoteGranted || s.VotedFor() != "foo" {
@ -236,7 +243,9 @@ func TestServerAppendEntriesWithStaleTermsAreRejected(t *testing.T) {
s.Start() s.Start()
defer s.Stop() defer s.Stop()
s.(*server).setTerm(2) s.(*server).mutex.Lock()
s.(*server).currentTerm = 2
s.(*server).mutex.Unlock()
// Append single entry. // Append single entry.
e, _ := newLogEntry(nil, 1, 1, &testCommand1{Val: "foo", I: 10}) e, _ := newLogEntry(nil, 1, 1, &testCommand1{Val: "foo", I: 10})