Skip to content

Commit

Permalink
integration: add test for fast fail while dropping index reading
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed Sep 13, 2018
1 parent 87ca397 commit c023117
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions integration/network_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package integration

import (
"context"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -139,6 +141,62 @@ func TestNetworkPartition4Members(t *testing.T) {
clusterMustProgress(t, clus.Members)
}

func TestFastFailReadUnderNotLeader(t *testing.T) {
defer testutil.AfterTest(t)

clus := NewClusterV3(t, &ClusterConfig{Size: 5})
defer clus.Terminate(t)

leadIndex := clus.WaitLeader(t)

// majority: leader, follower, follower / minority: follower, follower
majority := []int{leadIndex, (leadIndex + 1) % 5, (leadIndex + 2) % 5}
minority := []int{(leadIndex + 3) % 5, (leadIndex + 4) % 5}

majorityMembers := getMembersByIndexSlice(clus.cluster, majority)
minorityMembers := getMembersByIndexSlice(clus.cluster, minority)

cliIndex := minority[0]
cli := clus.Client(cliIndex)
timeout := 20 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
_, err := cli.Put(ctx, "foo", "bar")
cancel()
if err != nil {
t.Fatalf("get error = %v, want nil", err)
}

// network partition (bi-directional)
// minority leader must be lost
// the request must fast faild
injectPartition(t, majorityMembers, minorityMembers)
clus.waitNoLeader(minorityMembers)
clus.waitLeader(t, majorityMembers)

// test for reading index dropped
ctx, cancel = context.WithTimeout(context.Background(), timeout)
_, err = cli.Get(ctx, "foo")
cancel()
if !strings.Contains(err.Error(), "raft read index dropped") {
t.Fatalf("expect dropped read index error, but get %s", err)
}

// add the leader to the endpoints
// the client will switch
cli.SetEndpoints(majorityMembers[0].grpcAddr, cli.Endpoints()[0])
for i := 0; i < 5; i++ {
ctx, cancel = context.WithTimeout(context.Background(), timeout)
_, err = cli.Get(ctx, "foo")
cancel()
if err == nil {
break
}
}
if err != nil {
t.Fatalf("get error = %s, want nil", err)
}
}

func getMembersByIndexSlice(clus *cluster, idxs []int) []*member {
ms := make([]*member, len(idxs))
for i, idx := range idxs {
Expand Down

0 comments on commit c023117

Please sign in to comment.