From c321aa195c0768ffb2b5e5b784c12fc215bfa98a Mon Sep 17 00:00:00 2001 From: Javier Alvarado <35480615+codexnull@users.noreply.github.com> Date: Wed, 10 Apr 2019 18:06:16 -0700 Subject: [PATCH] Fix use of deprecated function grpc.WithTimeout() (#3253) Replace uses of grpc.WithTimeout() with context.WithTimeout(). Fix test error messages. --- dgraph/cmd/bulk/loader.go | 9 ++++++--- dgraph/cmd/live/load-json/load_test.go | 6 +++--- dgraph/cmd/live/load-uids/load_test.go | 8 ++++---- ee/backup/run.go | 9 ++++++--- x/x.go | 10 +++++++--- z/exec.go | 16 +++++++++------- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/dgraph/cmd/bulk/loader.go b/dgraph/cmd/bulk/loader.go index b59bb1aca69..31ff90d439c 100644 --- a/dgraph/cmd/bulk/loader.go +++ b/dgraph/cmd/bulk/loader.go @@ -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, diff --git a/dgraph/cmd/live/load-json/load_test.go b/dgraph/cmd/live/load-json/load_test.go index d1e5c7d0f44..b1059b4f8b1 100644 --- a/dgraph/cmd/live/load-json/load_test.go +++ b/dgraph/cmd/live/load-json/load_test.go @@ -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) } @@ -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) } @@ -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) } diff --git a/dgraph/cmd/live/load-uids/load_test.go b/dgraph/cmd/live/load-uids/load_test.go index 66a75b254b0..3026bdbf633 100644 --- a/dgraph/cmd/live/load-uids/load_test.go +++ b/dgraph/cmd/live/load-uids/load_test.go @@ -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) } @@ -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) } @@ -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) } @@ -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) } diff --git a/ee/backup/run.go b/ee/backup/run.go index 3c8b0ae29fd..ed592ee246d 100644 --- a/ee/backup/run.go +++ b/ee/backup/run.go @@ -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) } diff --git a/x/x.go b/x/x.go index ddab9881adb..1a543b6d0b2 100644 --- a/x/x.go +++ b/x/x.go @@ -19,6 +19,7 @@ package x import ( "bufio" "bytes" + "context" "crypto/tls" "encoding/json" "fmt" @@ -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) { diff --git a/z/exec.go b/z/exec.go index 6ab23511e8f..2cc50ec6625 100644 --- a/z/exec.go +++ b/z/exec.go @@ -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. @@ -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) } @@ -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 {