Skip to content

Commit

Permalink
Fixing the flaky test TestNodes (dgraph-io#3530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Wang authored and dna2github committed Jul 19, 2019
1 parent 8f29f30 commit e185fc4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
6 changes: 2 additions & 4 deletions systest/group-delete/group_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/z"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)

func NodesSetup(t *testing.T, c *dgo.Dgraph) {
Expand Down Expand Up @@ -125,9 +124,8 @@ func getError(rc io.ReadCloser) error {
}

func TestNodes(t *testing.T) {
conn, err := grpc.Dial(z.SockAddr, grpc.WithInsecure())
require.NoError(t, err)
dg := dgo.NewDgraphClient(api.NewDgraphClient(conn))
dg, err := z.GetClientToGroup("1")
require.NoError(t, err, "error while getting connection to group 1")

NodesSetup(t, dg)

Expand Down
50 changes: 49 additions & 1 deletion z/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,25 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"

"github.com/dgraph-io/dgo"
"github.com/dgraph-io/dgo/protos/api"
"google.golang.org/grpc"
)

// StateResponse represents the structure of the JSON object returned by calling
// the /state endpoint in zero.
type StateResponse struct {
Groups map[string]struct {
Members map[string]interface{} `json:"members"`
Members map[string]struct {
Addr string `json:"addr"`
GroupID int `json:"groupId"`
ID string `json:"id"`
LastUpdate string `json:"lastUpdate"`
Leader bool `json:"leader"`
} `json:"members"`
Tablets map[string]struct {
GroupID int `json:"groupId"`
Predicate string `json:"predicate"`
Expand Down Expand Up @@ -64,3 +76,39 @@ func GetState() (*StateResponse, error) {
}
return &st, nil
}

func GetClientToGroup(groupID string) (*dgo.Dgraph, error) {
state, err := GetState()
if err != nil {
return nil, err
}

group, ok := state.Groups[groupID]
if !ok {
return nil, fmt.Errorf("group %s does not exist", groupID)
}

if len(group.Members) == 0 {
return nil, fmt.Errorf("the group %s has no members", groupID)
}

member := group.Members["1"]
parts := strings.Split(member.Addr, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("the member has an invalid address: %v", member.Addr)
}
// internalPort is used for communication between alpha nodes
internalPort, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("unable to parse the port number from %s", parts[1])
}

// externalPort is for handling connections from clients
externalPort := internalPort + 2000

conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", externalPort), grpc.WithInsecure())
if err != nil {
return nil, err
}
return dgo.NewDgraphClient(api.NewDgraphClient(conn)), nil
}

0 comments on commit e185fc4

Please sign in to comment.