Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TestRaft_StartAsLeader #386

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,6 @@ func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps Sna
// Initialize as a follower.
r.setState(Follower)

// Start as leader if specified. This should only be used
// for testing purposes.
if conf.StartAsLeader {
r.setState(Leader)
r.setLeader(r.localAddr)
}

// Restore the current term and the last log.
r.setCurrentTerm(currentTerm)
r.setLastLog(lastLog.Index, lastLog.Term)
Expand Down
4 changes: 0 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ type Config struct {
// step down as leader.
LeaderLeaseTimeout time.Duration

// StartAsLeader forces Raft to start in the leader state. This should
// never be used except for testing purposes, as it can cause a split-brain.
StartAsLeader bool

// The unique ID for this server across all time. When running with
// ProtocolVersion < 3, you must set this to be the same as the network
// address of your transport.
Expand Down
45 changes: 0 additions & 45 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,51 +1653,6 @@ func TestRaft_VerifyLeader_PartialConnect(t *testing.T) {
}
}

func TestRaft_StartAsLeader(t *testing.T) {
conf := inmemConfig(t)
conf.StartAsLeader = true
c := MakeCluster(1, t, conf)
defer c.Close()
raft := c.rafts[0]

// Watch leaderCh for change
select {
case v := <-raft.LeaderCh():
if !v {
c.FailNowf("should become leader")
}
case <-time.After(c.conf.HeartbeatTimeout * 4):
// Longer than you think as possibility of multiple elections
c.FailNowf("timeout becoming leader")
}

// Should be leader
if s := raft.State(); s != Leader {
c.FailNowf("expected leader: %v", s)
}

// Should be able to apply
future := raft.Apply([]byte("test"), c.conf.CommitTimeout)
if err := future.Error(); err != nil {
c.FailNowf("err: %v", err)
}

// Check the response
if future.Response().(int) != 1 {
c.FailNowf("bad response: %v", future.Response())
}

// Check the index
if idx := future.Index(); idx == 0 {
c.FailNowf("bad index: %d", idx)
}

// Check that it is applied to the FSM
if len(getMockFSM(c.fsms[0]).logs) != 1 {
c.FailNowf("did not apply to FSM!")
}
}

func TestRaft_NotifyCh(t *testing.T) {
ch := make(chan bool, 1)
conf := inmemConfig(t)
Expand Down