Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GraphQL): fixes flaky test for subscriptions. #6065

Merged
merged 13 commits into from
Oct 8, 2020
Merged
82 changes: 46 additions & 36 deletions graphql/e2e/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ const (
`
)

func TestSubscription(t *testing.T) {
var subExp = 3 * time.Second
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid global variables to improve readability and reduce complexity

View Rule


func TestSubscription(t *testing.T) {
dg, err := testutil.DgraphClient(groupOnegRPC)
require.NoError(t, err)
testutil.DropAll(t, dg)
Expand All @@ -89,6 +90,7 @@ func TestSubscription(t *testing.T) {
}
addResult := add.ExecuteAsPost(t, adminEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second * 2)

add = &common.GraphQLParams{
Query: `mutation {
Expand All @@ -104,18 +106,15 @@ func TestSubscription(t *testing.T) {
}
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
resp := make(map[string]interface{})
require.NoError(t, json.Unmarshal(addResult.Data, &resp))
productID := resp["addProduct"].(map[string]interface{})["product"].([]interface{})[0].(map[string]interface{})["productID"].(string)
time.Sleep(time.Second)
subscriptionClient, err := common.NewGraphQLSubscription(subscriptionEndpoint, &schema.Request{
Query: fmt.Sprintf(`subscription{
getProduct(productID: "%s"){
Query: `subscription{
queryProduct{
name
}
}`, productID),
}`,
}, `{}`)
require.Nil(t, err)

res, err := subscriptionClient.RecvMsg()
require.NoError(t, err)

Expand All @@ -125,13 +124,10 @@ func TestSubscription(t *testing.T) {
require.NoError(t, err)
require.Nil(t, subscriptionResp.Errors)

require.JSONEq(t, `{"getProduct":{"name":"sanitizer"}}`, string(subscriptionResp.Data))
require.JSONEq(t, `{"queryProduct":[{"name":"sanitizer"}]}`, string(subscriptionResp.Data))
require.Contains(t, subscriptionResp.Extensions, touchedUidskey)
require.Greater(t, int(subscriptionResp.Extensions[touchedUidskey].(float64)), 0)

// Background indexing is happening so wait till it get indexed.
time.Sleep(time.Second * 2)

// Update the product to get the latest update.
add = &common.GraphQLParams{
Query: `mutation{
Expand All @@ -145,6 +141,7 @@ func TestSubscription(t *testing.T) {
}
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

res, err = subscriptionClient.RecvMsg()
require.NoError(t, err)
Expand All @@ -157,11 +154,10 @@ func TestSubscription(t *testing.T) {
require.Nil(t, subscriptionResp.Errors)

// Check the latest update.
require.JSONEq(t, `{"getProduct":{"name":"mask"}}`, string(subscriptionResp.Data))
require.JSONEq(t, `{"queryProduct":[{"name":"mask"}]}`, string(subscriptionResp.Data))
require.Contains(t, subscriptionResp.Extensions, touchedUidskey)
require.Greater(t, int(subscriptionResp.Extensions[touchedUidskey].(float64)), 0)

time.Sleep(2 * time.Second)
// Change schema to terminate subscription..
add = &common.GraphQLParams{
Query: `mutation updateGQLSchema($sch: String!) {
Expand All @@ -175,10 +171,9 @@ func TestSubscription(t *testing.T) {
}
addResult = add.ExecuteAsPost(t, adminEndpoint)
require.Nil(t, addResult.Errors)

time.Sleep(time.Second)
res, err = subscriptionClient.RecvMsg()
require.NoError(t, err)

require.Nil(t, res)
}

Expand Down Expand Up @@ -229,8 +224,9 @@ func TestSubscriptionAuth(t *testing.T) {

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

jwtToken, err := metaInfo.GetSignedToken("secret", 100*time.Second)
jwtToken, err := metaInfo.GetSignedToken("secret", subExp)
require.NoError(t, err)

payload := fmt.Sprintf(`{"Authorization": "%s"}`, jwtToken)
Expand Down Expand Up @@ -292,6 +288,8 @@ func TestSubscriptionAuth(t *testing.T) {

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

res, err = subscriptionClient.RecvMsg()
require.NoError(t, err)

Expand Down Expand Up @@ -321,6 +319,7 @@ func TestSubscriptionAuth(t *testing.T) {
}
addResult = add.ExecuteAsPost(t, adminEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

res, err = subscriptionClient.RecvMsg()
require.NoError(t, err)
Expand Down Expand Up @@ -374,8 +373,9 @@ func TestSubscriptionWithAuthShouldExpireWithJWT(t *testing.T) {

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

jwtToken, err := metaInfo.GetSignedToken("secret", 10*time.Second)
jwtToken, err := metaInfo.GetSignedToken("secret", subExp)
require.NoError(t, err)

payload := fmt.Sprintf(`{"Authorization": "%s"}`, jwtToken)
Expand All @@ -402,7 +402,7 @@ func TestSubscriptionWithAuthShouldExpireWithJWT(t *testing.T) {
string(resp.Data))

// Wait for JWT to expire.
time.Sleep(10 * time.Second)
time.Sleep(subExp)

// Add another TODO for bob but this should not be visible as the subscription should have
// ended.
Expand All @@ -423,6 +423,7 @@ func TestSubscriptionWithAuthShouldExpireWithJWT(t *testing.T) {

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

res, err = subscriptionClient.RecvMsg()
require.NoError(t, err)
Expand Down Expand Up @@ -504,7 +505,6 @@ func TestSubscriptionAuthWithoutExpiry(t *testing.T) {
}

func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndependently(t *testing.T) {
t.Skip()
dg, err := testutil.DgraphClient(groupOnegRPC)
require.NoError(t, err)
testutil.DropAll(t, dg)
Expand Down Expand Up @@ -551,8 +551,9 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

jwtToken, err := metaInfo.GetSignedToken("secret", 10*time.Second)
jwtToken, err := metaInfo.GetSignedToken("secret", subExp)
require.NoError(t, err)

// first subscription
Expand All @@ -578,7 +579,7 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep
string(resp.Data))

// 2nd subscription
jwtToken, err = metaInfo.GetSignedToken("secret", 20*time.Second)
jwtToken, err = metaInfo.GetSignedToken("secret", 2*subExp)
require.NoError(t, err)
payload = fmt.Sprintf(`{"Authorization": "%s"}`, jwtToken)
subscriptionClient1, err := common.NewGraphQLSubscription(subscriptionEndpoint, &schema.Request{
Expand All @@ -593,16 +594,14 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep

res, err = subscriptionClient1.RecvMsg()
require.NoError(t, err)

err = json.Unmarshal(res, &resp)
require.NoError(t, err)

require.Nil(t, resp.Errors)
require.JSONEq(t, `{"queryTodo":[{"owner":"jatin","text":"GraphQL is exciting!!"}]}`,
string(resp.Data))

// Wait for JWT to expire for first subscription.
time.Sleep(10 * time.Second)
time.Sleep(subExp)

// Add another TODO for jatin for which 1st subscription shouldn't get updates.
add = &common.GraphQLParams{
Expand All @@ -621,15 +620,18 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep
}
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

res, err = subscriptionClient.RecvMsg()
require.NoError(t, err)
require.Nil(t, res) // 1st subscription should get the empty response as subscription has expired.

time.Sleep(time.Second)
res, err = subscriptionClient1.RecvMsg()
require.NoError(t, err)
err = json.Unmarshal(res, &resp)
require.NoError(t, err)
require.Nil(t, resp.Errors)
// 2nd one still running and should get the update
require.JSONEq(t, `{"queryTodo": [
{
Expand All @@ -642,8 +644,8 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep
}]}`, string(resp.Data))

// add extra delay for 2nd subscription to timeout
time.Sleep(10 * time.Second)
// Add another TODO for jatin for which 2nd subscription shouldn't get updates.
time.Sleep(subExp)
// Add another TODO for jatin for which 2nd subscription shouldn't get update.
add = &common.GraphQLParams{
Query: `mutation{
addTodo(input: [
Expand All @@ -658,14 +660,15 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep
}
}`,
}

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)
res, err = subscriptionClient1.RecvMsg()
require.NoError(t, err)
require.Nil(t, res) // 2nd subscription should get the empty response as subscription has expired.
}

func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndependently(t *testing.T) {
t.Skip()
dg, err := testutil.DgraphClient(groupOnegRPC)
require.NoError(t, err)
testutil.DropAll(t, dg)
Expand Down Expand Up @@ -712,8 +715,9 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

jwtToken, err := metaInfo.GetSignedToken("secret", 10*time.Second)
jwtToken, err := metaInfo.GetSignedToken("secret", subExp)
require.NoError(t, err)

// first subscription
Expand Down Expand Up @@ -757,10 +761,11 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend

addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

// 2nd subscription
metaInfo.AuthVars["USER"] = "pawan"
jwtToken, err = metaInfo.GetSignedToken("secret", 20*time.Second)
jwtToken, err = metaInfo.GetSignedToken("secret", 2*subExp)
require.NoError(t, err)
payload = fmt.Sprintf(`{"Authorization": "%s"}`, jwtToken)
subscriptionClient1, err := common.NewGraphQLSubscription(subscriptionEndpoint, &schema.Request{
Expand All @@ -784,7 +789,7 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend
string(resp.Data))

// Wait for JWT to expire for 1st subscription.
time.Sleep(10 * time.Second)
time.Sleep(subExp)

// Add another TODO for jatin for which 1st subscription shouldn't get updates.
add = &common.GraphQLParams{
Expand All @@ -803,7 +808,7 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend
}
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
require.NoError(t, err)
time.Sleep(time.Second)
// 1st subscription should get the empty response as subscription has expired
res, err = subscriptionClient.RecvMsg()
require.NoError(t, err)
Expand All @@ -826,11 +831,12 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend
}
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)

time.Sleep(time.Second)
res, err = subscriptionClient1.RecvMsg()
require.NoError(t, err)
err = json.Unmarshal(res, &resp)
require.NoError(t, err)
require.Nil(t, resp.Errors)
// 2nd one still running and should get the update
require.JSONEq(t, `{"queryTodo": [
{
Expand All @@ -844,7 +850,7 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend

// add delay for 2nd subscription to timeout
// Wait for JWT to expire.
time.Sleep(10 * time.Second)
time.Sleep(subExp)
// Add another TODO for pawan for which 2nd subscription shouldn't get updates.
add = &common.GraphQLParams{
Query: `mutation{
Expand All @@ -861,7 +867,11 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend
}`,
}

// 2nd subscription should get the empty response as subscriptio has expired
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)
time.Sleep(time.Second)

// 2nd subscription should get the empty response as subscription has expired
res, err = subscriptionClient1.RecvMsg()
require.NoError(t, err)
require.Nil(t, res)
Expand Down