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
55 changes: 32 additions & 23 deletions graphql/e2e/subscription/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const (
`
)

var subExp = 10 * time.Second

func TestSubscription(t *testing.T) {

dg, err := testutil.DgraphClient(groupOnegRPC)
Expand All @@ -89,6 +91,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,7 +107,6 @@ func TestSubscription(t *testing.T) {
}
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)

subscriptionClient, err := common.NewGraphQLSubscription(subscriptionEndpoint, &schema.Request{
Query: `subscription{
getProduct(productID: "0x2"){
Expand Down Expand Up @@ -143,7 +145,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 Down Expand Up @@ -173,7 +175,7 @@ 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)

Expand Down Expand Up @@ -228,7 +230,7 @@ func TestSubscriptionAuth(t *testing.T) {
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)

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 +292,7 @@ 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 +322,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 @@ -400,7 +404,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 +425,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 @@ -456,7 +461,6 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep
"USER": "jatin",
"ROLE": "USER",
}

add = &common.GraphQLParams{
Query: `mutation{
addTodo(input: [
Expand All @@ -475,7 +479,7 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)

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

// first subscription
Expand All @@ -501,7 +505,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 @@ -516,16 +520,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 @@ -544,6 +546,7 @@ 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)
Expand All @@ -553,6 +556,7 @@ func TestSubscriptionAuth_SameQueryAndClaimsButDifferentExpiry_ShouldExpireIndep
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 @@ -565,8 +569,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 @@ -581,7 +585,9 @@ 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.
Expand Down Expand Up @@ -635,7 +641,7 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend
addResult = add.ExecuteAsPost(t, graphQLEndpoint)
require.Nil(t, addResult.Errors)

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

// first subscription
Expand Down Expand Up @@ -679,10 +685,10 @@ 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 @@ -697,16 +703,14 @@ func TestSubscriptionAuth_SameQueryDifferentClaimsAndExpiry_ShouldExpireIndepend

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":"pawan","text":"GraphQL is exciting!!"}]}`,
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 @@ -725,7 +729,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 @@ -748,11 +752,13 @@ 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 @@ -766,7 +772,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 @@ -783,7 +789,10 @@ 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