From 674b79e645a7043a601517a076da0eadaca36a6b Mon Sep 17 00:00:00 2001 From: Aman Mangal Date: Thu, 27 Feb 2020 21:16:14 +0530 Subject: [PATCH] more fixes --- dgraph/cmd/alpha/http_test.go | 8 +++++--- systest/mutations_test.go | 7 +++---- systest/plugin_test.go | 3 +-- systest/queries_test.go | 38 +++++++++++++++++++++++++---------- testutil/client.go | 10 ++++++++- 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/dgraph/cmd/alpha/http_test.go b/dgraph/cmd/alpha/http_test.go index 6aee22b453b..33d0cca5c84 100644 --- a/dgraph/cmd/alpha/http_test.go +++ b/dgraph/cmd/alpha/http_test.go @@ -276,12 +276,14 @@ func runWithRetries(method, contentType, url string, body string) ( } switch { + case retries > 20: + return qr, respBody, err + case strings.Contains(err.Error(), "already being modified"): + time.Sleep(time.Second) case strings.Contains(err.Error(), "is not indexed") || strings.Contains(err.Error(), "doesn't have reverse edge") || - strings.Contains(err.Error(), "Need @count directive in schema") || - strings.Contains(err.Error(), "already being modified"): + strings.Contains(err.Error(), "Need @count directive in schema"): time.Sleep(time.Millisecond * 100) - continue case strings.Contains(err.Error(), "Token is expired"): grootAccessJwt, grootRefreshJwt, err = testutil.HttpLogin(&testutil.LoginParams{ Endpoint: addr + "/admin", diff --git a/systest/mutations_test.go b/systest/mutations_test.go index 5a31ea38f78..0a7184a44a7 100644 --- a/systest/mutations_test.go +++ b/systest/mutations_test.go @@ -747,8 +747,10 @@ func ListToScalar(t *testing.T, c *dgo.Dgraph) { ctx := context.Background() require.NoError(t, c.Alter(ctx, &api.Operation{Schema: `pred: [string] @index(exact) .`})) + _, err := testutil.RetryQuery(ctx, c, `{q(func: eq(pred, "")){uid}}`) + require.NoError(t, err) - err := c.Alter(ctx, &api.Operation{Schema: `pred: string @index(exact) .`}) + err = c.Alter(ctx, &api.Operation{Schema: `pred: string @index(exact) .`}) require.Error(t, err) require.Contains(t, err.Error(), `Type can't be changed from list to scalar for attr: [pred] without dropping it first.`) @@ -1379,7 +1381,6 @@ func HasDeletedEdge(t *testing.T, c *dgo.Dgraph) { you(func: has(end)) { count(uid) } }`) check(t, err) - t.Logf("resp: %s\n", resp.GetJson()) m := make(map[string][]U) err = json.Unmarshal(resp.GetJson(), &m) check(t, err) @@ -1405,7 +1406,6 @@ func HasDeletedEdge(t *testing.T, c *dgo.Dgraph) { deleteMu.DelNquads = []byte(fmt.Sprintf(` <%s> * . `, ids[len(ids)-1])) - t.Logf("deleteMu: %+v\n", deleteMu) _, err = txn.Mutate(ctx, deleteMu) check(t, err) @@ -1478,7 +1478,6 @@ func HasReverseEdge(t *testing.T, c *dgo.Dgraph) { }`) check(t, err) - t.Logf("resp: %s\n", resp.GetJson()) m := make(map[string][]F) err = json.Unmarshal(resp.GetJson(), &m) check(t, err) diff --git a/systest/plugin_test.go b/systest/plugin_test.go index 62821388dbd..3945114aa86 100644 --- a/systest/plugin_test.go +++ b/systest/plugin_test.go @@ -87,8 +87,7 @@ func TestPlugins(t *testing.T) { check(t, txn.Commit(ctx)) for _, test := range cases { - txn := cluster.client.NewTxn() - reply, err := txn.Query(ctx, test.query) + reply, err := testutil.RetryQuery(ctx, cluster.client, test.query) check(t, err) testutil.CompareJSON(t, test.wantResult, string(reply.GetJson())) } diff --git a/systest/queries_test.go b/systest/queries_test.go index df7cef19eac..7db6e003339 100644 --- a/systest/queries_test.go +++ b/systest/queries_test.go @@ -215,9 +215,8 @@ func MultipleBlockEval(t *testing.T, c *dgo.Dgraph) { } }` - txn = c.NewTxn() for _, tc := range tests { - resp, err := txn.Query(ctx, fmt.Sprintf(queryFmt, tc.in)) + resp, err := testutil.RetryQuery(ctx, c, fmt.Sprintf(queryFmt, tc.in)) require.NoError(t, err) testutil.CompareJSON(t, tc.out, string(resp.Json)) } @@ -306,9 +305,8 @@ func UnmatchedVarEval(t *testing.T, c *dgo.Dgraph) { }, } - txn = c.NewTxn() for _, tc := range tests { - resp, err := txn.Query(ctx, tc.in) + resp, err := testutil.RetryQuery(ctx, c, tc.in) require.NoError(t, err) testutil.CompareJSON(t, tc.out, string(resp.Json)) } @@ -329,6 +327,10 @@ func SchemaQueryTest(t *testing.T, c *dgo.Dgraph) { require.NoError(t, err) require.NoError(t, txn.Commit(ctx)) + // wait for schema to get updated + _, err = testutil.RetryQuery(ctx, c, `{q(func: eq(name, "")){uid}}`) + require.NoError(t, err) + txn = c.NewTxn() resp, err := txn.Query(ctx, `schema {}`) require.NoError(t, err) @@ -438,6 +440,10 @@ func SchemaQueryTestPredicate2(t *testing.T, c *dgo.Dgraph) { require.NoError(t, err) require.NoError(t, txn.Commit(ctx)) + // wait for schema to get updated + _, err = testutil.RetryQuery(ctx, c, `{q(func: eq(name, "")){uid}}`) + require.NoError(t, err) + txn = c.NewTxn() resp, err := txn.Query(ctx, `schema(pred: [name]) {}`) require.NoError(t, err) @@ -700,7 +706,7 @@ func FuzzyMatch(t *testing.T, c *dgo.Dgraph) { }, } for _, tc := range tests { - resp, err := c.NewTxn().Query(ctx, tc.in) + resp, err := testutil.RetryQuery(ctx, c, tc.in) if tc.failure != "" { require.Error(t, err) require.Contains(t, err.Error(), tc.failure) @@ -739,6 +745,10 @@ func QueryHashIndex(t *testing.T, c *dgo.Dgraph) { require.NoError(t, err) require.NoError(t, txn.Commit(ctx)) + // wait for indexing to complete + _, err = testutil.RetryQuery(ctx, c, `{q(func: eq(name, "")){uid}}`) + require.NoError(t, err) + tests := []struct { in, out string }{ @@ -818,7 +828,7 @@ func QueryHashIndex(t *testing.T, c *dgo.Dgraph) { } for _, tc := range tests { - resp, err := c.NewTxn().Query(ctx, tc.in) + resp, err := testutil.RetryQuery(ctx, c, tc.in) require.NoError(t, err) testutil.CompareJSON(t, tc.out, string(resp.Json)) } @@ -832,9 +842,11 @@ func RegexpToggleTrigramIndex(t *testing.T, c *dgo.Dgraph) { name: string @index(term) @lang . `, })) + _, err := testutil.RetryQuery(ctx, c, `{q(func: anyofterms(name, "")){uid}}`) + require.NoError(t, err) txn := c.NewTxn() - _, err := txn.Mutate(ctx, &api.Mutation{ + _, err = txn.Mutate(ctx, &api.Mutation{ SetNquads: []byte(` _:x1 "The luck is in the details" . _:x1 "The art is in the details"@en . @@ -867,7 +879,7 @@ func RegexpToggleTrigramIndex(t *testing.T, c *dgo.Dgraph) { t.Log("testing without trigram index") for _, tc := range tests { - resp, err := c.NewTxn().Query(ctx, tc.in) + resp, err := testutil.RetryQuery(ctx, c, tc.in) require.NoError(t, err) testutil.CompareJSON(t, tc.out, string(resp.Json)) } @@ -877,10 +889,12 @@ func RegexpToggleTrigramIndex(t *testing.T, c *dgo.Dgraph) { name: string @index(trigram) @lang . `, })) + _, err = testutil.RetryQuery(ctx, c, `{q(func: regexp(name, /name/)){uid}}`) + require.NoError(t, err) t.Log("testing with trigram index") for _, tc := range tests { - resp, err := c.NewTxn().Query(ctx, tc.in) + resp, err := testutil.RetryQuery(ctx, c, tc.in) require.NoError(t, err) testutil.CompareJSON(t, tc.out, string(resp.Json)) } @@ -890,9 +904,11 @@ func RegexpToggleTrigramIndex(t *testing.T, c *dgo.Dgraph) { name: string @index(term) @lang . `, })) + _, err = testutil.RetryQuery(ctx, c, `{q(func: anyofterms(name, "")){uid}}`) + require.NoError(t, err) t.Log("testing without trigram index at root") - _, err = c.NewTxn().Query(ctx, `{q(func:regexp(name, /art/)) {name}}`) + _, err = testutil.RetryQuery(ctx, c, `{q(func:regexp(name, /art/)) {name}}`) require.Error(t, err) require.Contains(t, err.Error(), "Attribute name does not have trigram index for regex matching.") } @@ -936,7 +952,7 @@ func GroupByUidWorks(t *testing.T, c *dgo.Dgraph) { }, } for _, tc := range tests { - resp, err := c.NewTxn().Query(ctx, tc.in) + resp, err := testutil.RetryQuery(ctx, c, tc.in) require.NoError(t, err) testutil.CompareJSON(t, tc.out, string(resp.Json)) } diff --git a/testutil/client.go b/testutil/client.go index 6974c3f2bb2..485289d7872 100644 --- a/testutil/client.go +++ b/testutil/client.go @@ -182,12 +182,20 @@ func RetryQueryWithTxn(ctx context.Context, txn *dgo.Txn, q string) (*api.Respon } func retryQuery(ctx context.Context, f func() (*api.Response, error)) (*api.Response, error) { + retries := 0 + maxRetries := 200 for { + retries++ resp, err := f() + if retries > maxRetries { + return resp, err + } + if err != nil && (strings.Contains(err.Error(), "Please retry") || strings.Contains(err.Error(), "is not indexed") || strings.Contains(err.Error(), "doesn't have reverse edge") || - strings.Contains(err.Error(), "Need @count directive in schema")) { + strings.Contains(err.Error(), "Need @count directive in schema") || + strings.Contains(err.Error(), "does not have trigram index")) { time.Sleep(10 * time.Millisecond) continue