Skip to content

Commit

Permalink
Fix use of deprecated function grpc.WithTimeout() (hypermodeinc#3253)
Browse files Browse the repository at this point in the history
Replace uses of grpc.WithTimeout() with context.WithTimeout(). Fix test error messages.
  • Loading branch information
codexnull authored and dna2github committed Jul 19, 2019
1 parent 6516b1d commit c321aa1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 23 deletions.
9 changes: 6 additions & 3 deletions dgraph/cmd/bulk/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ type loader struct {

func newLoader(opt options) *loader {
fmt.Printf("Connecting to zero at %s\n", opt.ZeroAddr)
zero, err := grpc.Dial(opt.ZeroAddr,

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

zero, err := grpc.DialContext(ctx, opt.ZeroAddr,
grpc.WithBlock(),
grpc.WithInsecure(),
grpc.WithTimeout(time.Minute))
grpc.WithInsecure())
x.Checkf(err, "Unable to connect to zero, Is it running at %s?", opt.ZeroAddr)
st := &state{
opt: opt,
Expand Down
6 changes: 3 additions & 3 deletions dgraph/cmd/live/load-json/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestLiveLoadJSONFile(t *testing.T) {
"--dgraph", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading JSON file ran successfully")
require.NoError(t, err, "live loading JSON file exited with error")

checkLoadedData(t)
}
Expand All @@ -111,7 +111,7 @@ func TestLiveLoadJSONCompressedStream(t *testing.T) {
"--dgraph", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading JSON stream ran successfully")
require.NoError(t, err, "live loading JSON stream exited with error")

checkLoadedData(t)
}
Expand All @@ -132,7 +132,7 @@ func TestLiveLoadJSONMultipleFiles(t *testing.T) {
"--dgraph", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading multiple JSON files ran successfully")
require.NoError(t, err, "live loading multiple JSON files exited with error")

checkLoadedData(t)
}
Expand Down
8 changes: 4 additions & 4 deletions dgraph/cmd/live/load-uids/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestLiveLoadJsonUidKeep(t *testing.T) {
"--dgraph", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading JSON file ran successfully")
require.NoError(t, err, "live loading JSON file exited with error")

checkLoadedData(t, false)
}
Expand All @@ -137,7 +137,7 @@ func TestLiveLoadJsonUidDiscard(t *testing.T) {
"--dgraph", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading JSON file ran successfully")
require.NoError(t, err, "live loading JSON file exited with error")

checkLoadedData(t, true)
}
Expand All @@ -151,7 +151,7 @@ func TestLiveLoadRdfUidKeep(t *testing.T) {
"--dgraph", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading JSON file ran successfully")
require.NoError(t, err, "live loading JSON file exited with error")

checkLoadedData(t, false)
}
Expand All @@ -165,7 +165,7 @@ func TestLiveLoadRdfUidDiscard(t *testing.T) {
"--dgraph", alphaService},
}
err := z.Pipeline(pipeline)
require.NoError(t, err, "live loading JSON file ran successfully")
require.NoError(t, err, "live loading JSON file exited with error")

checkLoadedData(t, true)
}
Expand Down
9 changes: 6 additions & 3 deletions ee/backup/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ func runRestoreCmd() error {
// user.
if opt.zero != "" {
fmt.Println("Updating Zero timestamp at:", opt.zero)
zero, err := grpc.Dial(opt.zero,

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

zero, err := grpc.DialContext(ctx, opt.zero,
grpc.WithBlock(),
grpc.WithInsecure(),
grpc.WithTimeout(10*time.Second))
grpc.WithInsecure())
if err != nil {
return x.Wrapf(err, "Unable to connect to %s", opt.zero)
}
Expand Down
10 changes: 7 additions & 3 deletions x/x.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package x
import (
"bufio"
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -431,15 +432,18 @@ func SetupConnection(host string, tlsCfg *tls.Config, useGz bool) (*grpc.ClientC

dialOpts := append([]grpc.DialOption{},
grpc.WithDefaultCallOptions(callOpts...),
grpc.WithBlock(),
grpc.WithTimeout(10*time.Second))
grpc.WithBlock())

if tlsCfg != nil {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)))
} else {
dialOpts = append(dialOpts, grpc.WithInsecure())
}
return grpc.Dial(host, dialOpts...)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

return grpc.DialContext(ctx, host, dialOpts...)
}

func Diff(dst map[string]struct{}, src map[string]struct{}) ([]string, []string) {
Expand Down
16 changes: 9 additions & 7 deletions z/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import (
"github.com/dgraph-io/dgraph/x"
)

// for debugging the tests

// These are exported so they can also be set directly from outside this package.
var (
showOutput bool = os.Getenv("DEBUG_SHOW_OUTPUT") != ""
showCommand bool = os.Getenv("DEBUG_SHOW_COMMAND") != ""
ShowOutput bool = os.Getenv("DEBUG_SHOW_OUTPUT") != ""
ShowError bool = os.Getenv("DEBUG_SHOW_ERROR") != ""
ShowCommand bool = os.Getenv("DEBUG_SHOW_COMMAND") != ""
)

// Pipeline runs several commands such that the output of one command becomes the input of the next.
Expand All @@ -44,7 +44,7 @@ func Pipeline(cmds [][]string) error {
// Run all commands in parallel, connecting stdin of each to the stdout of the previous.
for i, c := range cmds {
lastCmd := i == numCmds-1
if showCommand {
if ShowCommand {
fmt.Fprintf(os.Stderr, "%+v", c)
}

Expand All @@ -54,14 +54,16 @@ func Pipeline(cmds [][]string) error {
p, _ = cmd[i].StdoutPipe()
}

if showOutput {
if ShowOutput {
cmd[i].Stderr = os.Stderr
if lastCmd {
cmd[i].Stdout = os.Stdout
}
} else if ShowError {
cmd[i].Stderr = os.Stderr
}

if showCommand {
if ShowCommand {
if lastCmd {
fmt.Fprintf(os.Stderr, "\n")
} else {
Expand Down

0 comments on commit c321aa1

Please sign in to comment.