Skip to content

Commit

Permalink
Adding test for node join
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Nov 13, 2013
1 parent 907df05 commit 49c5e98
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func (m *MockFSM) Apply(log []byte) {
// Return configurations optimized for in-memory
func inmemConfig() *Config {
return &Config{
HeartbeatTimeout: 5 * time.Millisecond,
HeartbeatTimeout: 10 * time.Millisecond,
ElectionTimeout: 10 * time.Millisecond,
CommitTimeout: time.Millisecond,
MaxAppendEntries: 16,
ShutdownOnRemove: true,
}
}

Expand Down Expand Up @@ -63,7 +64,7 @@ func TestRaft_SingleNode(t *testing.T) {
}
defer raft.Shutdown()

time.Sleep(15 * time.Millisecond)
time.Sleep(conf.HeartbeatTimeout * 3)

// Should be leader
if s := raft.State(); s != Leader {
Expand All @@ -90,6 +91,14 @@ type cluster struct {
rafts []*Raft
}

func (c *cluster) Merge(other *cluster) {
c.dirs = append(c.dirs, other.dirs...)
c.stores = append(c.stores, other.stores...)
c.fsms = append(c.fsms, other.fsms...)
c.trans = append(c.trans, other.trans...)
c.rafts = append(c.rafts, other.rafts...)
}

func (c *cluster) Close() {
for _, r := range c.rafts {
r.Shutdown()
Expand Down Expand Up @@ -399,3 +408,56 @@ func TestRaft_ApplyConcurrent(t *testing.T) {
// Check the FSMs
c.EnsureSame(t)
}

func TestRaft_JoinNode(t *testing.T) {
// Make a cluster
c := MakeCluster(2, t, nil)
defer c.Close()

// Apply a log to this cluster to ensure it is 'newer'
leader := c.Leader()
future := leader.Apply([]byte("first"), 0)
if err := future.Error(); err != nil {
t.Fatalf("err: %v", err)
} else {
log.Printf("[INFO] Applied log")
}

// Make a new cluster of 1
c1 := MakeCluster(1, t, nil)

// Merge clusters
c.Merge(c1)
c.FullyConnect()

// Wait until we have 2 leaders
limit := time.Now().Add(100 * time.Millisecond)
var leaders []*Raft
for time.Now().Before(limit) && len(leaders) != 2 {
time.Sleep(10 * time.Millisecond)
leaders = c.GetInState(Leader)
}
if len(leaders) != 2 {
t.Fatalf("expected two leader: %v", leaders)
}

// Join the new node in
future = leader.AddPeer(c1.rafts[0].localAddr)
if err := future.Error(); err != nil {
t.Fatalf("err: %v", err)
}

// Wait until we have 2 followers
limit = time.Now().Add(100 * time.Millisecond)
var followers []*Raft
for time.Now().Before(limit) && len(followers) != 2 {
time.Sleep(10 * time.Millisecond)
followers = c.GetInState(Follower)
}
if len(followers) != 2 {
t.Fatalf("expected two followers: %v", followers)
}

// Check the FSMs
c.EnsureSame(t)
}

0 comments on commit 49c5e98

Please sign in to comment.