|
@@ -111,9 +111,9 @@ func TestServerRequestVoteApprovedIfAlreadyVotedInOlderTerm(t *testing.T) {
|
|
|
// Ensure that a vote request is denied if the log is out of date.
|
|
// Ensure that a vote request is denied if the log is out of date.
|
|
|
func TestServerRequestVoteDenyIfCandidateLogIsBehind(t *testing.T) {
|
|
func TestServerRequestVoteDenyIfCandidateLogIsBehind(t *testing.T) {
|
|
|
tmpLog := newLog()
|
|
tmpLog := newLog()
|
|
|
- e0, _ := newLogEntry(tmpLog, 1, 1, &testCommand1{Val: "foo", I: 20})
|
|
|
|
|
- e1, _ := newLogEntry(tmpLog, 2, 1, &testCommand2{X: 100})
|
|
|
|
|
- e2, _ := newLogEntry(tmpLog, 3, 2, &testCommand1{Val: "bar", I: 0})
|
|
|
|
|
|
|
+ e0, _ := newLogEntry(tmpLog, nil, 1, 1, &testCommand1{Val: "foo", I: 20})
|
|
|
|
|
+ e1, _ := newLogEntry(tmpLog, nil, 2, 1, &testCommand2{X: 100})
|
|
|
|
|
+ e2, _ := newLogEntry(tmpLog, nil, 3, 2, &testCommand1{Val: "bar", I: 0})
|
|
|
s := newTestServerWithLog("1", &testTransporter{}, []*LogEntry{e0, e1, e2})
|
|
s := newTestServerWithLog("1", &testTransporter{}, []*LogEntry{e0, e1, e2})
|
|
|
|
|
|
|
|
// start as a follower with term 2 and index 3
|
|
// start as a follower with term 2 and index 3
|
|
@@ -151,7 +151,7 @@ func TestServerRequestVoteDenyIfCandidateLogIsBehind(t *testing.T) {
|
|
|
|
|
|
|
|
// // Ensure that we can self-promote a server to candidate, obtain votes and become a fearless leader.
|
|
// // Ensure that we can self-promote a server to candidate, obtain votes and become a fearless leader.
|
|
|
func TestServerPromoteSelf(t *testing.T) {
|
|
func TestServerPromoteSelf(t *testing.T) {
|
|
|
- e0, _ := newLogEntry(newLog(), 1, 1, &testCommand1{Val: "foo", I: 20})
|
|
|
|
|
|
|
+ e0, _ := newLogEntry(newLog(), nil, 1, 1, &testCommand1{Val: "foo", I: 20})
|
|
|
s := newTestServerWithLog("1", &testTransporter{}, []*LogEntry{e0})
|
|
s := newTestServerWithLog("1", &testTransporter{}, []*LogEntry{e0})
|
|
|
|
|
|
|
|
// start as a follower
|
|
// start as a follower
|
|
@@ -204,7 +204,7 @@ func TestServerAppendEntries(t *testing.T) {
|
|
|
defer s.Stop()
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// Append single entry.
|
|
// Append single entry.
|
|
|
- e, _ := newLogEntry(nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
|
|
|
|
+ e, _ := newLogEntry(nil, nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
entries := []*LogEntry{e}
|
|
entries := []*LogEntry{e}
|
|
|
resp := s.AppendEntries(newAppendEntriesRequest(1, 0, 0, 0, "ldr", entries))
|
|
resp := s.AppendEntries(newAppendEntriesRequest(1, 0, 0, 0, "ldr", entries))
|
|
|
if resp.Term != 1 || !resp.Success {
|
|
if resp.Term != 1 || !resp.Success {
|
|
@@ -215,8 +215,8 @@ func TestServerAppendEntries(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Append multiple entries + commit the last one.
|
|
// Append multiple entries + commit the last one.
|
|
|
- e1, _ := newLogEntry(nil, 2, 1, &testCommand1{Val: "bar", I: 20})
|
|
|
|
|
- e2, _ := newLogEntry(nil, 3, 1, &testCommand1{Val: "baz", I: 30})
|
|
|
|
|
|
|
+ e1, _ := newLogEntry(nil, nil, 2, 1, &testCommand1{Val: "bar", I: 20})
|
|
|
|
|
+ e2, _ := newLogEntry(nil, nil, 3, 1, &testCommand1{Val: "baz", I: 30})
|
|
|
entries = []*LogEntry{e1, e2}
|
|
entries = []*LogEntry{e1, e2}
|
|
|
resp = s.AppendEntries(newAppendEntriesRequest(1, 1, 1, 1, "ldr", entries))
|
|
resp = s.AppendEntries(newAppendEntriesRequest(1, 1, 1, 1, "ldr", entries))
|
|
|
if resp.Term != 1 || !resp.Success {
|
|
if resp.Term != 1 || !resp.Success {
|
|
@@ -248,7 +248,7 @@ func TestServerAppendEntriesWithStaleTermsAreRejected(t *testing.T) {
|
|
|
s.(*server).mutex.Unlock()
|
|
s.(*server).mutex.Unlock()
|
|
|
|
|
|
|
|
// Append single entry.
|
|
// Append single entry.
|
|
|
- e, _ := newLogEntry(nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
|
|
|
|
+ e, _ := newLogEntry(nil, nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
entries := []*LogEntry{e}
|
|
entries := []*LogEntry{e}
|
|
|
resp := s.AppendEntries(newAppendEntriesRequest(1, 0, 0, 0, "ldr", entries))
|
|
resp := s.AppendEntries(newAppendEntriesRequest(1, 0, 0, 0, "ldr", entries))
|
|
|
if resp.Term != 2 || resp.Success {
|
|
if resp.Term != 2 || resp.Success {
|
|
@@ -266,8 +266,8 @@ func TestServerAppendEntriesRejectedIfAlreadyCommitted(t *testing.T) {
|
|
|
defer s.Stop()
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
// Append single entry + commit.
|
|
// Append single entry + commit.
|
|
|
- e1, _ := newLogEntry(nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
|
|
- e2, _ := newLogEntry(nil, 2, 1, &testCommand1{Val: "foo", I: 15})
|
|
|
|
|
|
|
+ e1, _ := newLogEntry(nil, nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
|
|
+ e2, _ := newLogEntry(nil, nil, 2, 1, &testCommand1{Val: "foo", I: 15})
|
|
|
entries := []*LogEntry{e1, e2}
|
|
entries := []*LogEntry{e1, e2}
|
|
|
resp := s.AppendEntries(newAppendEntriesRequest(1, 0, 0, 2, "ldr", entries))
|
|
resp := s.AppendEntries(newAppendEntriesRequest(1, 0, 0, 2, "ldr", entries))
|
|
|
if resp.Term != 1 || !resp.Success {
|
|
if resp.Term != 1 || !resp.Success {
|
|
@@ -275,7 +275,7 @@ func TestServerAppendEntriesRejectedIfAlreadyCommitted(t *testing.T) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Append entry again (post-commit).
|
|
// Append entry again (post-commit).
|
|
|
- e, _ := newLogEntry(nil, 2, 1, &testCommand1{Val: "bar", I: 20})
|
|
|
|
|
|
|
+ e, _ := newLogEntry(nil, nil, 2, 1, &testCommand1{Val: "bar", I: 20})
|
|
|
entries = []*LogEntry{e}
|
|
entries = []*LogEntry{e}
|
|
|
resp = s.AppendEntries(newAppendEntriesRequest(1, 2, 1, 1, "ldr", entries))
|
|
resp = s.AppendEntries(newAppendEntriesRequest(1, 2, 1, 1, "ldr", entries))
|
|
|
if resp.Term != 1 || resp.Success {
|
|
if resp.Term != 1 || resp.Success {
|
|
@@ -289,9 +289,9 @@ func TestServerAppendEntriesOverwritesUncommittedEntries(t *testing.T) {
|
|
|
s.Start()
|
|
s.Start()
|
|
|
defer s.Stop()
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
- entry1, _ := newLogEntry(nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
|
|
- entry2, _ := newLogEntry(nil, 2, 1, &testCommand1{Val: "foo", I: 15})
|
|
|
|
|
- entry3, _ := newLogEntry(nil, 2, 2, &testCommand1{Val: "bar", I: 20})
|
|
|
|
|
|
|
+ entry1, _ := newLogEntry(nil, nil, 1, 1, &testCommand1{Val: "foo", I: 10})
|
|
|
|
|
+ entry2, _ := newLogEntry(nil, nil, 2, 1, &testCommand1{Val: "foo", I: 15})
|
|
|
|
|
+ entry3, _ := newLogEntry(nil, nil, 2, 2, &testCommand1{Val: "bar", I: 20})
|
|
|
|
|
|
|
|
// Append single entry + commit.
|
|
// Append single entry + commit.
|
|
|
entries := []*LogEntry{entry1, entry2}
|
|
entries := []*LogEntry{entry1, entry2}
|