Skip to content

Commit

Permalink
fix(GraphQL): fixes flaky test for subscriptions. (#6065) (#6724)
Browse files Browse the repository at this point in the history
(cherry picked from commit b4c6a73)
  • Loading branch information
JatinDev543 authored Nov 3, 2020
1 parent 73e8738 commit 60ca856
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 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

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,16 +106,15 @@ func TestSubscription(t *testing.T) {
}
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)

time.Sleep(time.Second)
subscriptionClient, err := common.NewGraphQLSubscription(subscriptionEndpoint, &schema.Request{
Query: `subscription{
getProduct(productID: "0x2"){
queryProduct{
name
}
}`,
}, `{}`)
require.Nil(t, err)

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

Expand All @@ -123,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 @@ -143,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 @@ -155,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 @@ -173,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 @@ -227,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 @@ -290,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 @@ -319,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 @@ -372,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 @@ -400,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 @@ -421,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 @@ -502,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 @@ -549,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 @@ -576,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 @@ -591,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 @@ -619,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 @@ -640,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 @@ -656,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 @@ -710,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 @@ -755,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 @@ -782,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 @@ -801,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 @@ -824,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 @@ -842,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 @@ -859,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

0 comments on commit 60ca856

Please sign in to comment.